-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add API and Session tests regarding parallel calls
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |