-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
RenderNgAppService.test.ts
240 lines (203 loc) · 8.01 KB
/
RenderNgAppService.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
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
import { Component, EventEmitter, Input, NgModule, Output, Type } from '@angular/core';
import { platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { TestBed } from '@angular/core/testing';
import { BehaviorSubject } from 'rxjs';
import { StoryFnAngularReturnType } from '../types';
import { RenderNgAppService } from './RenderNgAppService';
jest.mock('@angular/platform-browser-dynamic');
declare const document: Document;
describe('RenderNgAppService', () => {
let renderNgAppService: RenderNgAppService;
beforeEach(async () => {
document.body.innerHTML = '<div id="root"></div>';
(platformBrowserDynamic as any).mockImplementation(platformBrowserDynamicTesting);
renderNgAppService = new RenderNgAppService();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should initialize', () => {
expect(renderNgAppService).toBeDefined();
});
describe('render', () => {
it('should add storybook-wrapper for story template', async () => {
await renderNgAppService.render(
(): StoryFnAngularReturnType => ({
template: '🦊',
props: {},
}),
false
);
expect(document.body.getElementsByTagName('storybook-wrapper')[0].innerHTML).toBe('🦊');
});
it('should add storybook-wrapper for story component', async () => {
@Component({ selector: 'foo', template: '🦊' })
class FooComponent {}
await renderNgAppService.render(
(): StoryFnAngularReturnType => ({
component: FooComponent,
props: {},
}),
false
);
expect(document.body.getElementsByTagName('storybook-wrapper')[0].innerHTML).toBe(
'<foo>🦊</foo>'
);
});
});
describe('getNgModuleMetadata', () => {
describe('with simple component', () => {
@Component({
selector: 'foo',
template: `
<p id="input">{{ input }}</p>
<p id="inputBindingPropertyName">{{ localPropertyName }}</p>
<p id="localProperty">{{ localProperty }}</p>
<p id="localFunction">{{ localFunction() }}</p>
<p id="output" (click)="output.emit('outputEmitted')"></p>
<p id="outputBindingPropertyName" (click)="localOutput.emit('outputEmitted')"></p>
`,
})
class FooComponent {
@Input()
public input: string;
@Input('inputBindingPropertyName')
public localPropertyName: string;
@Output()
public output = new EventEmitter<string>();
@Output('outputBindingPropertyName')
public localOutput = new EventEmitter<string>();
public localProperty: string;
public localFunction = () => '';
}
it('should initialize inputs', async () => {
const props = {
input: 'input',
inputBindingPropertyName: 'inputBindingPropertyName',
localProperty: 'localProperty',
localFunction: () => 'localFunction',
};
const ngModule = renderNgAppService.getNgModuleMetadata(
{ component: FooComponent, props },
new BehaviorSubject(props)
);
const { fixture } = await configureTestingModule(ngModule);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('p#input').innerHTML).toEqual(props.input);
expect(fixture.nativeElement.querySelector('p#inputBindingPropertyName').innerHTML).toEqual(
props.inputBindingPropertyName
);
expect(fixture.nativeElement.querySelector('p#localProperty').innerHTML).toEqual(
props.localProperty
);
expect(fixture.nativeElement.querySelector('p#localFunction').innerHTML).toEqual(
props.localFunction()
);
});
it('should initialize outputs', async () => {
let expectedOutputValue: string;
let expectedOutputBindingValue: string;
const props = {
output: (value: string) => {
expectedOutputValue = value;
},
outputBindingPropertyName: (value: string) => {
expectedOutputBindingValue = value;
},
};
const ngModule = renderNgAppService.getNgModuleMetadata(
{ component: FooComponent, props },
new BehaviorSubject(props)
);
const { fixture } = await configureTestingModule(ngModule);
fixture.detectChanges();
fixture.nativeElement.querySelector('p#output').click();
fixture.nativeElement.querySelector('p#outputBindingPropertyName').click();
expect(expectedOutputValue).toEqual('outputEmitted');
expect(expectedOutputBindingValue).toEqual('outputEmitted');
});
it('should change inputs if storyProps$ Subject emit', async () => {
const initialProps = {
input: 'input',
};
const storyProps$ = new BehaviorSubject(initialProps);
const ngModule = renderNgAppService.getNgModuleMetadata(
{ component: FooComponent, props: initialProps },
storyProps$
);
const { fixture } = await configureTestingModule(ngModule);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('p#input').innerHTML).toEqual(
initialProps.input
);
expect(fixture.nativeElement.querySelector('p#inputBindingPropertyName').innerHTML).toEqual(
''
);
const newProps = {
input: 'new input',
inputBindingPropertyName: 'new inputBindingPropertyName',
localProperty: 'new localProperty',
localFunction: () => 'new localFunction',
};
storyProps$.next(newProps);
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('p#input').innerHTML).toEqual(newProps.input);
expect(fixture.nativeElement.querySelector('p#inputBindingPropertyName').innerHTML).toEqual(
newProps.inputBindingPropertyName
);
expect(fixture.nativeElement.querySelector('p#localProperty').innerHTML).toEqual(
newProps.localProperty
);
expect(fixture.nativeElement.querySelector('p#localFunction').innerHTML).toEqual(
newProps.localFunction()
);
});
it('should not override outputs if storyProps$ Subject emit', async () => {
let expectedOutputValue;
let expectedOutputBindingValue;
const initialProps = {
output: (value: string) => {
expectedOutputValue = value;
},
outputBindingPropertyName: (value: string) => {
expectedOutputBindingValue = value;
},
};
const storyProps$ = new BehaviorSubject(initialProps);
const ngModule = renderNgAppService.getNgModuleMetadata(
{ component: FooComponent, props: initialProps },
storyProps$
);
const { fixture } = await configureTestingModule(ngModule);
fixture.detectChanges();
const newProps = {
input: 'new input',
output: () => {
expectedOutputValue = 'should not be called';
},
outputBindingPropertyName: () => {
expectedOutputBindingValue = 'should not be called';
},
};
storyProps$.next(newProps);
fixture.detectChanges();
fixture.nativeElement.querySelector('p#output').click();
fixture.nativeElement.querySelector('p#outputBindingPropertyName').click();
expect(fixture.nativeElement.querySelector('p#input').innerHTML).toEqual(newProps.input);
expect(expectedOutputValue).toEqual('outputEmitted');
expect(expectedOutputBindingValue).toEqual('outputEmitted');
});
});
});
async function configureTestingModule(ngModule: NgModule) {
await TestBed.configureTestingModule({
declarations: ngModule.declarations,
providers: ngModule.providers,
}).compileComponents();
const fixture = TestBed.createComponent(ngModule.bootstrap[0] as Type<unknown>);
return {
fixture,
};
}
});