-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NoQA] [Reassure] SearchPage perf tests #33872
Changes from 7 commits
d6159ca
2e5041e
e840140
e13f88e
db3f77d
c8b23a5
e1a73ab
bddd3b6
34b2c49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import {act, fireEvent, screen} from '@testing-library/react-native'; | ||
import React from 'react'; | ||
import Onyx from 'react-native-onyx'; | ||
import {measurePerformance} from 'reassure'; | ||
import _ from 'underscore'; | ||
import SearchPage from '@pages/SearchPage'; | ||
import ComposeProviders from '../../src/components/ComposeProviders'; | ||
import {LocaleContextProvider} from '../../src/components/LocaleContextProvider'; | ||
import OnyxProvider from '../../src/components/OnyxProvider'; | ||
import {CurrentReportIDContextProvider} from '../../src/components/withCurrentReportID'; | ||
import {KeyboardStateProvider} from '../../src/components/withKeyboardState'; | ||
import {WindowDimensionsProvider} from '../../src/components/withWindowDimensions'; | ||
import CONST from '../../src/CONST'; | ||
import ONYXKEYS from '../../src/ONYXKEYS'; | ||
import createPersonalDetails from '../utils/collections/personalDetails'; | ||
import createRandomReport from '../utils/collections/reports'; | ||
import PusherHelper from '../utils/PusherHelper'; | ||
import * as TestHelper from '../utils/TestHelper'; | ||
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; | ||
|
||
jest.mock('../../src/libs/Navigation/Navigation'); | ||
|
||
const mockedNavigate = jest.fn(); | ||
jest.mock('@react-navigation/native', () => { | ||
const actualNav = jest.requireActual('@react-navigation/native'); | ||
return { | ||
...actualNav, | ||
useFocusEffect: jest.fn(), | ||
useIsFocused: () => ({ | ||
navigate: mockedNavigate, | ||
}), | ||
useRoute: () => jest.fn(), | ||
useNavigation: () => ({ | ||
navigate: jest.fn(), | ||
addListener: () => jest.fn(), | ||
}), | ||
createNavigationContainerRef: jest.fn(), | ||
}; | ||
}); | ||
|
||
const getMockedReportsMap = (length = 100) => { | ||
const mockReports = Array.from({length}, (__, i) => { | ||
const reportID = i + 1; | ||
const report = createRandomReport(reportID); | ||
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`; | ||
|
||
return {[reportKey]: report}; | ||
}); | ||
|
||
return _.assign({}, ...mockReports); | ||
}; | ||
|
||
const getMockedPersonalDetailsMap = (length) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating |
||
const mockPersonalDetails = Array.from({length}, (__, i) => { | ||
const personalDetailsKey = i + 1; | ||
const personalDetails = createPersonalDetails(personalDetailsKey); | ||
return {[personalDetailsKey]: personalDetails}; | ||
}); | ||
|
||
return _.assign({}, ...mockPersonalDetails); | ||
}; | ||
|
||
const mockedReports = getMockedReportsMap(600); | ||
const mockedBetas = _.values(CONST.BETAS); | ||
const mockedPersonalDetails = getMockedPersonalDetailsMap(100); | ||
|
||
beforeAll(() => | ||
Onyx.init({ | ||
keys: ONYXKEYS, | ||
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT], | ||
registerStorageEventListener: () => {}, | ||
}), | ||
); | ||
|
||
// Initialize the network key for OfflineWithFeedback | ||
beforeEach(() => { | ||
global.fetch = TestHelper.getGlobalFetchMock(); | ||
wrapOnyxWithWaitForBatchedUpdates(Onyx); | ||
Onyx.merge(ONYXKEYS.NETWORK, {isOffline: false}); | ||
}); | ||
|
||
// Clear out Onyx after each test so that each test starts with a clean state | ||
afterEach(() => { | ||
Onyx.clear(); | ||
PusherHelper.teardown(); | ||
}); | ||
|
||
function SearchPageWrapper(args) { | ||
return ( | ||
<ComposeProviders components={[OnyxProvider, CurrentReportIDContextProvider, KeyboardStateProvider, WindowDimensionsProvider, LocaleContextProvider]}> | ||
<SearchPage | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...args} | ||
navigation={args.navigation} | ||
/> | ||
</ComposeProviders> | ||
); | ||
} | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there really no way to share such methods across Jest tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved it to |
||
* | ||
* @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 scenario = async () => { | ||
await screen.findByTestId('SearchPage'); | ||
|
||
const input = screen.getByTestId('options-selector-input'); | ||
fireEvent.changeText(input, 'Email Four'); | ||
fireEvent.changeText(input, 'Report'); | ||
fireEvent.changeText(input, 'Email Five'); | ||
}; | ||
|
||
const navigation = {addListener}; | ||
|
||
return waitForBatchedUpdates() | ||
.then(() => | ||
Onyx.multiSet({ | ||
...mockedReports, | ||
[ONYXKEYS.IS_SIDEBAR_LOADED]: true, | ||
[ONYXKEYS.PERSONAL_DETAILS_LIST]: mockedPersonalDetails, | ||
[ONYXKEYS.BETAS]: mockedBetas, | ||
[ONYXKEYS.IS_SEARCHING_FOR_REPORTS]: true, | ||
}), | ||
) | ||
.then(() => measurePerformance(<SearchPageWrapper navigation={navigation} />, {scenario, runs})); | ||
}); | ||
|
||
test('[Search Page] should render options list', async () => { | ||
const {triggerTransitionEnd, addListener} = createAddListenerMock(); | ||
const smallMockedPersonalDetails = getMockedPersonalDetailsMap(5); | ||
|
||
const scenario = async () => { | ||
await screen.findByTestId('SearchPage'); | ||
await act(triggerTransitionEnd); | ||
await screen.findByText(smallMockedPersonalDetails['1'].login); | ||
await screen.findByText(smallMockedPersonalDetails['2'].login); | ||
}; | ||
|
||
const navigation = {addListener}; | ||
|
||
return waitForBatchedUpdates() | ||
.then(() => | ||
Onyx.multiSet({ | ||
...mockedReports, | ||
[ONYXKEYS.IS_SIDEBAR_LOADED]: true, | ||
[ONYXKEYS.PERSONAL_DETAILS_LIST]: smallMockedPersonalDetails, | ||
[ONYXKEYS.BETAS]: mockedBetas, | ||
[ONYXKEYS.IS_SEARCHING_FOR_REPORTS]: true, | ||
}), | ||
) | ||
.then(() => measurePerformance(<SearchPageWrapper navigation={navigation} />, {scenario, runs})); | ||
}); | ||
|
||
test('[Search Page] should search in options list', async () => { | ||
const {triggerTransitionEnd, addListener} = createAddListenerMock(); | ||
|
||
const scenario = async () => { | ||
await screen.findByTestId('SearchPage'); | ||
const input = screen.getByTestId('options-selector-input'); | ||
|
||
fireEvent.changeText(input, mockedPersonalDetails['88'].login); | ||
await act(triggerTransitionEnd); | ||
await screen.findByText(mockedPersonalDetails['88'].login); | ||
|
||
fireEvent.changeText(input, mockedPersonalDetails['45'].login); | ||
await act(triggerTransitionEnd); | ||
await screen.findByText(mockedPersonalDetails['45'].login); | ||
}; | ||
|
||
const navigation = {addListener}; | ||
|
||
return waitForBatchedUpdates() | ||
.then(() => | ||
Onyx.multiSet({ | ||
...mockedReports, | ||
[ONYXKEYS.IS_SIDEBAR_LOADED]: true, | ||
[ONYXKEYS.PERSONAL_DETAILS_LIST]: mockedPersonalDetails, | ||
[ONYXKEYS.BETAS]: mockedBetas, | ||
[ONYXKEYS.IS_SEARCHING_FOR_REPORTS]: true, | ||
}), | ||
) | ||
.then(() => measurePerformance(<SearchPageWrapper navigation={navigation} />, {scenario, runs})); | ||
}); | ||
|
||
test('[Search Page] should click on list item', async () => { | ||
const {triggerTransitionEnd, addListener} = createAddListenerMock(); | ||
|
||
const scenario = async () => { | ||
await screen.findByTestId('SearchPage'); | ||
const input = screen.getByTestId('options-selector-input'); | ||
|
||
fireEvent.changeText(input, mockedPersonalDetails['6'].login); | ||
await act(triggerTransitionEnd); | ||
const optionButton = await screen.findByText(mockedPersonalDetails['6'].login); | ||
|
||
fireEvent.press(optionButton); | ||
}; | ||
|
||
const navigation = {addListener}; | ||
return waitForBatchedUpdates() | ||
.then(() => | ||
Onyx.multiSet({ | ||
...mockedReports, | ||
[ONYXKEYS.IS_SIDEBAR_LOADED]: true, | ||
[ONYXKEYS.PERSONAL_DETAILS_LIST]: mockedPersonalDetails, | ||
[ONYXKEYS.BETAS]: mockedBetas, | ||
[ONYXKEYS.IS_SEARCHING_FOR_REPORTS]: true, | ||
}), | ||
) | ||
.then(() => measurePerformance(<SearchPageWrapper navigation={navigation} />, {scenario, runs})); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a comment here to adhere with the style rules. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally noting this is for automated tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will do