Skip to content

Commit

Permalink
refactor: move createAddListenerMock to separate file, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rinej committed Jan 19, 2024
1 parent bddd3b6 commit 34b2c49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/pages/SearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const propTypes = {
/** Whether we are searching for reports in the server */
isSearchingForReports: PropTypes.bool,

/**
* The navigation prop passed by the navigator.
*
* This is required because transitionEnd event doesn't trigger in the automated testing environment.
*/
navigation: PropTypes.shape({}),
};

Expand Down
34 changes: 4 additions & 30 deletions tests/perf-test/SearchPage.perf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,8 @@ function SearchPageWrapper(args) {

const runs = CONST.PERFORMANCE_TESTS.RUNS;

/**
* This is a helper function to create a mock for the addListener function of the react-navigation library.
* Same approach as in ReportScreen.perf-test.js
*
* P.S: This can't be moved to a utils file because Jest wants any external function to stay in the scope.
*
* @returns {Object} An object with two functions: triggerTransitionEnd and addListener
*/
const createAddListenerMock = () => {
const transitionEndListeners = [];
const triggerTransitionEnd = () => {
transitionEndListeners.forEach((transitionEndListener) => transitionEndListener());
};

const addListener = jest.fn().mockImplementation((listener, callback) => {
if (listener === 'transitionEnd') {
transitionEndListeners.push(callback);
}
return () => {
_.filter(transitionEndListeners, (cb) => cb !== callback);
};
});

return {triggerTransitionEnd, addListener};
};

test('[Search Page] should interact when text input changes', async () => {
const {addListener} = createAddListenerMock();
const {addListener} = TestHelper.createAddListenerMock();

const scenario = async () => {
await screen.findByTestId('SearchPage');
Expand All @@ -147,7 +121,7 @@ test('[Search Page] should interact when text input changes', async () => {
});

test('[Search Page] should render options list', async () => {
const {triggerTransitionEnd, addListener} = createAddListenerMock();
const {triggerTransitionEnd, addListener} = TestHelper.createAddListenerMock();
const smallMockedPersonalDetails = getMockedPersonalDetails(5);

const scenario = async () => {
Expand All @@ -173,7 +147,7 @@ test('[Search Page] should render options list', async () => {
});

test('[Search Page] should search in options list', async () => {
const {triggerTransitionEnd, addListener} = createAddListenerMock();
const {triggerTransitionEnd, addListener} = TestHelper.createAddListenerMock();

const scenario = async () => {
await screen.findByTestId('SearchPage');
Expand Down Expand Up @@ -204,7 +178,7 @@ test('[Search Page] should search in options list', async () => {
});

test('[Search Page] should click on list item', async () => {
const {triggerTransitionEnd, addListener} = createAddListenerMock();
const {triggerTransitionEnd, addListener} = TestHelper.createAddListenerMock();

const scenario = async () => {
await screen.findByTestId('SearchPage');
Expand Down
25 changes: 24 additions & 1 deletion tests/utils/TestHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,27 @@ function assertFormDataMatchesObject(formData, obj) {
expect(_.reduce(Array.from(formData.entries()), (memo, x) => ({...memo, [x[0]]: x[1]}), {})).toEqual(expect.objectContaining(obj));
}

export {getGlobalFetchMock, signInWithTestUser, signOutTestUser, setPersonalDetails, buildPersonalDetails, buildTestReportComment, assertFormDataMatchesObject};
/**
* This is a helper function to create a mock for the addListener function of the react-navigation library.
*
* @returns {Object} An object with two functions: triggerTransitionEnd and addListener
*/
const createAddListenerMock = () => {
const transitionEndListeners = [];
const triggerTransitionEnd = () => {
transitionEndListeners.forEach((transitionEndListener) => transitionEndListener());
};

const addListener = jest.fn().mockImplementation((listener, callback) => {
if (listener === 'transitionEnd') {
transitionEndListeners.push(callback);
}
return () => {
_.filter(transitionEndListeners, (cb) => cb !== callback);
};
});

return {triggerTransitionEnd, addListener};
};

export {getGlobalFetchMock, signInWithTestUser, signOutTestUser, setPersonalDetails, buildPersonalDetails, buildTestReportComment, assertFormDataMatchesObject, createAddListenerMock};

0 comments on commit 34b2c49

Please sign in to comment.