UNPKG

14.4 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const core_1 = require("@angular/core");
16const vitest_1 = require("vitest");
17const decorators_1 = require("./decorators");
18const decorateStory_1 = __importDefault(require("./decorateStory"));
19(0, vitest_1.describe)('decorateStory', () => {
20 (0, vitest_1.describe)('angular behavior', () => {
21 (0, vitest_1.it)('should use componentWrapperDecorator with args', () => {
22 const decorators = [
23 (0, decorators_1.componentWrapperDecorator)(ParentComponent, ({ args }) => args),
24 (0, decorators_1.componentWrapperDecorator)((story) => `<grandparent [grandparentInput]="grandparentInput">${story}</grandparent>`, ({ args }) => args),
25 (0, decorators_1.componentWrapperDecorator)((story) => `<great-grandparent>${story}</great-grandparent>`),
26 ];
27 const decorated = (0, decorateStory_1.default)(() => ({ template: '</child>' }), decorators);
28 (0, vitest_1.expect)(decorated(makeContext({
29 component: FooComponent,
30 args: {
31 parentInput: 'Parent input',
32 grandparentInput: 'grandparent input',
33 parentOutput: () => { },
34 },
35 }))).toEqual({
36 props: {
37 parentInput: 'Parent input',
38 grandparentInput: 'grandparent input',
39 parentOutput: vitest_1.expect.any(Function),
40 },
41 template: '<great-grandparent><grandparent [grandparentInput]="grandparentInput"><parent [parentInput]="parentInput" (parentOutput)="parentOutput($event)"></child></parent></grandparent></great-grandparent>',
42 userDefinedTemplate: true,
43 });
44 });
45 (0, vitest_1.it)('should use componentWrapperDecorator with input / output', () => {
46 const decorators = [
47 (0, decorators_1.componentWrapperDecorator)(ParentComponent, {
48 parentInput: 'Parent input',
49 parentOutput: () => { },
50 }),
51 (0, decorators_1.componentWrapperDecorator)((story) => `<grandparent [grandparentInput]="grandparentInput">${story}</grandparent>`, {
52 grandparentInput: 'Grandparent input',
53 sameInput: 'Should be override by story props',
54 }),
55 (0, decorators_1.componentWrapperDecorator)((story) => `<great-grandparent>${story}</great-grandparent>`),
56 ];
57 const decorated = (0, decorateStory_1.default)(() => ({ template: '</child>', props: { sameInput: 'Story input' } }), decorators);
58 (0, vitest_1.expect)(decorated(makeContext({
59 component: FooComponent,
60 }))).toEqual({
61 props: {
62 parentInput: 'Parent input',
63 parentOutput: vitest_1.expect.any(Function),
64 grandparentInput: 'Grandparent input',
65 sameInput: 'Story input',
66 },
67 template: '<great-grandparent><grandparent [grandparentInput]="grandparentInput"><parent [parentInput]="parentInput" (parentOutput)="parentOutput($event)"></child></parent></grandparent></great-grandparent>',
68 userDefinedTemplate: true,
69 });
70 });
71 (0, vitest_1.it)('should use componentWrapperDecorator', () => {
72 const decorators = [
73 (0, decorators_1.componentWrapperDecorator)(ParentComponent),
74 (0, decorators_1.componentWrapperDecorator)((story) => `<grandparent>${story}</grandparent>`),
75 (0, decorators_1.componentWrapperDecorator)((story) => `<great-grandparent>${story}</great-grandparent>`),
76 ];
77 const decorated = (0, decorateStory_1.default)(() => ({ template: '</child>' }), decorators);
78 (0, vitest_1.expect)(decorated(makeContext({ component: FooComponent }))).toEqual({
79 template: '<great-grandparent><grandparent><parent></child></parent></grandparent></great-grandparent>',
80 userDefinedTemplate: true,
81 });
82 });
83 (0, vitest_1.it)('should use template in preference to component parameters', () => {
84 const decorators = [
85 (s) => {
86 const story = s();
87 return {
88 ...story,
89 template: `<parent>${story.template}</parent>`,
90 };
91 },
92 (s) => {
93 const story = s();
94 return {
95 ...story,
96 template: `<grandparent>${story.template}</grandparent>`,
97 };
98 },
99 (s) => {
100 const story = s();
101 return {
102 ...story,
103 template: `<great-grandparent>${story.template}</great-grandparent>`,
104 };
105 },
106 ];
107 const decorated = (0, decorateStory_1.default)(() => ({ template: '</child>' }), decorators);
108 (0, vitest_1.expect)(decorated(makeContext({ component: FooComponent }))).toEqual({
109 template: '<great-grandparent><grandparent><parent></child></parent></grandparent></great-grandparent>',
110 userDefinedTemplate: true,
111 });
112 });
113 (0, vitest_1.it)('should include story templates in decorators', () => {
114 const decorators = [
115 (s) => {
116 const story = s();
117 return {
118 ...story,
119 template: `<parent>${story.template}</parent>`,
120 };
121 },
122 (s) => {
123 const story = s();
124 return {
125 ...story,
126 template: `<grandparent>${story.template}</grandparent>`,
127 };
128 },
129 (s) => {
130 const story = s();
131 return {
132 ...story,
133 template: `<great-grandparent>${story.template}</great-grandparent>`,
134 };
135 },
136 ];
137 const decorated = (0, decorateStory_1.default)(() => ({ template: '</child>' }), decorators);
138 (0, vitest_1.expect)(decorated(makeContext({}))).toEqual({
139 template: '<great-grandparent><grandparent><parent></child></parent></grandparent></great-grandparent>',
140 userDefinedTemplate: true,
141 });
142 });
143 (0, vitest_1.it)('should include story components in decorators', () => {
144 const decorators = [
145 (s) => {
146 const story = s();
147 return {
148 ...story,
149 template: `<parent>${story.template}</parent>`,
150 };
151 },
152 (s) => {
153 const story = s();
154 return {
155 ...story,
156 template: `<grandparent>${story.template}</grandparent>`,
157 };
158 },
159 (s) => {
160 const story = s();
161 return {
162 ...story,
163 template: `<great-grandparent>${story.template}</great-grandparent>`,
164 };
165 },
166 ];
167 const decorated = (0, decorateStory_1.default)(() => ({}), decorators);
168 (0, vitest_1.expect)(decorated(makeContext({ component: FooComponent }))).toEqual({
169 template: '<great-grandparent><grandparent><parent><foo></foo></parent></grandparent></great-grandparent>',
170 userDefinedTemplate: false,
171 });
172 });
173 (0, vitest_1.it)('should keep template with an empty value', () => {
174 const decorators = [
175 (0, decorators_1.componentWrapperDecorator)(ParentComponent),
176 ];
177 const decorated = (0, decorateStory_1.default)(() => ({ template: '' }), decorators);
178 (0, vitest_1.expect)(decorated(makeContext({ component: FooComponent }))).toEqual({
179 template: '<parent></parent>',
180 });
181 });
182 (0, vitest_1.it)('should only keeps args with a control or an action in argTypes', () => {
183 const decorated = (0, decorateStory_1.default)((context) => ({
184 template: `Args available in the story : ${Object.keys(context.args).join()}`,
185 }), []);
186 (0, vitest_1.expect)(decorated(makeContext({
187 component: FooComponent,
188 argTypes: {
189 withControl: { control: { type: 'object' }, name: 'withControl' },
190 withAction: { action: 'onClick', name: 'withAction' },
191 toRemove: { name: 'toRemove' },
192 },
193 args: {
194 withControl: 'withControl',
195 withAction: () => ({}),
196 toRemove: 'toRemove',
197 },
198 }))).toEqual({
199 template: 'Args available in the story : withControl,withAction',
200 userDefinedTemplate: true,
201 });
202 });
203 });
204 (0, vitest_1.describe)('default behavior', () => {
205 (0, vitest_1.it)('calls decorators in out to in order', () => {
206 const decorators = [
207 (s) => {
208 const story = s();
209 return { ...story, props: { a: [...story.props.a, 1] } };
210 },
211 (s) => {
212 const story = s();
213 return { ...story, props: { a: [...story.props.a, 2] } };
214 },
215 (s) => {
216 const story = s();
217 return { ...story, props: { a: [...story.props.a, 3] } };
218 },
219 ];
220 const decorated = (0, decorateStory_1.default)(() => ({ props: { a: [0] } }), decorators);
221 (0, vitest_1.expect)(decorated(makeContext({}))).toEqual({ props: { a: [0, 1, 2, 3] } });
222 });
223 (0, vitest_1.it)('passes context through to sub decorators', () => {
224 const decorators = [
225 (s, c) => {
226 const story = s({ ...c, k: 1 });
227 return { ...story, props: { a: [...story.props.a, c.k] } };
228 },
229 (s, c) => {
230 const story = s({ ...c, k: 2 });
231 return { ...story, props: { a: [...story.props.a, c.k] } };
232 },
233 (s, c) => {
234 const story = s({ ...c, k: 3 });
235 return { ...story, props: { a: [...story.props.a, c.k] } };
236 },
237 ];
238 const decorated = (0, decorateStory_1.default)((c) => ({ props: { a: [c.k] } }), decorators);
239 (0, vitest_1.expect)(decorated(makeContext({ k: 0 }))).toEqual({ props: { a: [1, 2, 3, 0] } });
240 });
241 (0, vitest_1.it)('DOES NOT merge parameter or pass through parameters key in context', () => {
242 const decorators = [
243 (s, c) => {
244 const story = s({ ...c, k: 1, parameters: { p: 1 } });
245 return {
246 ...story,
247 props: { a: [...story.props.a, c.k], p: [...story.props.p, c.parameters.p] },
248 };
249 },
250 (s, c) => {
251 const story = s({ ...c, k: 2, parameters: { p: 2 } });
252 return {
253 ...story,
254 props: { a: [...story.props.a, c.k], p: [...story.props.p, c.parameters.p] },
255 };
256 },
257 (s, c) => {
258 const story = s({ ...c, k: 3, parameters: { p: 3 } });
259 return {
260 ...story,
261 props: { a: [...story.props.a, c.k], p: [...story.props.p, c.parameters.p] },
262 };
263 },
264 ];
265 const decorated = (0, decorateStory_1.default)((c) => ({ props: { a: [c.k], p: [c.parameters.p] } }), decorators);
266 (0, vitest_1.expect)(decorated(makeContext({ k: 0, parameters: { p: 0 } }))).toEqual({
267 props: { a: [1, 2, 3, 0], p: [0, 0, 0, 0] },
268 });
269 });
270 });
271});
272function makeContext(input) {
273 return {
274 id: 'id',
275 kind: 'kind',
276 name: 'name',
277 viewMode: 'story',
278 parameters: {},
279 ...input,
280 };
281}
282let FooComponent = class FooComponent {
283};
284FooComponent = __decorate([
285 (0, core_1.Component)({
286 selector: 'foo',
287 template: `foo`,
288 })
289], FooComponent);
290let ParentComponent = class ParentComponent {
291};
292__decorate([
293 (0, core_1.Input)(),
294 __metadata("design:type", String)
295], ParentComponent.prototype, "parentInput", void 0);
296__decorate([
297 (0, core_1.Output)(),
298 __metadata("design:type", Object)
299], ParentComponent.prototype, "parentOutput", void 0);
300ParentComponent = __decorate([
301 (0, core_1.Component)({
302 selector: 'parent',
303 template: `<ng-content></ng-content>`,
304 })
305], ParentComponent);