-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
InlineSpecList.cy.tsx
253 lines (202 loc) · 8.74 KB
/
InlineSpecList.cy.tsx
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
import { Specs_InlineSpecListFragment, Specs_InlineSpecListFragmentDoc, SpecFilter_SetPreferencesDocument, RunAllSpecsDocument } from '../generated/graphql-test'
import InlineSpecList from './InlineSpecList.vue'
import { defaultMessages } from '@cy/i18n'
let specs: Array<any> = []
describe('InlineSpecList', () => {
const mountInlineSpecList = ({ specFilter, experimentalRunAllSpecs }: {specFilter?: string, experimentalRunAllSpecs?: boolean} = {}) => cy.mountFragment(Specs_InlineSpecListFragmentDoc, {
onResult: (ctx) => {
if (!ctx.currentProject?.specs) {
return ctx
}
specs = ctx.currentProject.specs = specs.map((spec) => ({ __typename: 'Spec', ...spec, id: spec.relative }))
if (specFilter) {
ctx.currentProject.savedState = { specFilter }
}
if (experimentalRunAllSpecs) {
ctx.currentProject.config = [{ field: 'experimentalRunAllSpecs', value: true }]
}
return ctx
},
render: (gqlValue) => {
return (
<div class="bg-gray-1000">
<InlineSpecList gql={gqlValue}></InlineSpecList>
</div>
)
},
})
describe('with no saved search term', () => {
beforeEach(() => {
cy.fixture('found-specs').then((foundSpecs) => specs = foundSpecs)
})
it('should render a list of specs', () => {
mountInlineSpecList()
cy.get('li')
.should('exist')
.and('have.length', 7)
// overflow is required for the virtual list to work
// this test will fail if the overflow set by `useVirtualList`
// is overridden
cy.get('[data-cy="specs-list-container"]')
.should('have.css', 'overflow-y', 'auto')
cy.percySnapshot()
})
it('should support fuzzy sort', () => {
mountInlineSpecList()
cy.get('input').type('compspec')
cy.get('li').should('have.length', 4)
.should('contain', 'src/components')
.and('contain', 'Spec-A.spec.tsx')
// Don't want the search to be too fuzzy
// sr => src, co => components, se => spec
cy.get('input').clear().type('srcose')
cy.get('li').should('have.length', 0)
})
it('should open CreateSpec modal', () => {
mountInlineSpecList()
const newSpecSelector = `[aria-label="New spec"]`
cy.get(newSpecSelector).click()
cy.contains(defaultMessages.createSpec.newSpecModalTitle).should('be.visible')
cy.percySnapshot()
})
it('should handle spec refresh', () => {
const scrollVirtualList = (lastItem: string) => {
cy.findAllByTestId('spec-row-item').last().dblclick().then(($el) => {
if (!$el.text().includes(lastItem)) {
scrollVirtualList(lastItem)
}
})
}
let _gqlValue: Specs_InlineSpecListFragment
cy.mountFragment(Specs_InlineSpecListFragmentDoc, {
onResult (ctx) {
if (ctx.currentProject?.specs) {
ctx.currentProject.specs = ctx.currentProject.specs.slice(0, 50)
}
return ctx
},
render (gqlValue) {
_gqlValue = gqlValue
return (
<div class="bg-gray-1000">
<InlineSpecList gql={gqlValue}></InlineSpecList>
</div>
)
},
}).then(() => {
const sortedSpecs = _gqlValue?.currentProject?.specs.sort((a, b) => a.relative < b.relative ? -1 : 1) || []
const firstSpec = sortedSpecs[0]
const lastSpec = sortedSpecs[sortedSpecs.length - 1]
cy.contains(firstSpec.fileName).should('be.visible')
scrollVirtualList(lastSpec.fileName)
cy.contains(lastSpec.fileName).should('be.visible')
cy.then(() => {
// Emulating a gql update that shouldn't cause a scroll snap
if (_gqlValue.currentProject?.specs) {
_gqlValue.currentProject.specs = [..._gqlValue.currentProject.specs]
}
})
cy.contains(lastSpec.fileName).should('be.visible')
const newSpec = { ...lastSpec, relative: 'zzz/my-test.spec.tsx', fileName: 'my-test' }
cy.then(() => {
// Checking that specs list refreshes when spec is added
if (_gqlValue.currentProject?.specs) {
_gqlValue.currentProject.specs = _gqlValue.currentProject.specs.concat(newSpec)
}
})
cy.contains(firstSpec.fileName).should('be.visible')
scrollVirtualList(newSpec.fileName)
cy.contains(newSpec.fileName).should('be.visible')
cy.then(() => {
// Checking that specs list refreshes when spec is deleted
if (_gqlValue.currentProject?.specs) {
_gqlValue.currentProject.specs = _gqlValue.currentProject.specs.filter(((spec) => spec.relative !== newSpec.relative))
}
})
cy.contains(firstSpec.fileName).should('be.visible')
scrollVirtualList(lastSpec.fileName)
cy.contains(newSpec.fileName).should('not.exist')
})
})
})
describe('with a saved spec filter', () => {
beforeEach(() => {
cy.fixture('found-specs').then((foundSpecs) => specs = foundSpecs)
mountInlineSpecList({ specFilter: 'saved-search-term 🗑' })
cy.findByLabelText(defaultMessages.specPage.searchPlaceholder)
.as('searchField')
cy.findByLabelText(defaultMessages.specPage.clearSearch, { selector: 'button' })
.as('searchFieldClearButton')
})
it('starts with the saved filter', () => {
cy.get('@searchField').should('have.value', 'saved-search-term 🗑')
cy.get('li').should('not.exist')
cy.get('@searchFieldClearButton').click()
cy.get('li').should('have.length.greaterThan', 0)
})
it('calls gql mutation to save updated filter', () => {
const setSpecFilterStub = cy.stub()
cy.stubMutationResolver(SpecFilter_SetPreferencesDocument, (defineResult, variables) => {
const specFilter = JSON.parse(variables.value)?.specFilter
setSpecFilterStub(specFilter)
})
// since there is a saved search, clear it out
cy.get('@searchFieldClearButton').click()
cy.get('@searchField').type('test')
cy.wrap(setSpecFilterStub).should('have.been.calledWith', 'test')
cy.get('@searchField').type('{backspace}{backspace}')
cy.wrap(setSpecFilterStub).should('have.been.calledWith', 'te')
cy.get('@searchField').type('{backspace}{backspace}')
cy.wrap(setSpecFilterStub).should('have.been.calledWith', '')
})
})
describe('Run all Specs', () => {
const hoverRunAllSpecs = (directory: string, specNumber: number) => {
let command = cy.contains('[data-cy=directory-item]', directory)
return command.realHover().then(() => {
cy.get('[data-cy=play-button]').should('exist')
cy.get(`[data-cy="run-all-specs-for-${directory}"]`).realHover().then(() => {
cy.get('[data-cy=tooltip-content]').should('contain.text', `Run ${specNumber} spec`)
})
})
}
beforeEach(() => {
cy.fixture('found-specs').then((foundSpecs) => specs = foundSpecs)
})
it('does not show feature unless experimentalRunAllSpecs is enabled', () => {
mountInlineSpecList({ experimentalRunAllSpecs: false })
cy.findByTestId('run-all-specs-for-all').should('not.exist')
cy.contains('[data-cy=directory-item]', 'src').realHover()
cy.findByTestId('run-all-specs-for-src').should('not.exist')
})
it('displays runAllSpecs when hovering over a spec-list directory row', () => {
mountInlineSpecList({ experimentalRunAllSpecs: true })
hoverRunAllSpecs('src', 4)
})
it('checks if functionality works after a search', () => {
mountInlineSpecList({ experimentalRunAllSpecs: true, specFilter: 'B' })
hoverRunAllSpecs('src/components', 1)
})
it('allows keyboard interactions to run spec groups without toggling sections', () => {
// this test is specifically to catch regressions of a bug caused by nesting controls: https://github.com/cypress-io/cypress/issues/24762
// TODO: #24966 remove this test when the structure of controls in this area has been flattened out
mountInlineSpecList({ experimentalRunAllSpecs: true })
const mutationStub = cy.stub().as('mutationStub')
cy.stubMutationResolver(RunAllSpecsDocument, () => {
mutationStub()
})
cy.findAllByTestId('spec-file-item').should('have.length', 4)
cy.findAllByTestId('run-all-specs-button').eq(0)
.click()
.type(' ')
// make sure typing didn't change displayed items
cy.findAllByTestId('spec-file-item').should('have.length', 4)
cy.findAllByTestId('run-all-specs-button').eq(1)
.focus()
.type('{enter}')
// make sure typing didn't change displayed items
cy.findAllByTestId('spec-file-item').should('have.length', 4)
cy.get('@mutationStub').should('have.been.calledThrice')
})
})
})