/
component-test.ts
53 lines (50 loc) · 1.47 KB
/
component-test.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
import { assertMinimumCypressVersion } from '@nrwl/cypress/src/utils/cypress-version';
import {
generateFiles,
joinPathFragments,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import {
getArgsDefaultValue,
getComponentProps,
} from '../utils/storybook-ast/storybook-inputs';
import { ComponentTestSchema } from './schema';
export function componentTestGenerator(
tree: Tree,
options: ComponentTestSchema
) {
assertMinimumCypressVersion(10);
const { root } = readProjectConfiguration(tree, options.project);
const componentDirPath = joinPathFragments(root, options.componentDir);
const componentFilePath = joinPathFragments(
componentDirPath,
`${options.componentFileName}.ts`
);
const componentTestFilePath = joinPathFragments(
componentDirPath,
`${options.componentFileName}.cy.ts`
);
if (tree.exists(componentFilePath) && !tree.exists(componentTestFilePath)) {
const props = getComponentProps(
tree,
componentFilePath,
getArgsDefaultValue,
false
);
generateFiles(
tree,
joinPathFragments(__dirname, 'files'),
componentDirPath,
{
componentName: options.componentName,
componentFileName: options.componentFileName.startsWith('./')
? options.componentFileName.slice(2)
: options.componentFileName,
props: props.filter((p) => typeof p.defaultValue !== 'undefined'),
tpl: '',
}
);
}
}
export default componentTestGenerator;