Skip to content

Commit

Permalink
queueMarkAsRead, test: Check messageFlags is not called in less than 2s.
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkuniya committed Aug 21, 2019
1 parent de731dd commit 529f982
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/__tests__/exampleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { createStore } from 'redux';

import type { CrossRealmBot, Message, PmRecipientUser, Stream, User } from '../api/modelTypes';
import type { GlobalState, RealmState } from '../reduxTypes';
import type { Account } from '../types';
import type { Auth, Account } from '../types';
import { ACCOUNT_SWITCH, LOGIN_SUCCESS } from '../actionConstants';
import rootReducer from '../boot/reducers';
import { authOfAccount } from '../account/accountMisc';

// TODO either fix Jest test-discovery patterns, or rename this file,
// so this dummy test isn't required.
Expand Down Expand Up @@ -66,6 +67,7 @@ const makeAccount = (user: User): Account => ({

const selfUser: User = makeUser({ name: 'self' });
const selfAccount: Account = makeAccount(selfUser);
const selfAuth: Auth = authOfAccount(selfAccount);

const otherUser: User = makeUser({ name: 'other' });

Expand Down Expand Up @@ -221,6 +223,7 @@ export const eg = {
makeCrossRealmBot,
selfUser,
selfAccount,
selfAuth,
otherUser,
crossRealmBot,
makeStream,
Expand Down
40 changes: 40 additions & 0 deletions src/api/__tests__/queueMarkAsRead-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* @flow strict-local */
import queueMarkAsRead, { resetAll } from '../queueMarkAsRead';
import * as messagesFlags from '../messages/messagesFlags';
import { eg } from '../../__tests__/exampleData';

// $FlowFixMe Make flow understand about mocking
messagesFlags.default = jest.fn(() => {});

describe('queueMarkAsRead', () => {
beforeEach(() => {
resetAll();
jest.clearAllMocks();
jest.clearAllTimers();
});

test('should not call messagesFlags on consecutive calls of queueMarkAsRead', () => {
queueMarkAsRead(eg.selfAuth, [1, 2, 3]);
queueMarkAsRead(eg.selfAuth, [4, 5, 6]);
queueMarkAsRead(eg.selfAuth, [7, 8, 9]);
queueMarkAsRead(eg.selfAuth, [10, 11, 12]);

expect(messagesFlags.default).toHaveBeenCalledTimes(1);
});

test('should call messagesFlags, if calls to queueMarkAsRead are 2s apart', async () => {
const currentDate = Date.now();
const secondCall = currentDate + 2100;
// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(currentDate);

queueMarkAsRead(eg.selfAuth, [13, 14, 15]);

// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(secondCall);

queueMarkAsRead(eg.selfAuth, [16, 17, 18]);

expect(messagesFlags.default).toHaveBeenCalledTimes(2);
});
});
9 changes: 9 additions & 0 deletions src/api/queueMarkAsRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import messagesFlags from './messages/messagesFlags';
let unsentMessageIds = [];
let lastSentTime = 0;

/**
* Exported so that it can be used in test
* See queueMarkAsRead-test.js
*/
export const resetAll = () => {
unsentMessageIds = [];
lastSentTime = 0;
};

const processQueue = (auth: Auth) => {
if (Date.now() - lastSentTime > 2000) {
messagesFlags(auth, unsentMessageIds, 'add', 'read');
Expand Down

0 comments on commit 529f982

Please sign in to comment.