Skip to content

Commit

Permalink
tests: Add API and Session tests regarding parallel calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kidroca committed Dec 2, 2021
1 parent badccf5 commit 2a2c367
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/unit/APITest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import _ from 'underscore';
import * as API from '../../src/libs/API';
import * as Network from '../../src/libs/Network';
import HttpUtils from '../../src/libs/HttpUtils';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
import CONFIG from '../../src/CONFIG';

Network.setIsReady(true);

test('Authenticate should not make parallel auth requests', () => {
// We're setting up a basic case where all requests succeed
const xhr = jest.spyOn(HttpUtils, 'xhr').mockResolvedValue({jsonCode: 200});

// Given multiple auth calls happening at the same time
_.each(_.range(5), () => API.Authenticate({
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
partnerUserID: 'testUserId',
partnerUserSecret: 'testUserSecret',
}));

// Then a single auth request should be made
return waitForPromisesToResolve()
.then(() => {
expect(xhr).toHaveBeenCalledTimes(1);
});
});

test('Multiple Authenticate calls should be resolved with the same value', () => {
// A mock where only the first xhr responds with 200 and all the rest 999
const mockResponse = {jsonCode: 200};
jest.spyOn(HttpUtils, 'xhr')
.mockResolvedValueOnce(mockResponse)
.mockResolvedValue({jsonCode: 999});

// Given multiple auth calls happening at the same time
const tasks = _.map(_.range(5), () => API.Authenticate({
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
partnerUserID: 'testUserId',
partnerUserSecret: 'testUserSecret',
}));

// Then they all should be resolved from the first response
return Promise.all(tasks)
.then((results) => {
expect(_.size(results)).toEqual(5);
_.each(results, response => expect(response).toBe(mockResponse));
});
});
29 changes: 29 additions & 0 deletions tests/unit/SessionTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import _ from 'underscore';
import * as Network from '../../src/libs/Network';
import * as Session from '../../src/libs/actions/Session';
import * as Pusher from '../../src/libs/Pusher/pusher';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';

Network.setIsReady(true);

jest.mock('../../src/libs/API', () => ({
__esModule: true,
reauthenticate: jest.fn()
.mockResolvedValue({jsonCode: 200}),
}));

jest.mock('../../src/libs/Pusher/pusher', () => ({
__esModule: true,
reconnect: jest.fn(),
}));

test('Multiple reauthenticatePusher calls should result in a single reconnect call', () => {
// Given multiple reauthenticate calls happening at the same time
_.each(_.range(5), () => Session.reauthenticatePusher());

// Then a single reconnect call should be made
return waitForPromisesToResolve()
.then(() => {
expect(Pusher.reconnect).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2a2c367

Please sign in to comment.