/
RelevantRunSpecsDataSource.spec.ts
280 lines (226 loc) · 8.15 KB
/
RelevantRunSpecsDataSource.spec.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import chai from 'chai'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import debugLib from 'debug'
import { GraphQLInt, GraphQLString } from 'graphql'
import { DataContext } from '../../../src'
import { createTestDataContext } from '../helper'
import { RelevantRunSpecsDataSource } from '../../../src/sources'
import { FAKE_PROJECT_ONE_RUNNING_RUN_ONE_SPEC } from './fixtures/graphqlFixtures'
import { createGraphQL } from '../helper-graphql'
import dedent from 'dedent'
chai.use(sinonChai)
const { expect } = chai
const debug = debugLib('cypress:data-context:test:sources:RelevantRunSpecsDataSource')
describe('RelevantRunSpecsDataSource', () => {
let ctx: DataContext
let dataSource: RelevantRunSpecsDataSource
beforeEach(() => {
ctx = createTestDataContext('open')
dataSource = new RelevantRunSpecsDataSource(ctx)
sinon.stub(ctx.project, 'projectId').resolves('test123')
})
describe('getRelevantRunSpecs()', () => {
it('returns no specs or statuses when no specs found for run', async () => {
const result = await dataSource.getRelevantRunSpecs([])
expect(result).to.eql([])
})
it('returns the runs the cloud sends and sets the polling interval', async () => {
sinon.stub(ctx.cloud, 'executeRemoteGraphQL').resolves(FAKE_PROJECT_ONE_RUNNING_RUN_ONE_SPEC)
expect(dataSource.pollingInterval).to.eql(15)
const result = await dataSource.getRelevantRunSpecs(['fake-id'])
expect(result).to.eql(FAKE_PROJECT_ONE_RUNNING_RUN_ONE_SPEC.data.cloudNodesByIds)
expect(dataSource.pollingInterval).to.eql(20)
})
})
describe('polling', () => {
let clock: sinon.SinonFakeTimers
beforeEach(() => {
clock = sinon.useFakeTimers()
})
afterEach(() => {
clock.restore()
})
it('polls and emits changes', async () => {
const testData = FAKE_PROJECT_ONE_RUNNING_RUN_ONE_SPEC
//clone the fixture so it is not modified for future tests
const FAKE_PROJECT = JSON.parse(JSON.stringify(testData)) as typeof testData
const runId = testData.data.cloudNodesByIds[0].id
sinon.stub(ctx.cloud, 'executeRemoteGraphQL').resolves(FAKE_PROJECT)
const query = `
query Test {
test {
id
runNumber
completedInstanceCount
totalInstanceCount
totalTests
status
}
}
`
const fields = {
id: {
type: GraphQLString,
},
runNumber: {
type: GraphQLString,
},
completedInstanceCount: {
type: GraphQLInt,
},
totalInstanceCount: {
type: GraphQLInt,
},
totalTests: {
type: GraphQLInt,
},
status: {
type: GraphQLString,
},
}
return createGraphQL(query, fields, async (source, args, context, info) => {
const subscriptionIterator = dataSource.pollForSpecs(runId, info)
const firstEmit = await subscriptionIterator.next()
expect(firstEmit, 'should emit because of first value').to.eql({ done: false, value: FAKE_PROJECT.data.cloudNodesByIds[0] })
FAKE_PROJECT.data.cloudNodesByIds[0].totalInstanceCount++
debug('**** tick after total instance count increase')
await clock.nextAsync()
const secondEmit = await subscriptionIterator.next()
expect(secondEmit, 'should emit because of updated "totalInstanceCount"').to.eql({ done: false, value: FAKE_PROJECT.data.cloudNodesByIds[0] })
FAKE_PROJECT.data.cloudNodesByIds[0].scheduledToCompleteAt = (new Date()).toISOString()
debug('**** tick after adding scheduledToCompleteAt')
await clock.nextAsync()
const thirdEmit = await subscriptionIterator.next()
expect(thirdEmit, 'should emit again because of updated "scheduledToCompleteAt"').to.eql({ done: false, value: FAKE_PROJECT.data.cloudNodesByIds[0] })
FAKE_PROJECT.data.cloudNodesByIds[0].totalTests++
debug('**** tick after testCounts increase')
await clock.nextAsync()
const forthEmit = await subscriptionIterator.next()
expect(forthEmit, 'should emit again because of updated "testCounts"').to.eql({ done: false, value: FAKE_PROJECT.data.cloudNodesByIds[0] })
FAKE_PROJECT.data.cloudNodesByIds[0].status = 'FAILED'
debug('**** tick after setting status Failed')
await clock.nextAsync()
const finalEmit = await subscriptionIterator.next()
expect(finalEmit, 'should emit again because of updated "status"').to.eql({ done: false, value: FAKE_PROJECT.data.cloudNodesByIds[0] })
subscriptionIterator.return(undefined)
return {}
}).then((result) => {
if (result.errors) {
throw result.errors[0]
}
const expected = {
data: {
test: {
completedInstanceCount: null,
id: null,
runNumber: null,
status: null,
totalInstanceCount: null,
totalTests: null,
},
},
}
expect(result).to.eql(expected)
})
})
it('should create query', async () => {
const gqlStub = sinon.stub(ctx.cloud, 'executeRemoteGraphQL').resolves({ data: {} })
const fields = {
value: {
type: GraphQLString,
},
value2: {
type: GraphQLString,
},
value3: {
type: GraphQLString,
},
}
const query = `
query Test {
test {
value
value2
}
}
`
const query2 = `
query Test {
test {
value2
value3
}
}
`
let iterator1: ReturnType<RelevantRunSpecsDataSource['pollForSpecs']>
let iterator2: ReturnType<RelevantRunSpecsDataSource['pollForSpecs']>
return createGraphQL(query, fields, async (source, args, context, info) => {
iterator1 = dataSource.pollForSpecs('runId', info)
})
.then(() => {
const expected =
dedent`query RelevantRunSpecsDataSource_Specs($ids: [ID!]!) {
cloudNodesByIds(ids: $ids) {
id
... on CloudRun {
...Subscriptions
}
}
pollingIntervals {
runByNumber
}
}
fragment Subscriptions on Test {
...Fragment0
}
fragment Fragment0 on Test {
value
value2
}`
expect(gqlStub).to.have.been.called
expect(gqlStub.firstCall.args[0]).to.haveOwnProperty('operation')
expect(gqlStub.firstCall.args[0].operation, 'should match initial query with one fragment').to.eql(`${expected }\n`)
})
.then(() => {
return createGraphQL(query2, fields, async (source, args, context, info) => {
iterator2 = dataSource.pollForSpecs('runId', info)
await clock.nextAsync()
})
})
.then(() => {
const expected =
dedent`query RelevantRunSpecsDataSource_Specs($ids: [ID!]!) {
cloudNodesByIds(ids: $ids) {
id
... on CloudRun {
...Subscriptions
}
}
pollingIntervals {
runByNumber
}
}
fragment Subscriptions on Test {
...Fragment0
...Fragment1
}
fragment Fragment0 on Test {
value
value2
}
fragment Fragment1 on Test {
value2
value3
}`
expect(gqlStub).to.have.been.calledTwice
expect(gqlStub.secondCall.args[0]).to.haveOwnProperty('operation')
expect(gqlStub.secondCall.args[0].operation, 'should match second query with two fragments').to.eql(`${expected }\n`)
})
.then(() => {
iterator1.return(undefined)
iterator2.return(undefined)
})
})
})
})