From 805c16570a3504770c036008b18b157448e0af2c Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Sat, 26 Oct 2024 00:13:23 +0530 Subject: [PATCH 01/15] test: created provider wrapper --- __tests__/AuthScreen-test.tsx | 5 +++-- jest-setup.js | 3 +++ src/utils/tests/index.tsx | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/utils/tests/index.tsx diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index 41c73fed..1b827052 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -3,9 +3,10 @@ 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 { customRenderWithProvider } from '../src/utils/tests'; -it.skip('AuthScreen is rendered', () => { - render(); +it('AuthScreen is rendered', () => { + customRenderWithProvider(AuthScreen); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); 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/index.tsx b/src/utils/tests/index.tsx new file mode 100644 index 00000000..69c189b1 --- /dev/null +++ b/src/utils/tests/index.tsx @@ -0,0 +1,33 @@ +import React from 'react'; +import { Provider as StoreProvider } from 'react-redux'; +import { AuthProvider } from '../../context/AuthContext'; +import { store } from '../../../App'; +import { + render, + RenderOptions, + RenderResult, +} from '@testing-library/react-native'; +import Toast from 'react-native-toast-message'; + +export function customRenderWithProvider( + Component: React.FC, + renderOptions?: RenderOptions, +): RenderResult { + return render( + + + + + , + renderOptions, + ); +} + +export const withToast = (Component: React.FC) => { + return () => ( + <> + + + + ); +}; From d9a7d15fa440b4ade52b29279b2076524e957a96 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Sat, 26 Oct 2024 00:13:23 +0530 Subject: [PATCH 02/15] test: created provider wrapper --- __tests__/AuthScreen-test.tsx | 5 +++-- jest-setup.js | 3 +++ src/utils/tests/index.tsx | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/utils/tests/index.tsx diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index b8e0e46f..5c794ea2 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -5,6 +5,7 @@ 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 { customRenderWithProvider } from '../src/utils/tests'; jest.mock('react-redux', () => { return { @@ -13,8 +14,8 @@ jest.mock('react-redux', () => { }; }); -it.skip('AuthScreen is rendered', () => { - render(); +it('AuthScreen is rendered', () => { + customRenderWithProvider(AuthScreen); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); 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/index.tsx b/src/utils/tests/index.tsx new file mode 100644 index 00000000..69c189b1 --- /dev/null +++ b/src/utils/tests/index.tsx @@ -0,0 +1,33 @@ +import React from 'react'; +import { Provider as StoreProvider } from 'react-redux'; +import { AuthProvider } from '../../context/AuthContext'; +import { store } from '../../../App'; +import { + render, + RenderOptions, + RenderResult, +} from '@testing-library/react-native'; +import Toast from 'react-native-toast-message'; + +export function customRenderWithProvider( + Component: React.FC, + renderOptions?: RenderOptions, +): RenderResult { + return render( + + + + + , + renderOptions, + ); +} + +export const withToast = (Component: React.FC) => { + return () => ( + <> + + + + ); +}; From e2ee5e5742dd302fd201be724d023a1423259689 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Tue, 29 Oct 2024 05:24:46 +0530 Subject: [PATCH 03/15] test: create goal test updated --- __mocks__/mockData/Goals/mockData.ts | 84 +++++++++++++++++++ .../Goals/components/Create-Goals-test.tsx | 68 +++++++++++++-- 2 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 __mocks__/mockData/Goals/mockData.ts 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__/Goals/components/Create-Goals-test.tsx b/__tests__/Goals/components/Create-Goals-test.tsx index ea37cb5f..501f39f3 100644 --- a/__tests__/Goals/components/Create-Goals-test.tsx +++ b/__tests__/Goals/components/Create-Goals-test.tsx @@ -1,8 +1,11 @@ import React from 'react'; -import { render, fireEvent } from '@testing-library/react-native'; -import MembersPage from '../../../src/screens/MemberScreen/MembersPage'; +import { render, fireEvent, waitFor } from '@testing-library/react-native'; import CreateGoals from '../../../src/components/ToDoComponent/SettingGoals/CreateGoals'; +import { Alert } from 'react-native'; +import { mockGoalData, mockUsersData } from '../../../__mocks__/mockData/Goals/mockData'; +const axios = require('axios'); +jest.mock('axios'); jest.mock('react-native-gesture-handler', () => {}); describe('MainScreen', () => { @@ -24,6 +27,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 +65,57 @@ 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) }], + ); + }); + + // @ts-ignore + spyAlert.mock.calls[0][2][0].onPress(); + expect(navigationProp.navigate).toHaveBeenCalledWith('GoalsScreen'); }); }); From 05776fcd84b34c9b5f930c6ce2d92e4ae6b47c9f Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 31 Oct 2024 02:08:29 +0530 Subject: [PATCH 04/15] test(AuthScreen): updated "sign with github" test case --- __tests__/AuthScreen-test.tsx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index bbc4a9ba..97587bad 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -2,11 +2,10 @@ 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 { customRenderWithProvider } from '../src/utils/tests'; +import { Linking } from 'react-native'; jest.mock('react-redux', () => { return { @@ -15,22 +14,24 @@ jest.mock('react-redux', () => { }; }); +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', () => { customRenderWithProvider(AuthScreen); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); -it.skip('Clicking on Sign in with Github shows a toast', async () => { - render( - <> - - - , - ); +it('Clicking on Sign in with Github opens browser', async () => { + customRenderWithProvider(AuthScreen); + 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); }); describe('AuthScreen', () => { From 24b2394d9fd6cd6daf3c5d69194429392b62a6ec Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Sat, 2 Nov 2024 03:55:40 +0530 Subject: [PATCH 05/15] test: updated card component skipped test case --- __tests__/Component-test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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( Date: Sat, 26 Oct 2024 00:13:23 +0530 Subject: [PATCH 06/15] test: created provider wrapper --- __tests__/AuthScreen-test.tsx | 5 +++-- jest-setup.js | 3 +++ src/utils/tests/index.tsx | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/utils/tests/index.tsx diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index b8e0e46f..5c794ea2 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -5,6 +5,7 @@ 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 { customRenderWithProvider } from '../src/utils/tests'; jest.mock('react-redux', () => { return { @@ -13,8 +14,8 @@ jest.mock('react-redux', () => { }; }); -it.skip('AuthScreen is rendered', () => { - render(); +it('AuthScreen is rendered', () => { + customRenderWithProvider(AuthScreen); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); 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/index.tsx b/src/utils/tests/index.tsx new file mode 100644 index 00000000..69c189b1 --- /dev/null +++ b/src/utils/tests/index.tsx @@ -0,0 +1,33 @@ +import React from 'react'; +import { Provider as StoreProvider } from 'react-redux'; +import { AuthProvider } from '../../context/AuthContext'; +import { store } from '../../../App'; +import { + render, + RenderOptions, + RenderResult, +} from '@testing-library/react-native'; +import Toast from 'react-native-toast-message'; + +export function customRenderWithProvider( + Component: React.FC, + renderOptions?: RenderOptions, +): RenderResult { + return render( + + + + + , + renderOptions, + ); +} + +export const withToast = (Component: React.FC) => { + return () => ( + <> + + + + ); +}; From d46aeb4dbbbe9b102eadc15a393b8426e0d12d59 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Tue, 29 Oct 2024 05:24:46 +0530 Subject: [PATCH 07/15] test: create goal test updated --- __mocks__/mockData/Goals/mockData.ts | 84 +++++++++++++++++++ .../Goals/components/Create-Goals-test.tsx | 68 +++++++++++++-- 2 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 __mocks__/mockData/Goals/mockData.ts 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__/Goals/components/Create-Goals-test.tsx b/__tests__/Goals/components/Create-Goals-test.tsx index ea37cb5f..501f39f3 100644 --- a/__tests__/Goals/components/Create-Goals-test.tsx +++ b/__tests__/Goals/components/Create-Goals-test.tsx @@ -1,8 +1,11 @@ import React from 'react'; -import { render, fireEvent } from '@testing-library/react-native'; -import MembersPage from '../../../src/screens/MemberScreen/MembersPage'; +import { render, fireEvent, waitFor } from '@testing-library/react-native'; import CreateGoals from '../../../src/components/ToDoComponent/SettingGoals/CreateGoals'; +import { Alert } from 'react-native'; +import { mockGoalData, mockUsersData } from '../../../__mocks__/mockData/Goals/mockData'; +const axios = require('axios'); +jest.mock('axios'); jest.mock('react-native-gesture-handler', () => {}); describe('MainScreen', () => { @@ -24,6 +27,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 +65,57 @@ 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) }], + ); + }); + + // @ts-ignore + spyAlert.mock.calls[0][2][0].onPress(); + expect(navigationProp.navigate).toHaveBeenCalledWith('GoalsScreen'); }); }); From fb4cc24bb252e680697e146c050805fef5220789 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 31 Oct 2024 02:08:29 +0530 Subject: [PATCH 08/15] test(AuthScreen): updated "sign with github" test case --- __tests__/AuthScreen-test.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index 5c794ea2..1a6c65ee 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -6,6 +6,7 @@ import { Toast } from 'react-native-toast-message/lib/src/Toast'; import { Provider } from 'react-redux'; import { configureStore } from '@reduxjs/toolkit'; import { customRenderWithProvider } from '../src/utils/tests'; +import { Linking } from 'react-native'; jest.mock('react-redux', () => { return { @@ -14,22 +15,24 @@ jest.mock('react-redux', () => { }; }); +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', () => { customRenderWithProvider(AuthScreen); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); -it.skip('Clicking on Sign in with Github shows a toast', async () => { - render( - <> - - - , - ); +it('Clicking on Sign in with Github opens browser', async () => { + customRenderWithProvider(AuthScreen); + 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); }); describe('AuthScreen', () => { From b6426e66c6bdbbdf76743bf8731fb5369a660330 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Sat, 2 Nov 2024 03:55:40 +0530 Subject: [PATCH 09/15] test: updated card component skipped test case --- __tests__/Component-test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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( Date: Wed, 6 Nov 2024 00:57:27 +0530 Subject: [PATCH 10/15] refactor: removed ts-ignore and update onPress call --- __tests__/Goals/components/Create-Goals-test.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/__tests__/Goals/components/Create-Goals-test.tsx b/__tests__/Goals/components/Create-Goals-test.tsx index 501f39f3..dd1822c5 100644 --- a/__tests__/Goals/components/Create-Goals-test.tsx +++ b/__tests__/Goals/components/Create-Goals-test.tsx @@ -2,7 +2,10 @@ import React from 'react'; import { render, fireEvent, waitFor } from '@testing-library/react-native'; import CreateGoals from '../../../src/components/ToDoComponent/SettingGoals/CreateGoals'; import { Alert } from 'react-native'; -import { mockGoalData, mockUsersData } from '../../../__mocks__/mockData/Goals/mockData'; +import { + mockGoalData, + mockUsersData, +} from '../../../__mocks__/mockData/Goals/mockData'; const axios = require('axios'); jest.mock('axios'); @@ -114,8 +117,7 @@ describe('MainScreen', () => { ); }); - // @ts-ignore - spyAlert.mock.calls[0][2][0].onPress(); + spyAlert.mock.calls?.[0]?.[2]?.[0].onPress!(); expect(navigationProp.navigate).toHaveBeenCalledWith('GoalsScreen'); }); }); From 18a38fe4c76a2666422b1c06e53ae2050356a26f Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Wed, 6 Nov 2024 02:02:48 +0530 Subject: [PATCH 11/15] refactor: updated variable name to improve readability --- __tests__/Goals/components/Create-Goals-test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/__tests__/Goals/components/Create-Goals-test.tsx b/__tests__/Goals/components/Create-Goals-test.tsx index dd1822c5..7dbfb968 100644 --- a/__tests__/Goals/components/Create-Goals-test.tsx +++ b/__tests__/Goals/components/Create-Goals-test.tsx @@ -117,7 +117,9 @@ describe('MainScreen', () => { ); }); - spyAlert.mock.calls?.[0]?.[2]?.[0].onPress!(); + const alertMockCall = spyAlert.mock.calls?.[0]?.[2]?.[0]; + alertMockCall?.onPress!(); + expect(navigationProp.navigate).toHaveBeenCalledWith('GoalsScreen'); }); }); From eb5525257cbd8ba0f0c02ab180b256410b870414 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 7 Nov 2024 01:23:51 +0530 Subject: [PATCH 12/15] test: added one more test for github login. --- __tests__/AuthScreen-test.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index 97587bad..1878f4b2 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -6,6 +6,7 @@ import { Provider } from 'react-redux'; import { configureStore } from '@reduxjs/toolkit'; import { customRenderWithProvider } from '../src/utils/tests'; import { Linking } from 'react-native'; +import AuthApis from '../src/constants/apiConstant/AuthApi'; jest.mock('react-redux', () => { return { @@ -27,11 +28,26 @@ it('AuthScreen is rendered', () => { }); 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); + customRenderWithProvider(AuthScreen); const githubSignInBtn = screen.getByText(Strings.SIGN_IN_BUTTON_TEXT); fireEvent.press(githubSignInBtn); expect(Linking.openURL).toHaveBeenCalledTimes(1); + expect(Linking.openURL).toHaveBeenCalledWith(githubUrl); }); describe('AuthScreen', () => { From cccf67cd83048dc9592291a2f664337f33fadbda Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 7 Nov 2024 01:29:27 +0530 Subject: [PATCH 13/15] refactor: removed render abstraction and created a wrapper provider --- __tests__/AuthScreen-test.tsx | 14 +++++++++++--- __tests__/ProviderWrapper.tsx | 18 ++++++++++++++++++ src/utils/tests/index.tsx | 33 --------------------------------- 3 files changed, 29 insertions(+), 36 deletions(-) create mode 100644 __tests__/ProviderWrapper.tsx delete mode 100644 src/utils/tests/index.tsx diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index 1878f4b2..e1c14fc1 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -4,9 +4,9 @@ import AuthScreen from '../src/screens/AuthScreen/AuthScreen'; import Strings from '../src/i18n/en'; import { Provider } from 'react-redux'; import { configureStore } from '@reduxjs/toolkit'; -import { customRenderWithProvider } from '../src/utils/tests'; import { Linking } from 'react-native'; import AuthApis from '../src/constants/apiConstant/AuthApi'; +import ProviderWrapper from './ProviderWrapper'; jest.mock('react-redux', () => { return { @@ -22,7 +22,11 @@ jest.mock('react-native/Libraries/Linking/Linking', () => ({ })); it('AuthScreen is rendered', () => { - customRenderWithProvider(AuthScreen); + render( + + + + ); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); }); @@ -42,7 +46,11 @@ it('Clicking on Sign in with Github opens browser', async () => { const baseUrl = AuthApis.GITHUB_AUTH_API; const githubUrl = mockBuildUrl(baseUrl, queryParams); - customRenderWithProvider(AuthScreen); + render( + + + + ); const githubSignInBtn = screen.getByText(Strings.SIGN_IN_BUTTON_TEXT); fireEvent.press(githubSignInBtn); diff --git a/__tests__/ProviderWrapper.tsx b/__tests__/ProviderWrapper.tsx new file mode 100644 index 00000000..9beaadc3 --- /dev/null +++ b/__tests__/ProviderWrapper.tsx @@ -0,0 +1,18 @@ +import React, { ReactNode } from 'react'; +import { Provider as StoreProvider } from 'react-redux'; +import { AuthProvider } from '../src/context/AuthContext'; +import { store } from '../App'; + +interface ProviderWrapperProps { + children: ReactNode; +} + +const ProviderWrapper: React.FC = ({ children }) => ( + + + <>{children} + + +); + +export default ProviderWrapper; diff --git a/src/utils/tests/index.tsx b/src/utils/tests/index.tsx deleted file mode 100644 index 69c189b1..00000000 --- a/src/utils/tests/index.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import React from 'react'; -import { Provider as StoreProvider } from 'react-redux'; -import { AuthProvider } from '../../context/AuthContext'; -import { store } from '../../../App'; -import { - render, - RenderOptions, - RenderResult, -} from '@testing-library/react-native'; -import Toast from 'react-native-toast-message'; - -export function customRenderWithProvider( - Component: React.FC, - renderOptions?: RenderOptions, -): RenderResult { - return render( - - - - - , - renderOptions, - ); -} - -export const withToast = (Component: React.FC) => { - return () => ( - <> - - - - ); -}; From 3cf1a3bb9f042046f10b6f9cdf556faf719400db Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Thu, 7 Nov 2024 01:34:04 +0530 Subject: [PATCH 14/15] refactor: updated ProviderWrapper location --- __tests__/AuthScreen-test.tsx | 6 +++--- {__tests__ => src/utils/tests}/ProviderWrapper.tsx | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) rename {__tests__ => src/utils/tests}/ProviderWrapper.tsx (79%) diff --git a/__tests__/AuthScreen-test.tsx b/__tests__/AuthScreen-test.tsx index e1c14fc1..4db3f3c1 100644 --- a/__tests__/AuthScreen-test.tsx +++ b/__tests__/AuthScreen-test.tsx @@ -6,7 +6,7 @@ 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 './ProviderWrapper'; +import ProviderWrapper from '../src/utils/tests/ProviderWrapper'; jest.mock('react-redux', () => { return { @@ -25,7 +25,7 @@ it('AuthScreen is rendered', () => { render( - + , ); screen.getByText(/welcome to/i); screen.getByText(/real dev squad/i); @@ -49,7 +49,7 @@ it('Clicking on Sign in with Github opens browser', async () => { render( - + , ); const githubSignInBtn = screen.getByText(Strings.SIGN_IN_BUTTON_TEXT); diff --git a/__tests__/ProviderWrapper.tsx b/src/utils/tests/ProviderWrapper.tsx similarity index 79% rename from __tests__/ProviderWrapper.tsx rename to src/utils/tests/ProviderWrapper.tsx index 9beaadc3..e66c1517 100644 --- a/__tests__/ProviderWrapper.tsx +++ b/src/utils/tests/ProviderWrapper.tsx @@ -1,7 +1,7 @@ import React, { ReactNode } from 'react'; import { Provider as StoreProvider } from 'react-redux'; -import { AuthProvider } from '../src/context/AuthContext'; -import { store } from '../App'; +import { AuthProvider } from '../../context/AuthContext'; +import { store } from '../../../App'; interface ProviderWrapperProps { children: ReactNode; From b83e36fb2f49fd5a8a313b0a1b4385a3b4f7aac5 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 8 Nov 2024 22:43:00 +0530 Subject: [PATCH 15/15] test: fix removed fragment --- src/utils/tests/ProviderWrapper.tsx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/utils/tests/ProviderWrapper.tsx b/src/utils/tests/ProviderWrapper.tsx index e66c1517..774be9bc 100644 --- a/src/utils/tests/ProviderWrapper.tsx +++ b/src/utils/tests/ProviderWrapper.tsx @@ -1,17 +1,15 @@ -import React, { ReactNode } from 'react'; +import React from 'react'; import { Provider as StoreProvider } from 'react-redux'; import { AuthProvider } from '../../context/AuthContext'; import { store } from '../../../App'; interface ProviderWrapperProps { - children: ReactNode; + children: JSX.Element; } const ProviderWrapper: React.FC = ({ children }) => ( - - <>{children} - + {children} );