diff --git a/src/app/LogoutButton/index.test.tsx b/src/app/LogoutButton/index.test.tsx new file mode 100644 index 0000000000000000000000000000000000000000..1021266b9a7accbccafccd245f41c0969bb98dd1 --- /dev/null +++ b/src/app/LogoutButton/index.test.tsx @@ -0,0 +1,50 @@ +import React from 'react'; +import { render, fireEvent, waitFor } from 'utils/testing'; +import * as ROUTES from 'constants/routes'; + +import LogoutButton from '.'; +import { UserContext } from 'provider'; + +describe('LogoutButton', () => { + const initialUser = { + id: null, + email: '', + name: '', + role: null, + }; + + const userContextMock = { + user: initialUser, + firstAuthenticated: false, + isAuthenticated: true, + isUnpaidClient: false, + isPaidClient: false, + isNutritionist: false, + isAdmin: false, + isLoading: false, + isFirstLoading: false, + signup: jest.fn(), + login: jest.fn(), + loginWithGoogle: jest.fn(), + logout: jest.fn(), + }; + + it('renders correctly', () => { + render(, ROUTES.checkout); + }); + + it('calls logout and redirects to initial page when clicked', async () => { + const { getByTestId } = render( + + + , + ROUTES.checkout, + ); + + const logoutButton = getByTestId('logoutButton'); + expect(logoutButton).toBeTruthy(); + + await waitFor(() => fireEvent.press(logoutButton)); + expect(userContextMock.logout).toBeCalledTimes(1); + }); +}); diff --git a/src/app/LogoutButton/index.tsx b/src/app/LogoutButton/index.tsx new file mode 100644 index 0000000000000000000000000000000000000000..6e065f22c12612662eb97f1b5bf8d9cf861bb3ac --- /dev/null +++ b/src/app/LogoutButton/index.tsx @@ -0,0 +1,38 @@ +import React, { FC, useContext } from 'react'; +import { UserContext } from 'provider'; +import { Button, Icon } from 'react-native-elements'; +import { StyleSheet, View } from 'react-native'; +import { useNavigation } from '@react-navigation/core'; +import * as ROUTES from 'constants/routes'; + +const LogoutButton: FC = () => { + const { logout, isAuthenticated } = useContext(UserContext); + const navigation = useNavigation(); + + const handlePress = async () => { + await logout(); + navigation.reset({ + index: 0, + routes: [{ name: ROUTES.initial }], + }); + }; + + return isAuthenticated ? ( +