forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_current_user.test.js
45 lines (36 loc) · 1.33 KB
/
fetch_current_user.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2004-present Facebook. All Rights Reserved.
jest.mock('jquery');
beforeEach(() => jest.resetModules());
it('calls into $.ajax with the correct params', () => {
const $ = require('jquery');
const fetchCurrentUser = require('../fetchCurrentUser');
// Call into the function we want to test
const dummyCallback = () => {};
fetchCurrentUser(dummyCallback);
// Now make sure that $.ajax was properly called during the previous
// 2 lines
expect($.ajax).toHaveBeenCalledWith({
success: expect.any(Function),
type: 'GET',
url: 'http://example.com/currentUser',
});
});
it('calls the callback when $.ajax requests are finished', () => {
const $ = require('jquery');
const fetchCurrentUser = require('../fetchCurrentUser');
// Create a mock function for our callback
const callback = jest.fn();
fetchCurrentUser(callback);
// Now we emulate the process by which `$.ajax` would execute its own
// callback
$.ajax.mock.calls[0 /*first call*/][0 /*first argument*/].success({
firstName: 'Bobby',
lastName: 'Marley',
});
// And finally we assert that this emulated call by `$.ajax` incurred a
// call back into the mock function we provided as a callback
expect(callback.mock.calls[0 /*first call*/][0 /*first arg*/]).toEqual({
fullName: 'Bobby Marley',
loggedIn: true,
});
});