/
integration.js
171 lines (149 loc) · 4.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Octokit } from '@octokit/core'; // eslint-disable-line import/no-extraneous-dependencies
import { paginateGraphql } from '@octokit/plugin-paginate-graphql';
import { graphql, HttpResponse } from 'msw'; // https://mswjs.io/docs/getting-started
import { setupServer } from 'msw/node'; // https://mswjs.io/docs/getting-started/integrate/node
import { ApiWrapper } from '../apiwrapper';
import { RepositoryProjectsManager } from '../projects.js'; // eslint-disable-line import/extensions
const GraphQlOctokit = Octokit.plugin(paginateGraphql);
const octokit = new GraphQlOctokit({ auth: 'fake-token-value' }); // don't use default GITHUB_TOKEN token from env
const apiWrapper = new ApiWrapper({ octokit });
const rpm = new RepositoryProjectsManager({ apiWrapper, ownerName: 'acme', repositoryName: 'example-repository' });
let server;
const mock = ({ action, matcher, data }) => {
const actions = {
mutation: graphql.mutation,
query: graphql.query,
};
return actions[action](matcher, () => HttpResponse.json({ data }), { once: true });
};
describe('RepositoryProjectsManager integration test', () => {
beforeAll(() => {
server = setupServer();
server.listen();
});
afterEach(() => {
server.close();
});
beforeEach(() => {
server.use(
// fetchRepositoryAndProjects
mock({
action: 'query',
matcher: /paginate/,
data: {
repository: {
name: 'example-repository',
id: 'R_0000000001',
projectsV2: {
nodes: [
{
id: 'PVT_0000000000000001',
title: 'layer-100/bar',
number: 1099,
fields: {
nodes: [
{},
{},
{
id: 'PVTSSF_00000000000000000000001',
name: 'Status',
options: [
{
id: '00000001',
name: 'Todo',
},
{
id: '00000002',
name: 'In Progress',
},
{
id: '00000003',
name: 'Done',
},
],
},
{},
{},
],
},
},
],
pageInfo: {
hasNextPage: false,
endCursor: 'Nw',
},
},
},
},
}),
// fetchAssignedProjects
mock({
action: 'query',
matcher: /paginate/,
data: {
node: {
number: 74,
projectsV2: {
nodes: [],
pageInfo: {
hasNextPage: false,
endCursor: 'MQ',
},
},
},
},
}),
// fetchAssignedProjects (2nd call)
mock({
action: 'query',
matcher: /paginate/,
data: {
node: {
number: 74,
projectsV2: {
nodes: [
{
id: 'PVT_0000000000000001',
title: 'layer-100/bar',
},
],
pageInfo: {
hasNextPage: false,
endCursor: 'MQ',
},
},
},
},
}),
mock({
action: 'mutation',
matcher: /assignPRtoProject/,
data: {
addProjectV2ItemById: {
item: {
id: 'PVTI_0000000000000001',
},
},
},
}),
mock({
action: 'mutation',
matcher: /updateItemFieldValue/,
data: {
updateProjectV2ItemFieldValue: {
projectV2Item: {
id: 'PVTI_0000000000000001',
},
},
},
}),
);
});
test('when the PR is not assigned to a project yet', async () => {
const titles = ['layer-100/bar'];
const pullRequestNumber = 'PR_0000000000000001';
const outputProjects = await rpm.assign(pullRequestNumber, titles);
const outputTitles = outputProjects.map((p) => p.title);
expect(outputTitles).toEqual(titles);
});
});