-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
migrate-stories-to-6-2.spec.ts
162 lines (145 loc) · 5.29 KB
/
migrate-stories-to-6-2.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
import {
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { joinPathFragments, writeJson } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { storybookVersion } from '@nrwl/storybook';
import { findNodes } from '@nrwl/workspace/src/utils/ast-utils';
import * as ts from 'typescript';
import { SyntaxKind } from 'typescript';
import { nxVersion } from '../../../utils/versions';
import { migrateStoriesTo62Generator } from './migrate-stories-to-6-2';
import {
overrideCollectionResolutionForTesting,
wrapAngularDevkitSchematic,
} from '@nrwl/devkit/ngcli-adapter';
import { getTsSourceFile } from '@nrwl/storybook/src/utils/utilities';
const componentSchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'component'
);
const runAngularLibrarySchematic = wrapAngularDevkitSchematic(
'@schematics/angular',
'library'
);
const runAngularStorybookSchematic = wrapAngularDevkitSchematic(
'@nrwl/angular',
'storybook-configuration'
);
describe('migrate-stories-to-6-2 schematic', () => {
let appTree: Tree;
describe('angular project', () => {
beforeEach(async () => {
overrideCollectionResolutionForTesting({
'@nrwl/storybook': joinPathFragments(
__dirname,
'../../../../generators.json'
),
});
appTree = createTreeWithEmptyWorkspace();
await runAngularLibrarySchematic(appTree, {
name: 'test-ui-lib',
});
await componentSchematic(appTree, {
name: 'test-button',
project: 'test-ui-lib',
});
writeJson(appTree, 'package.json', {
devDependencies: {
'@nrwl/storybook': nxVersion,
'@storybook/addon-knobs': storybookVersion,
'@storybook/angular': storybookVersion,
},
});
writeJson(appTree, 'test-ui-lib/tsconfig.json', {});
await runAngularStorybookSchematic(appTree, {
name: 'test-ui-lib',
configureCypress: true,
});
appTree.write(
`test-ui-lib/src/lib/test-button/test-button.component.stories.ts`,
`
import { text, number, boolean } from '@storybook/addon-knobs';
import { TestButtonComponent } from './test-button.component';
export default {
title: 'TestButtonComponent',
};
export const primary = () => ({
component: TestButtonComponent,
moduleMetadata: {
imports: [],
},
props: {
buttonType: text('buttonType', 'button'),
style: text('style', 'default'),
age: number('age', 0),
isOn: boolean('isOn', false),
},
});
export const secondary = () => ({
component: TestButtonComponent,
moduleMetadata: {
imports: [],
},
props: {},
});
`
);
/**
* This needs to be updated for the following reason:
* - runAngularStorybookSchematic now generates the Storybook targets in
* the project configuration using the @storybook/angular executors
* - this means that the uiFramework property is not used any more
* - that property was used in versions before that so the migration script looks for it
* - the migrate-stories-to-6-2 migrator should have already taken effect in previous versions
* so there is no need to update the generator to look for the new executor as well
*/
const projectConfig = readProjectConfiguration(appTree, 'test-ui-lib');
projectConfig.targets.storybook.options.uiFramework =
'@storybook/angular';
projectConfig.targets.storybook.options.config = {
configFolder: projectConfig.targets.storybook.options.configDir,
};
updateProjectConfiguration(appTree, 'test-ui-lib', projectConfig);
});
it('should move the component from the story to parameters.component', async () => {
await migrateStoriesTo62Generator(appTree);
const storyFilePath =
'test-ui-lib/src/lib/test-button/test-button.component.stories.ts';
const file = getTsSourceFile(appTree, storyFilePath);
const storiesExportDefault = findNodes(file, [
ts.SyntaxKind.ExportAssignment,
]);
const defaultExportNode = storiesExportDefault[0];
const defaultExportObject = defaultExportNode
?.getChildren()
?.find((node) => {
return node.kind === SyntaxKind.ObjectLiteralExpression;
});
const defaultPropertiesList = defaultExportObject
?.getChildren()
?.find((node) => {
return node.kind === SyntaxKind.SyntaxList;
});
const hasTitle = defaultPropertiesList?.getChildren()?.find((node) => {
return (
node.kind === SyntaxKind.PropertyAssignment &&
node.getText().startsWith('title')
);
});
const hasComponent = defaultPropertiesList
?.getChildren()
?.find((node) => {
return (
node.kind === SyntaxKind.PropertyAssignment &&
node.getText().startsWith('component')
);
});
expect(appTree.exists(storyFilePath)).toBeTruthy();
expect(hasTitle).toBeTruthy();
expect(hasComponent).toBeTruthy();
});
});
});