diff --git a/__mocks__/mockData/Goals/mockData.ts b/__mocks__/mockData/Goals/mockData.ts new file mode 100644 index 00000000..2fb7e9f8 --- /dev/null +++ b/__mocks__/mockData/Goals/mockData.ts @@ -0,0 +1,84 @@ +export const mockUsersData = [ + { + incompleteUserDetails: false, + discordJoinedAt: '2023-05-18T08:42:15.623000+00:00', + discordId: '892345671234567890', + linkedin_id: 'user7891', + yoe: 3, + picture: { + publicId: 'profile/9kLmxpQw2YtRvCcc12AB/xyz9abcdwxyzdef123', + url: 'https://res.cloudinary.com/realdevsquad/image/upload/v1701234567/profile/9kLmxpQw2YtRvCcc12AB/xyz9abcdwxyzdef123.jpg', + }, + github_created_at: 1612345678000, + github_display_name: 'user7891', + github_id: 'dev7891', + id: '9kLmxpQw2YtRvCcc12AB', + twitter_id: 'coder7891', + status: 'active', + github_user_id: '67891234', + profileURL: 'https://my-profile-service-yhg4.onrender.com', + website: 'https://user7891.dev', + company: 'TechStartup XYZ', + designation: 'Backend Developer', + instagram_id: 'dev.7891', + profileStatus: 'ACTIVE', + roles: { + archived: false, + in_discord: true, + member: true, + }, + username: 'dev-user-7891', + last_name: 'user7891', + first_name: 'test user', + updated_at: 1729349563939, + created_at: 1729349563328, + }, + { + incompleteUserDetails: false, + discordJoinedAt: '2022-09-30T15:23:44.789000+00:00', + discordId: '123456789012345678', + linkedin_id: 'user4567', + yoe: 1, + picture: { + publicId: 'profile/8jKnWpLv1XsQbBaa45CD/abc8xyzawxyzabc890', + url: 'https://res.cloudinary.com/realdevsquad/image/upload/v1698765432/profile/8jKnWpLv1XsQbBaa45CD/abc8xyzawxyzabc890.jpg', + }, + github_created_at: 1598765432000, + github_display_name: 'user4567', + github_id: 'dev4567', + id: '8jKnWpLv1XsQbBaa45CD', + twitter_id: 'coder4567', + status: 'active', + github_user_id: '34567891', + profileURL: 'https://my-profile-service-yhg4.onrender.com', + website: 'https://user4567.tech', + company: 'None', + designation: 'Junior Developer', + instagram_id: '', + profileStatus: 'BLOCKED', + roles: { + archived: false, + in_discord: true, + member: false, + }, + username: 'dev-user-4567', + last_name: 'user4567', + first_name: 'dev', + updated_at: 1729349599999, + created_at: 1729349580000, + }, +]; + +export const mockGoalData = { + data: { + type: 'Goal', + attributes: { + title: 'test title', + description: 'test description', + created_by: 'testUserId', + assigned_to: 'testAssignedUserId', + ends_on: '2026-01-01T00:00:00Z', + assigned_by: 'testAssignedByUserId', + }, + }, +}; diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index b8e0e46f..4db3f3c1 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -2,9 +2,11 @@ import React from 'react'; import { screen, render, fireEvent } from '@testing-library/react-native'; import AuthScreen from '../src/screens/AuthScreen/AuthScreen'; import Strings from '../src/i18n/en'; -import { Toast } from 'react-native-toast-message/lib/src/Toast'; import { Provider } from 'react-redux'; import { configureStore } from '@reduxjs/toolkit'; +import { Linking } from 'react-native'; +import AuthApis from '../src/constants/apiConstant/AuthApi'; +import ProviderWrapper from '../src/utils/tests/ProviderWrapper'; jest.mock('react-redux', () => { return { @@ -13,22 +15,47 @@ jest.mock('react-redux', () => { }; }); -it.skip('AuthScreen is rendered', () => { - render(); +jest.mock('react-native/Libraries/Linking/Linking', () => ({ + openURL: jest.fn(() => Promise.resolve('mockResolve')), + getInitialURL: jest.fn(() => Promise.resolve('mockResolve')), + addEventListener: jest.fn(), +})); + +it('AuthScreen is rendered', () => { + render( + + + , + ); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); -it.skip('Clicking on Sign in with Github shows a toast', async () => { +it('Clicking on Sign in with Github opens browser', async () => { + const mockBuildUrl = (url: string, params: { [key: string]: string }) => { + const queryString = Object.keys(params) + .map((key) => `${key}=${params[key]}`) + .join('&'); + + return `${url}?${queryString}`; + }; + const queryParams = { + sourceUtm: 'rds-mobile-app', + redirectURL: 'https://realdevsquad.com/', + }; + const baseUrl = AuthApis.GITHUB_AUTH_API; + const githubUrl = mockBuildUrl(baseUrl, queryParams); + render( - <> + - - , + , ); + const githubSignInBtn = screen.getByText(Strings.SIGN_IN_BUTTON_TEXT); fireEvent.press(githubSignInBtn); - screen.getByText(/Sign in with GitHub coming soon/i); + expect(Linking.openURL).toHaveBeenCalledTimes(1); + expect(Linking.openURL).toHaveBeenCalledWith(githubUrl); }); describe('AuthScreen', () => { diff --git a/__tests__/Component-test.tsx b/__tests__/Component-test.tsx index d15a0bf1..05f8bf22 100644 --- a/__tests__/Component-test.tsx +++ b/__tests__/Component-test.tsx @@ -86,7 +86,7 @@ const DATA = { assigned_by: 'admin', }; -test.skip('setTimeout called which calls other two functions remove and changecard', () => { +test('setTimeout called which calls other two functions remove and changecard', () => { const { getByTestId } = render( {}); describe('MainScreen', () => { @@ -24,6 +30,9 @@ describe('MainScreen', () => { test('navigates to MemberScreen when "Assigned To" is pressed', async () => { const navigateMock = jest.fn(); + axios.get.mockResolvedValue({ + data: { users: mockUsersData, message: 'Users returned successfully!' }, + }); const { getByTestId, findByTestId } = render( , ); @@ -59,13 +68,58 @@ describe('MainScreen', () => { expect(userContainer).toBeNull(); }); - test.skip('navigates to FormScreen when "Create" button is pressed', () => { - const navigateMock = jest.fn(); - const { getByText } = render( - , + test('navigates to Goal Screen when "Create Goal" button is pressed', async () => { + const spyAlert = jest.spyOn(Alert, 'alert'); + axios.get.mockResolvedValue({ + data: { users: mockUsersData, message: 'Users returned successfully!' }, + }); + axios.post.mockResolvedValue(mockGoalData); + + const { + getByTestId, + getByPlaceholderText, + getByText, + findByTestId, + getAllByText, + } = render(); + + const createGoalButton = getByText(/create goal/i); + const titleInput = getByPlaceholderText( + 'Enter title max of 50 characters.', ); - const createButton = getByText('Create'); - fireEvent.press(createButton); - expect(navigateMock).toHaveBeenCalledWith('Form screen'); + const descriptionInput = getByPlaceholderText('Enter max 200 characters.'); + const selectUserButton = getByTestId('dropdown'); + + fireEvent.press(selectUserButton); + const userContainer = await findByTestId('user-container'); + expect(userContainer).toBeTruthy(); + + const userInput = getByPlaceholderText('Search User'); + await waitFor(() => { + fireEvent.changeText(userInput, 'test user'); + }); + const userItems = getAllByText(/test user/i); + expect(userItems).toBeTruthy(); + + await waitFor(() => { + fireEvent.press(userItems[0]); + fireEvent.changeText(titleInput, 'Test Goal'); + fireEvent.changeText(descriptionInput, 'Test Description'); + }); + + fireEvent.press(createGoalButton); + + await waitFor(() => { + expect(Alert.alert).toHaveBeenCalledWith( + 'Success', + `Task has been created and assigned to test user`, + [{ text: 'OK', onPress: expect.any(Function) }], + ); + }); + + const alertMockCall = spyAlert.mock.calls?.[0]?.[2]?.[0]; + alertMockCall?.onPress!(); + + expect(navigationProp.navigate).toHaveBeenCalledWith('GoalsScreen'); }); }); diff --git a/jest-setup.js b/jest-setup.js index d3bfbbdb..6b0abfeb 100644 --- a/jest-setup.js +++ b/jest-setup.js @@ -1,6 +1,9 @@ import { jest } from '@jest/globals'; import mockRNDeviceInfo from 'react-native-device-info/jest/react-native-device-info-mock'; +import mockRNCNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js'; + require('react-native-reanimated/src/jestUtils').setUpTests(); jest.mock('react-native-device-info', () => mockRNDeviceInfo); jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter'); +jest.mock('@react-native-community/netinfo', () => mockRNCNetInfo); diff --git a/src/utils/tests/ProviderWrapper.tsx b/src/utils/tests/ProviderWrapper.tsx new file mode 100644 index 00000000..774be9bc --- /dev/null +++ b/src/utils/tests/ProviderWrapper.tsx @@ -0,0 +1,16 @@ +import React from 'react'; +import { Provider as StoreProvider } from 'react-redux'; +import { AuthProvider } from '../../context/AuthContext'; +import { store } from '../../../App'; + +interface ProviderWrapperProps { + children: JSX.Element; +} + +const ProviderWrapper: React.FC = ({ children }) => ( + + {children} + +); + +export default ProviderWrapper;