diff --git a/tests/unit/APITest.js b/tests/unit/APITest.js new file mode 100644 index 000000000000..d5d3785d38c6 --- /dev/null +++ b/tests/unit/APITest.js @@ -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)); + }); +}); diff --git a/tests/unit/SessionTest.js b/tests/unit/SessionTest.js new file mode 100644 index 000000000000..dd6d98129b50 --- /dev/null +++ b/tests/unit/SessionTest.js @@ -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); + }); +});