-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathresolvers.mutation.js
More file actions
119 lines (99 loc) · 3.29 KB
/
resolvers.mutation.js
File metadata and controls
119 lines (99 loc) · 3.29 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const resolvers = require('../resolvers');
const mockContext = {
dataSources: {
userAPI: {
bookTrips: jest.fn(),
cancelTrip: jest.fn(),
findOrCreateUser: jest.fn(),
},
launchAPI: {
getLaunchesByIds: jest.fn(),
getLaunchById: jest.fn(),
},
},
user: { id: 1, email: 'a@a.a' },
};
describe('[Mutation.bookTrips]', () => {
const { bookTrips } = mockContext.dataSources.userAPI;
const { getLaunchesByIds } = mockContext.dataSources.launchAPI;
it('returns true if booking succeeds', async () => {
bookTrips.mockReturnValueOnce([{ launchId: 999 }]);
getLaunchesByIds.mockReturnValueOnce([{ id: 999, cursor: 'foo' }]);
// check the resolver response
const res = await resolvers.Mutation.bookTrips(
null,
{ launchIds: [123] },
mockContext,
);
expect(res).toEqual({
launches: [{ cursor: 'foo', id: 999 }],
message: 'trips booked successfully',
success: true,
});
// check if the dataSource was called with correct args
expect(bookTrips).toBeCalledWith({ launchIds: [123] });
});
it('returns false if booking fails', async () => {
bookTrips.mockReturnValueOnce([]);
// check the resolver response
const res = await resolvers.Mutation.bookTrips(
null,
{ launchIds: [123] },
mockContext,
);
expect(res.message).toBeDefined();
expect(res.success).toBeFalsy();
});
});
describe('[Mutation.cancelTrip]', () => {
const { cancelTrip } = mockContext.dataSources.userAPI;
const { getLaunchById } = mockContext.dataSources.launchAPI;
it('returns true if cancelling succeeds', async () => {
cancelTrip.mockReturnValueOnce(true);
getLaunchById.mockReturnValueOnce({ id: 999, cursor: 'foo' });
// check the resolver response
const res = await resolvers.Mutation.cancelTrip(
null,
{ launchId: 123 },
mockContext,
);
expect(res).toEqual({
success: true,
message: 'trip cancelled',
launches: [{ id: 999, cursor: 'foo' }],
});
// check if the dataSource was called with correct args
expect(cancelTrip).toBeCalledWith({ launchId: 123 });
});
it('returns false if cancelling fails', async () => {
cancelTrip.mockReturnValueOnce(false);
// check the resolver response
const res = await resolvers.Mutation.cancelTrip(
null,
{ launchId: 123 },
mockContext,
);
expect(res.message).toBeDefined();
expect(res.success).toBeFalsy();
});
});
describe('[Mutation.login]', () => {
const { findOrCreateUser } = mockContext.dataSources.userAPI;
it('returns base64 encoded email if successful', async () => {
const args = { email: 'a@a.a' };
findOrCreateUser.mockReturnValueOnce(true);
// check the resolver response
const res = await resolvers.Mutation.login(null, args, mockContext);
expect(res).toEqual('YUBhLmE=');
// check if the dataSource was called with correct args
expect(findOrCreateUser).toBeCalledWith(args);
});
it('returns nothing if login fails', async () => {
const args = { email: 'a@a.a' };
// simulate failed lookup/creation
findOrCreateUser.mockReturnValueOnce(false);
// check the resolver response
const res = await resolvers.Mutation.login(null, args, mockContext);
expect(res).toBeFalsy();
});
});