-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathauth.js
77 lines (70 loc) · 2.4 KB
/
auth.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// const MOCK_GROUPS_REQ_URL = 'https://api.topcoder-dev.com/v3/groups?memberId=12345&membershipType=user';
const MOCK_GROUPS_REQ_URL_V5 = 'https://api.topcoder-dev.com/v5/groups?memberId=12345&membershipType=user';
const MOCK_PROFILE_REQ_URL = 'https://api.topcoder-dev.com/v3/members/username12345';
jest.mock('isomorphic-fetch', () => jest.fn(url => Promise.resolve({
json: () => {
let content;
switch (url) {
case MOCK_GROUPS_REQ_URL_V5: content = ['Group1', 'Group2']; break;
case MOCK_PROFILE_REQ_URL: content = { userId: 12345 }; break;
default: throw new Error('Unexpected URL!');
}
return {
result: { content, status: 200 },
};
},
})));
// const fetch = require('isomorphic-fetch');
const { actions } = require('../../src');
describe('fetch with success response', () => {
beforeEach(() => jest.clearAllMocks());
// test('auth.loadProfile works as expected when authenticated', () => {
// const action = actions.auth.loadProfile('token');
// expect(action.type).toBe('AUTH/LOAD_PROFILE');
// return action.payload.then((res) => {
// // expect(fetch).toHaveBeenCalledWith(MOCK_PROFILE_REQ_URL, {
// // headers: {
// // Authorization: 'Bearer token',
// // 'Content-Type': 'application/json',
// // },
// // });
// console.log(fetch);
// console.log(MOCK_GROUPS_REQ_URL_V5);
// expect(fetch).toHaveBeenCalledWith(MOCK_GROUPS_REQ_URL_V5, {
// headers: {
// Authorization: 'Bearer token',
// 'Content-Type': 'application/json',
// },
// });
// expect(res).toEqual({
// groups: ['Group1', 'Group2'],
// userId: 12345,
// });
// });
// });
test('auth.loadProfile with empty token', () => {
const action = actions.auth.loadProfile('');
expect(action.type).toBe('AUTH/LOAD_PROFILE');
return action.payload.then((res) => {
expect(res).toBe(null);
});
});
});
describe.skip('fetch with failed response', () => {
beforeAll(() => {
global.fetch = jest.fn(() => Promise.resolve({
json: () => ({
result: { status: 404 },
}),
}));
});
test('fetch return 404', () => {
const action = actions.auth.loadProfile('token');
expect(action.type).toBe('AUTH/LOAD_PROFILE');
return action.payload.then((res) => {
expect(res).toEqual({
groups: [],
});
});
});
});