Skip to content

Commit

Permalink
queueMarkAsRead: Adapt to use Lolex in tests.
Browse files Browse the repository at this point in the history
Simplify tests significantly by using Jest.

In addition to changes in the tests proper, this also sets the
internal variable `lastSentTime` to an initial value of `-Infinity`
rather than `0`: Lolex's fake clock is initially set to a time of `0`,
which badly confuses the original code.

(An alternative solution would be to bump Lolex's initial time to some
value greater than 0, but this seems more clearly a hack.)
  • Loading branch information
rk-for-zulip committed Feb 11, 2020
1 parent e6e6054 commit 744a98b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
38 changes: 18 additions & 20 deletions src/api/__tests__/queueMarkAsRead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
import queueMarkAsRead, { resetAll } from '../queueMarkAsRead';
import * as messagesFlags from '../messages/messagesFlags';
import * as eg from '../../__tests__/exampleData';
import { Lolex } from '../../__tests__/aux/lolex';

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

jest.useFakeTimers();

describe('queueMarkAsRead', () => {
beforeEach(() => {
resetAll();
let lolex: Lolex;

beforeAll(() => {
lolex = new Lolex();
});

afterAll(() => {
lolex.dispose();
});

afterEach(() => {
jest.clearAllMocks();
jest.clearAllTimers();
lolex.clearAllTimers();
resetAll();
});

test('should not call messagesFlags on consecutive calls of queueMarkAsRead, setTimout on further calls', () => {
Expand All @@ -21,36 +30,25 @@ describe('queueMarkAsRead', () => {
queueMarkAsRead(eg.selfAuth, [7, 8, 9]);
queueMarkAsRead(eg.selfAuth, [10, 11, 12]);

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(lolex.getTimerCount()).toBe(1);
expect(messagesFlags.default).toHaveBeenCalledTimes(1);
});

test('should call messagesFlags, if calls to queueMarkAsRead are 2s apart', () => {
const start = Date.now();
// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(start);
queueMarkAsRead(eg.selfAuth, [13, 14, 15]);

// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(start + 2100);
lolex.advanceTimersByTime(2100);
queueMarkAsRead(eg.selfAuth, [16, 17, 18]);

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

test('should set timeout for time remaining for next API call to clear queue', () => {
const start = Date.now();
// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(start);
queueMarkAsRead(eg.selfAuth, [1, 2, 3]);

// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(start + 1900);
lolex.advanceTimersByTime(1900);
queueMarkAsRead(eg.selfAuth, [4, 5, 6]);

// $FlowFixMe Make flow understand about mocking
Date.now = jest.fn().mockReturnValue(start + 2001);
jest.runOnlyPendingTimers();
lolex.runOnlyPendingTimers();
expect(messagesFlags.default).toHaveBeenCalledTimes(2);
});
});
4 changes: 2 additions & 2 deletions src/api/queueMarkAsRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import messagesFlags from './messages/messagesFlags';
const debouncePeriodMs = 2000;

let unsentMessageIds = [];
let lastSentTime = 0;
let lastSentTime = -Infinity;
let timeout = null;

/** Private; exported only for tests. */
export const resetAll = () => {
unsentMessageIds = [];
lastSentTime = 0;
lastSentTime = -Infinity;
timeout = null;
};

Expand Down

0 comments on commit 744a98b

Please sign in to comment.