/
create-from-component.cy.ts
256 lines (195 loc) · 8.47 KB
/
create-from-component.cy.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
import defaultMessages from '@packages/frontend-shared/src/locales/en-US.json'
import { getPathForPlatform } from '../../src/paths'
function validateCreateFromVueComponentCard (beforeEachFn: () => void, expectedSpecPath: string) {
beforeEach(beforeEachFn)
it('Shows create from component card for Vue projects', () => {
cy.get('@ComponentCard')
.within(() => {
cy.findByRole('button', {
name: 'Create from component',
}).should('be.visible')
.and('not.be.disabled')
})
})
it('Can be closed with the x button', () => {
cy.get('@ComponentCard').click()
cy.findByRole('button', { name: 'Close' }).as('DialogCloseButton')
cy.get('@DialogCloseButton').click()
cy.findByRole('dialog', {
name: 'Choose a component',
}).should('not.exist')
})
it('Lists Vue components in the project', () => {
cy.get('@ComponentCard').click()
cy.findByText('2 matches').should('be.visible')
cy.findByText('App').should('be.visible')
cy.findByText('HelloWorld').should('be.visible')
})
it('Allows for the user to search through their components', () => {
cy.get('@ComponentCard').click()
cy.findByText('*.vue').should('be.visible')
cy.findByText('2 matches').should('be.visible')
cy.findByLabelText('file-name-input').type('HelloWorld')
cy.findByText('HelloWorld').should('be.visible')
cy.findByText('1 of 2 matches').should('be.visible')
cy.findByText('App').should('not.exist')
})
it('shows success modal when component spec is created', () => {
cy.get('@ComponentCard').click()
cy.findByText('HelloWorld').should('be.visible').click()
cy.findByRole('dialog', {
name: defaultMessages.createSpec.successPage.header,
}).as('SuccessDialog').within(() => {
cy.contains(getPathForPlatform(expectedSpecPath)).should('be.visible')
cy.findByRole('button', { name: 'Close' }).should('be.visible')
cy.findByRole('link', { name: 'Okay, run the spec' })
.should('have.attr', 'href', `#/specs/runner?file=${expectedSpecPath}`)
cy.findByRole('button', { name: 'Create another spec' }).click()
})
// 'Create from component' card appears again when the user selects "create another spec"
cy.findByText('Create from component').should('be.visible')
})
it('runs generated spec', () => {
cy.get('@ComponentCard').click()
cy.findByText('HelloWorld').should('be.visible').click()
cy.findByRole('dialog', {
name: defaultMessages.createSpec.successPage.header,
}).as('SuccessDialog').within(() => {
cy.contains(getPathForPlatform(expectedSpecPath)).should('be.visible')
cy.findByRole('button', { name: 'Close' }).should('be.visible')
cy.findByRole('link', { name: 'Okay, run the spec' })
.should('have.attr', 'href', `#/specs/runner?file=${expectedSpecPath}`).click()
})
cy.waitForSpecToFinish({ passCount: 1 })
})
}
function validateCreateFromReactComponentCard (beforeEachFn: () => void, expectedSpecPath: string) {
beforeEach(beforeEachFn)
it('Shows create from component card for React projects', () => {
cy.get('@ComponentCard')
.within(() => {
cy.findByRole('button', {
name: 'Create from component',
}).should('be.visible')
.and('not.be.disabled')
})
})
it('Can be closed with the x button', () => {
cy.get('@ComponentCard').click()
cy.findByRole('button', { name: 'Close' }).as('DialogCloseButton')
cy.get('@DialogCloseButton').click()
cy.findByRole('dialog', {
name: 'Choose a component',
}).should('not.exist')
})
it('Lists files in the project', () => {
cy.get('@ComponentCard').click()
cy.findByText('5 matches').should('be.visible')
cy.findByText('App').should('be.visible')
cy.findByText('index').should('be.visible')
})
it('Allows for the user to search through their components', () => {
cy.get('@ComponentCard').click()
cy.findByText('*.{js,jsx,tsx}').should('be.visible')
cy.findByText('5 matches').should('be.visible')
cy.findByLabelText('file-name-input').type('App')
cy.findByText('App').should('be.visible')
cy.findByText('1 of 5 matches').should('be.visible')
cy.findByText('index').should('not.exist')
cy.findByText('component').should('not.exist')
})
it('shows \'No components found\' if there are no exported components', () => {
cy.get('@ComponentCard').click()
cy.findByText('index').should('be.visible').click()
cy.findByTestId('react-component-row').should('not.exist')
cy.contains('No components found').should('be.visible')
})
it('shows \'Unable to parse file\' if there was an error parsing the file', () => {
cy.get('@ComponentCard').click()
// This component has a syntax error so we will fail to parse it
cy.findByText('Invalid').should('be.visible').click()
cy.findByTestId('react-component-row').should('not.exist')
cy.contains('Unable to parse file').should('be.visible')
})
it('shows success modal when component spec is created', () => {
cy.get('@ComponentCard').click()
// Expand the row
cy.findByText('App').should('be.visible').click()
// Click on 'app' component
cy.findByTestId('react-component-row').should('contain', 'App').click()
cy.findByRole('dialog', {
name: defaultMessages.createSpec.successPage.header,
}).as('SuccessDialog').within(() => {
cy.contains(getPathForPlatform(expectedSpecPath)).should('be.visible')
cy.findByRole('button', { name: 'Close' }).should('be.visible')
cy.findByRole('link', { name: 'Okay, run the spec' })
.should('have.attr', 'href', `#/specs/runner?file=${expectedSpecPath}`)
cy.findByRole('button', { name: 'Create another spec' }).click()
})
// 'Create from component' card appears again when the user selects "create another spec"
cy.findByText('Create from component').should('be.visible')
})
it('runs generated spec', () => {
cy.get('@ComponentCard').click()
// Expand the row
cy.findByText('App').should('be.visible').click()
// Click on 'app' component
cy.findByTestId('react-component-row').should('contain', 'App').click()
cy.findByRole('dialog', {
name: defaultMessages.createSpec.successPage.header,
}).as('SuccessDialog').within(() => {
cy.contains(getPathForPlatform(expectedSpecPath)).should('be.visible')
cy.findByRole('button', { name: 'Close' }).should('be.visible')
// There appears to be a race condition here where sometimes we try to run the spec
// before the file has been written to. Waiting here for 1 second resolves the issue.
cy.wait(2000)
cy.findByRole('link', { name: 'Okay, run the spec' })
.should('have.attr', 'href', `#/specs/runner?file=${expectedSpecPath}`).click()
})
cy.waitForSpecToFinish({ passCount: 1 })
})
}
describe('Create from component card', () => {
context('Vue', () => {
context('project with default spec pattern', () => {
validateCreateFromVueComponentCard(() => {
cy.scaffoldProject('no-specs-vue-2')
cy.openProject('no-specs-vue-2')
cy.startAppServer('component')
cy.visitApp()
cy.findAllByTestId('card').eq(0).as('ComponentCard')
}, 'src/components/HelloWorld.cy.js')
})
context('project with custom spec pattern', () => {
validateCreateFromVueComponentCard(() => {
cy.scaffoldProject('no-specs-vue-2')
cy.openProject('no-specs-vue-2', ['--config-file', 'cypress-custom-spec-pattern.config.js'])
cy.startAppServer('component')
cy.visitApp()
cy.findByText('New spec').click()
cy.findAllByTestId('card').eq(0).as('ComponentCard')
}, 'src/specs-folder/HelloWorld.cy.js')
})
})
context('React', () => {
context('project with default spec pattern', () => {
validateCreateFromReactComponentCard(() => {
cy.scaffoldProject('no-specs')
cy.openProject('no-specs')
cy.startAppServer('component')
cy.visitApp()
cy.findAllByTestId('card').eq(0).as('ComponentCard')
}, 'src/App.cy.jsx')
})
context('project with custom spec pattern', () => {
validateCreateFromReactComponentCard(() => {
cy.scaffoldProject('no-specs')
cy.openProject('no-specs', ['--config-file', 'cypress-custom-spec-pattern.config.ts'])
cy.startAppServer('component')
cy.visitApp()
cy.findByText('New spec').click()
cy.findAllByTestId('card').eq(0).as('ComponentCard')
}, 'src/specs-folder/App.cy.jsx')
})
})
})