-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
component-cypress-spec.spec.ts
209 lines (182 loc) · 5.87 KB
/
component-cypress-spec.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
import { Tree } from '@nrwl/devkit';
import componentCypressSpecGenerator from './component-cypress-spec';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import libraryGenerator from '../library/library';
import { Linter } from '@nrwl/linter';
import applicationGenerator from '../application/application';
import { formatFile } from '../../utils/format-file';
describe('react:component-cypress-spec', () => {
let appTree: Tree;
[
{
plainJS: false,
testCmpSrcWithProps: `import React from 'react';
import './test.scss';
export interface TestProps {
name: string;
displayAge: boolean;
}
export const Test = (props: TestProps) => {
return (
<div>
<h1>Welcome to test component, {props.name}</h1>
</div>
);
};
export default Test;
`,
testCmpSrcWithoutProps: `import React from 'react';
import './test.scss';
export const Test = () => {
return (
<div>
<h1>Welcome to test component</h1>
</div>
);
};
export default Test;
`,
},
{
plainJS: true,
testCmpSrcWithProps: `import React from 'react';
import './test.scss';
export const Test = (props: TestProps) => {
return (
<div>
<h1>Welcome to test component, {props.name}</h1>
</div>
);
};
export default Test;
`,
testCmpSrcWithoutProps: `import React from 'react';
import './test.scss';
export const Test = () => {
return (
<div>
<h1>Welcome to test component</h1>
</div>
);
};
export default Test;
`,
},
].forEach((testConfig) => {
let fileCmpExt = testConfig.plainJS ? 'js' : 'tsx';
let fileExt = testConfig.plainJS ? 'js' : 'ts';
describe(`using ${
testConfig.plainJS ? 'plain JS' : 'TypeScript'
} setup`, () => {
let cmpPath = `libs/test-ui-lib/src/lib/test-ui-lib.${fileCmpExt}`;
let cypressStorySpecFilePath = `apps/test-ui-lib-e2e/src/integration/test-ui-lib/test-ui-lib.spec.${fileExt}`;
if (!testConfig.plainJS) {
// hacky, but we should do this check only if we run with TypeScript,
// detecting component props in plain JS is "not possible"
describe('component with properties', () => {
beforeEach(async () => {
appTree = await createTestUILib('test-ui-lib', testConfig.plainJS);
appTree.write(cmpPath, testConfig.testCmpSrcWithProps);
await componentCypressSpecGenerator(appTree, {
componentPath: `lib/test-ui-lib.${fileCmpExt}`,
project: 'test-ui-lib',
js: testConfig.plainJS,
});
});
it('should properly set up the spec', () => {
expect(
formatFile`${appTree.read(cypressStorySpecFilePath, 'utf-8')}`
)
.toContain(formatFile`describe('test-ui-lib: Test component', () => {
beforeEach(() => cy.visit('/iframe.html?id=test--primary&args=name;displayAge:false;'));
it('should render the component', () => {
cy.get('h1').should('contain', 'Welcome to Test!');
});
})
`);
});
});
}
describe('component without properties', () => {
beforeEach(async () => {
appTree = await createTestUILib('test-ui-lib', testConfig.plainJS);
appTree.write(cmpPath, testConfig.testCmpSrcWithoutProps);
await componentCypressSpecGenerator(appTree, {
componentPath: `lib/test-ui-lib.${fileCmpExt}`,
project: 'test-ui-lib',
js: testConfig.plainJS,
});
});
it('should properly set up the spec', () => {
expect(formatFile`${appTree.read(cypressStorySpecFilePath, 'utf-8')}`)
.toContain(formatFile`describe('test-ui-lib: Test component', () => {
beforeEach(() => cy.visit('/iframe.html?id=test--primary'));
it('should render the component', () => {
cy.get('h1').should('contain', 'Welcome to Test!');
});
});
`);
});
});
});
});
it('should target the correct cypress suite', async () => {
appTree = await createTestUILib('test-ui-lib');
await applicationGenerator(appTree, {
babelJest: false,
e2eTestRunner: 'none',
linter: Linter.EsLint,
name: `other-e2e`,
skipFormat: true,
style: 'css',
unitTestRunner: 'none',
standaloneConfig: false,
});
await componentCypressSpecGenerator(appTree, {
componentPath: `lib/test-ui-lib.tsx`,
project: 'test-ui-lib',
cypressProject: 'other-e2e',
});
expect(
appTree.exists(
'apps/other-e2e/src/integration/test-ui-lib/test-ui-lib.spec.ts'
)
).toBeTruthy();
expect(
appTree.exists(
'apps/test-ui-lib/src/integration/test-ui-lib/test-ui-lib.spec.ts'
)
).toBeFalsy();
});
});
export async function createTestUILib(
libName: string,
plainJS = false
): Promise<Tree> {
let appTree = createTreeWithEmptyWorkspace();
await libraryGenerator(appTree, {
name: libName,
linter: Linter.EsLint,
js: plainJS,
component: true,
skipFormat: true,
skipTsConfig: false,
style: 'css',
unitTestRunner: 'jest',
standaloneConfig: false,
});
// create some Nx app that we'll use to generate the cypress
// spec into it. We don't need a real Cypress setup
await applicationGenerator(appTree, {
babelJest: false,
js: plainJS,
e2eTestRunner: 'none',
linter: Linter.EsLint,
name: `${libName}-e2e`,
skipFormat: true,
style: 'css',
unitTestRunner: 'none',
standaloneConfig: false,
});
return appTree;
}