Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jest.doMock('react-native', () => {
NativeModules: {
...ReactNative.NativeModules,
BootSplash: {
hide: jest.fn(),
hide: jest.fn().mockResolvedValue(undefined),
logoSizeRatio: 1,
navigationBarHeight: 0,
},
Expand Down
152 changes: 98 additions & 54 deletions tests/ui/UnreadIndicatorsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ jest.mock('@libs/Navigation/AppNavigator/usePreloadFullScreenNavigators', () =>
TestHelper.setupApp();
TestHelper.setupGlobalFetchMock();

let hasRenderedApp = false;

beforeEach(() => {
Onyx.set(ONYXKEYS.NVP_ONBOARDING, {hasCompletedGuidedSetupFlow: true});
});

afterEach(() => {
hasRenderedApp = false;
});

function scrollUpToRevealNewMessagesBadge() {
const hintText = translateLocal('sidebarScreen.listOfChatMessages');
fireEvent.scroll(screen.getByLabelText(hintText), {
Expand Down Expand Up @@ -101,6 +107,8 @@ const USER_B_ACCOUNT_ID = 2;
const USER_B_EMAIL = 'user_b@test.com';
const USER_C_ACCOUNT_ID = 3;
const USER_C_EMAIL = 'user_c@test.com';
const TEST_AUTH_TOKEN = 'test-auth-token';
const TEST_AUTO_GENERATED_LOGIN = 'expensify.cash-abc123';
let reportAction3CreatedDate: string;
let reportAction9CreatedDate: string;
const TEN_MINUTES_AGO = subMinutes(new Date(), 10);
Expand All @@ -124,71 +132,107 @@ const createdReportAction = {
],
};

function renderAppOnce() {
if (hasRenderedApp) {
return;
}
render(<App />);
hasRenderedApp = true;
}

async function fastSignInWithTestUser() {
await Onyx.multiSet({
[ONYXKEYS.CREDENTIALS]: {
login: USER_A_EMAIL,
autoGeneratedLogin: TEST_AUTO_GENERATED_LOGIN,
autoGeneratedPassword: 'Password1',
},
[ONYXKEYS.ACCOUNT]: {
validated: true,
isUsingExpensifyCard: false,
},
[ONYXKEYS.SESSION]: {
authToken: TEST_AUTH_TOKEN,
accountID: USER_A_ACCOUNT_ID,
email: USER_A_EMAIL,
encryptedAuthToken: TEST_AUTH_TOKEN,
},
[ONYXKEYS.BETAS]: ['all'],
[ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID]: 'randomID',
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[USER_A_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_A_EMAIL, USER_A_ACCOUNT_ID, 'A'),
},
});
await waitForBatchedUpdates();
}

/**
* Sets up a test with a logged in user that has one unread chat from another user. Returns the <App/> test instance.
*/
function signInAndGetAppWithUnreadChat(): Promise<void> {
// Render the App and sign in as a test user.
render(<App />);
return waitForBatchedUpdatesWithAct()
.then(async () => {
await waitForBatchedUpdatesWithAct();
const hintText = translateLocal('loginForm.loginForm');
const loginForm = screen.queryAllByLabelText(hintText);
expect(loginForm).toHaveLength(1);
})
.then(async () => TestHelper.signInWithTestUser(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined, undefined, 'A'))
.then(() => {
subscribeToUserEvents();
return waitForBatchedUpdates();
})
.then(async () => {
reportAction3CreatedDate = format(addSeconds(TEN_MINUTES_AGO, 30), CONST.DATE.FNS_DB_FORMAT_STRING);
reportAction9CreatedDate = format(addSeconds(TEN_MINUTES_AGO, 90), CONST.DATE.FNS_DB_FORMAT_STRING);
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[USER_B_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_B_EMAIL, USER_B_ACCOUNT_ID, 'B'),
});
// Simulate setting an unread report and personal details
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {
reportID: REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastReadTime: reportAction3CreatedDate,
lastVisibleActionCreated: reportAction9CreatedDate,
lastMessageText: 'Test',
participants: {
[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
lastActorAccountID: USER_B_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, {
[createdReportActionID]: createdReportAction,
1: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 10), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '1'),
2: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 20), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '2'),
3: TestHelper.buildTestReportComment(reportAction3CreatedDate, USER_B_ACCOUNT_ID, '3'),
4: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 40), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '4'),
5: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 50), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '5'),
6: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 60), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '6'),
7: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 70), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '7'),
8: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 80), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '8'),
9: TestHelper.buildTestReportComment(reportAction9CreatedDate, USER_B_ACCOUNT_ID, '9'),
});

// We manually setting the sidebar as loaded since the onLayout event does not fire in tests
setSidebarLoaded();
return waitForBatchedUpdatesWithAct();
});
async function signInAndGetAppWithUnreadChat(): Promise<void> {
await fastSignInWithTestUser();

// Render the App and ensure initial navigation state settles.
renderAppOnce();
await waitForBatchedUpdatesWithAct();

subscribeToUserEvents();

await waitForBatchedUpdates();

reportAction3CreatedDate = format(addSeconds(TEN_MINUTES_AGO, 30), CONST.DATE.FNS_DB_FORMAT_STRING);
reportAction9CreatedDate = format(addSeconds(TEN_MINUTES_AGO, 90), CONST.DATE.FNS_DB_FORMAT_STRING);

const personalDetails = {
[USER_B_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_B_EMAIL, USER_B_ACCOUNT_ID, 'B'),
};

const report = {
reportID: REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastReadTime: reportAction3CreatedDate,
lastVisibleActionCreated: reportAction9CreatedDate,
lastMessageText: 'Test',
participants: {
[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
lastActorAccountID: USER_B_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
};

const reportActions = {
[createdReportActionID]: createdReportAction,
1: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 10), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '1'),
2: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 20), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '2'),
3: TestHelper.buildTestReportComment(reportAction3CreatedDate, USER_B_ACCOUNT_ID, '3'),
4: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 40), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '4'),
5: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 50), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '5'),
6: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 60), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '6'),
7: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 70), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '7'),
8: TestHelper.buildTestReportComment(format(addSeconds(TEN_MINUTES_AGO, 80), CONST.DATE.FNS_DB_FORMAT_STRING), USER_B_ACCOUNT_ID, '8'),
9: TestHelper.buildTestReportComment(reportAction9CreatedDate, USER_B_ACCOUNT_ID, '9'),
};

await Promise.all([
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails),
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report),
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, reportActions),
]);

// We manually setting the sidebar as loaded since the onLayout event does not fire in tests
setSidebarLoaded();
await waitForBatchedUpdatesWithAct();
}

// Skipping this test because it is flaky and will be fixed here https://github.com/Expensify/App/issues/70126
describe.skip('Unread Indicators', () => {
describe('Unread Indicators', () => {
beforeAll(() => {
PusherHelper.setup();
});

beforeEach(() => {
jest.clearAllMocks();
hasRenderedApp = false;

global.fetch = TestHelper.getGlobalFetchMock();
// Unsubscribe to pusher channels
Expand Down