/
mount.ts
355 lines (307 loc) · 9.83 KB
/
mount.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import 'zone.js'
/**
* @hack fixes "Mocha has already been patched with Zone" error.
*/
// @ts-ignore
window.Mocha['__zone_patch__'] = false
import 'zone.js/testing'
import { CommonModule } from '@angular/common'
import { Component, ErrorHandler, EventEmitter, Injectable, SimpleChange, SimpleChanges, Type } from '@angular/core'
import {
ComponentFixture,
getTestBed,
TestModuleMetadata,
TestBed,
} from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'
import {
setupHooks,
} from '@cypress/mount-utils'
/**
* Additional module configurations needed while mounting the component, like
* providers, declarations, imports and even component @Inputs()
*
*
* @interface MountConfig
* @see https://angular.io/api/core/testing/TestModuleMetadata
*/
export interface MountConfig<T> extends TestModuleMetadata {
/**
* @memberof MountConfig
* @description flag to automatically create a cy.spy() for every component @Output() property
* @example
* export class ButtonComponent {
* @Output clicked = new EventEmitter()
* }
*
* cy.mount(ButtonComponent, { autoSpyOutputs: true })
* cy.get('@clickedSpy).should('have.been.called')
*/
autoSpyOutputs?: boolean
/**
* @memberof MountConfig
* @description flag defaulted to true to automatically detect changes in your components
*/
autoDetectChanges?: boolean
/**
* @memberof MountConfig
* @example
* import { ButtonComponent } from 'button/button.component'
* it('renders a button with Save text', () => {
* cy.mount(ButtonComponent, { componentProperties: { text: 'Save' }})
* cy.get('button').contains('Save')
* })
*
* it('renders a button with a cy.spy() replacing EventEmitter', () => {
* cy.mount(ButtonComponent, {
* componentProperties: {
* clicked: cy.spy().as('mySpy)
* }
* })
* cy.get('button').click()
* cy.get('@mySpy').should('have.been.called')
* })
*/
componentProperties?: Partial<{ [P in keyof T]: T[P] }>
}
/**
* Type that the `mount` function returns
* @type MountResponse<T>
*/
export type MountResponse<T> = {
/**
* Fixture for debugging and testing a component.
*
* @memberof MountResponse
* @see https://angular.io/api/core/testing/ComponentFixture
*/
fixture: ComponentFixture<T>
/**
* The instance of the root component class
*
* @memberof MountResponse
* @see https://angular.io/api/core/testing/ComponentFixture#componentInstance
*/
component: T
};
// 'zone.js/testing' is not properly aliasing `it.skip` but it does provide `xit`/`xspecify`
// Written up under https://github.com/angular/angular/issues/46297 but is not seeing movement
// so we'll patch here pending a fix in that library
globalThis.it.skip = globalThis.xit
@Injectable()
class CypressAngularErrorHandler implements ErrorHandler {
handleError (error: Error): void {
throw error
}
}
/**
* Bootstraps the TestModuleMetaData passed to the TestBed
*
* @param {Type<T>} component Angular component being mounted
* @param {MountConfig} config TestBed configuration passed into the mount function
* @returns {MountConfig} MountConfig
*/
function bootstrapModule<T> (
component: Type<T>,
config: MountConfig<T>,
): MountConfig<T> {
const { componentProperties, ...testModuleMetaData } = config
if (!testModuleMetaData.declarations) {
testModuleMetaData.declarations = []
}
if (!testModuleMetaData.imports) {
testModuleMetaData.imports = []
}
if (!testModuleMetaData.providers) {
testModuleMetaData.providers = []
}
// Replace default error handler since it will swallow uncaught exceptions.
// We want these to be uncaught so Cypress catches it and fails the test
testModuleMetaData.providers.push({
provide: ErrorHandler,
useClass: CypressAngularErrorHandler,
})
// check if the component is a standalone component
if ((component as any).ɵcmp.standalone) {
testModuleMetaData.imports.push(component)
} else {
testModuleMetaData.declarations.push(component)
}
if (!testModuleMetaData.imports.includes(CommonModule)) {
testModuleMetaData.imports.push(CommonModule)
}
return testModuleMetaData
}
/**
* Initializes the TestBed
*
* @param {Type<T> | string} component Angular component being mounted or its template
* @param {MountConfig} config TestBed configuration passed into the mount function
* @returns {Type<T>} componentFixture
*/
function initTestBed<T> (
component: Type<T> | string,
config: MountConfig<T>,
): Type<T> {
const { providers, ...configRest } = config
const componentFixture = createComponentFixture(component) as Type<T>
getTestBed().configureTestingModule({
...bootstrapModule(componentFixture, configRest),
})
if (providers != null) {
getTestBed().overrideComponent(componentFixture, {
add: {
providers,
},
})
}
return componentFixture
}
@Component({ selector: 'cy-wrapper-component', template: '' })
class WrapperComponent { }
/**
* Returns the Component if Type<T> or creates a WrapperComponent
*
* @param {Type<T> | string} component The component you want to create a fixture of
* @returns {Type<T> | WrapperComponent}
*/
function createComponentFixture<T> (
component: Type<T> | string,
): Type<T | WrapperComponent> {
if (typeof component === 'string') {
// getTestBed().overrideTemplate is available in v14+
// The static TestBed.overrideTemplate is available across versions
TestBed.overrideTemplate(WrapperComponent, component)
return WrapperComponent
}
return component
}
/**
* Creates the ComponentFixture
*
* @param {Type<T>} component Angular component being mounted
* @param {MountConfig<T>} config MountConfig
* @returns {ComponentFixture<T>} ComponentFixture
*/
function setupFixture<T> (
component: Type<T>,
config: MountConfig<T>,
): ComponentFixture<T> {
const fixture = getTestBed().createComponent(component)
fixture.whenStable().then(() => {
fixture.autoDetectChanges(config.autoDetectChanges ?? true)
})
return fixture
}
/**
* Gets the componentInstance and Object.assigns any componentProperties() passed in the MountConfig
*
* @param {MountConfig} config TestBed configuration passed into the mount function
* @param {ComponentFixture<T>} fixture Fixture for debugging and testing a component.
* @returns {T} Component being mounted
*/
function setupComponent<T extends { ngOnChanges? (changes: SimpleChanges): void }> (
config: MountConfig<T>,
fixture: ComponentFixture<T>): T {
let component: T = fixture.componentInstance
if (config?.componentProperties) {
component = Object.assign(component, config.componentProperties)
}
if (config.autoSpyOutputs) {
Object.keys(component).forEach((key: string, index: number, keys: string[]) => {
const property = component[key]
if (property instanceof EventEmitter) {
component[key] = createOutputSpy(`${key}Spy`)
}
})
}
// Manually call ngOnChanges when mounting components using the class syntax.
// This is necessary because we are assigning input values to the class directly
// on mount and therefore the ngOnChanges() lifecycle is not triggered.
if (component.ngOnChanges && config.componentProperties) {
const { componentProperties } = config
const simpleChanges: SimpleChanges = Object.entries(componentProperties).reduce((acc, [key, value]) => {
acc[key] = new SimpleChange(null, value, true)
return acc
}, {})
if (Object.keys(componentProperties).length > 0) {
component.ngOnChanges(simpleChanges)
}
}
return component
}
/**
* Mounts an Angular component inside Cypress browser
*
* @param {Type<T> | string} component Angular component being mounted or its template
* @param {MountConfig<T>} config configuration used to configure the TestBed
* @example
* import { HelloWorldComponent } from 'hello-world/hello-world.component'
* import { MyService } from 'services/my.service'
* import { SharedModule } from 'shared/shared.module';
* import { mount } from '@cypress/angular'
* it('can mount', () => {
* mount(HelloWorldComponent, {
* providers: [MyService],
* imports: [SharedModule]
* })
* cy.get('h1').contains('Hello World')
* })
*
* or
*
* it('can mount with template', () => {
* mount('<app-hello-world></app-hello-world>', {
* declarations: [HelloWorldComponent],
* providers: [MyService],
* imports: [SharedModule]
* })
* })
* @returns Cypress.Chainable<MountResponse<T>>
*/
export function mount<T> (
component: Type<T> | string,
config: MountConfig<T> = { },
): Cypress.Chainable<MountResponse<T>> {
const componentFixture = initTestBed(component, config)
const fixture = setupFixture(componentFixture, config)
const componentInstance = setupComponent(config, fixture)
const mountResponse: MountResponse<T> = {
fixture,
component: componentInstance,
}
const logMessage = typeof component === 'string' ? 'Component' : componentFixture.name
Cypress.log({
name: 'mount',
message: logMessage,
consoleProps: () => ({ result: mountResponse }),
})
return cy.wrap(mountResponse, { log: false })
}
/**
* Creates a new Event Emitter and then spies on it's `emit` method
*
* @param {string} alias name you want to use for your cy.spy() alias
* @returns EventEmitter<T>
*/
export const createOutputSpy = <T>(alias: string) => {
const emitter = new EventEmitter<T>()
cy.spy(emitter, 'emit').as(alias)
return emitter as any
}
// Only needs to run once, we reset before each test
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{
teardown: { destroyAfterEach: false },
},
)
setupHooks(() => {
// Not public, we need to call this to remove the last component from the DOM
getTestBed()['tearDownTestingModule']()
getTestBed().resetTestingModule()
})