-
Notifications
You must be signed in to change notification settings - Fork 840
/
integration.js
157 lines (136 loc) Β· 4.14 KB
/
integration.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const {createTestClient} = require('apollo-server-testing');
const gql = require('graphql-tag');
const nock = require('nock');
const {constructTestServer} = require('./__utils');
// the mocked REST API data
const {mockLaunchResponse} = require('../datasources/__tests__/launch');
// the mocked SQL DataSource store
const {mockStore} = require('../datasources/__tests__/user');
const GET_LAUNCHES = gql`
query launchList($after: String) {
launches(after: $after) {
cursor
hasMore
launches {
id
isBooked
rocket {
name
}
mission {
name
missionPatch
}
}
}
}
`;
const GET_LAUNCH = gql`
query launch($id: ID!) {
launch(id: $id) {
id
isBooked
rocket {
type
}
mission {
name
}
}
}
`;
const LOGIN = gql`
mutation login($email: String!) {
login(email: $email)
}
`;
const BOOK_TRIPS = gql`
mutation BookTrips($launchIds: [ID]!) {
bookTrips(launchIds: $launchIds) {
success
message
launches {
id
isBooked
}
}
}
`;
describe('Queries', () => {
it('fetches list of launches', async () => {
// create an instance of ApolloServer that mocks out context, while reusing
// existing dataSources, resolvers, and typeDefs.
// This function returns the server instance as well as our dataSource
// instances, so we can overwrite the underlying fetchers
const {server, launchAPI, userAPI} = constructTestServer({
context: () => ({user: {id: 1, email: 'a@a.a'}}),
});
// mock the datasources' underlying fetch methods, whether that's a REST
// lookup in the RESTDataSource or the store query in the Sequelize datasource
launchAPI.get = jest.fn(() => [mockLaunchResponse]);
userAPI.store = mockStore;
userAPI.store.trips.findAll.mockReturnValueOnce([
{dataValues: {launchId: 1}},
]);
// use our test server as input to the createTestClient fn
// This will give us an interface, similar to apolloClient.query
// to run queries against our instance of ApolloServer
const {query} = createTestClient(server);
const res = await query({query: GET_LAUNCHES});
expect(res).toMatchSnapshot();
});
it('fetches single launch', async () => {
const {server, launchAPI, userAPI} = constructTestServer({
context: () => ({user: {id: 1, email: 'a@a.a'}}),
});
launchAPI.get = jest.fn(() => [mockLaunchResponse]);
userAPI.store = mockStore;
userAPI.store.trips.findAll.mockReturnValueOnce([
{dataValues: {launchId: 1}},
]);
const {query} = createTestClient(server);
const res = await query({query: GET_LAUNCH, variables: {id: 1}});
expect(res).toMatchSnapshot();
});
});
describe('Mutations', () => {
it('returns login token', async () => {
const {server, launchAPI, userAPI} = constructTestServer({
context: () => {},
});
userAPI.store = mockStore;
userAPI.store.users.findOrCreate.mockReturnValueOnce([
{id: 1, email: 'a@a.a'},
]);
const {mutate} = createTestClient(server);
const res = await mutate({
mutation: LOGIN,
variables: {email: 'a@a.a'},
});
expect(res.data.login).toEqual('YUBhLmE=');
});
it('books trips', async () => {
const {server, launchAPI, userAPI} = constructTestServer({
context: () => ({user: {id: 1, email: 'a@a.a'}}),
});
// mock the underlying fetches
launchAPI.get = jest.fn();
// look up the launches from the launch API
launchAPI.get
.mockReturnValueOnce([mockLaunchResponse])
.mockReturnValueOnce([{...mockLaunchResponse, flight_number: 2}]);
// book the trip in the store
userAPI.store = mockStore;
userAPI.store.trips.findOrCreate
.mockReturnValueOnce([{get: () => ({launchId: 1})}])
.mockReturnValueOnce([{get: () => ({launchId: 2})}]);
// check if user is booked
userAPI.store.trips.findAll.mockReturnValue([{}]);
const {mutate} = createTestClient(server);
const res = await mutate({
mutation: BOOK_TRIPS,
variables: {launchIds: ['1', '2']},
});
expect(res).toMatchSnapshot();
});
});