UNPKG

20 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};
8Object.defineProperty(exports, "__esModule", { value: true });
9const core_1 = require("@angular/core");
10const ComputesTemplateFromComponent_1 = require("./ComputesTemplateFromComponent");
11const input_component_1 = require("./__testfixtures__/input.component");
12describe('angular source decorator', () => {
13 it('With no props should generate simple tag', () => {
14 const component = input_component_1.InputComponent;
15 const props = {};
16 const argTypes = {};
17 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
18 expect(source).toEqual('<doc-button></doc-button>');
19 });
20 describe('with component without selector', () => {
21 let WithoutSelectorComponent = class WithoutSelectorComponent {
22 };
23 WithoutSelectorComponent = __decorate([
24 (0, core_1.Component)({
25 template: `The content`,
26 })
27 ], WithoutSelectorComponent);
28 it('should add component ng-container', async () => {
29 const component = WithoutSelectorComponent;
30 const props = {};
31 const argTypes = {};
32 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
33 expect(source).toEqual(`<ng-container *ngComponentOutlet="WithoutSelectorComponent"></ng-container>`);
34 });
35 });
36 describe('with component with attribute selector', () => {
37 let WithAttributeComponent = class WithAttributeComponent {
38 };
39 WithAttributeComponent = __decorate([
40 (0, core_1.Component)({
41 selector: 'doc-button[foo]',
42 template: '<button></button>',
43 })
44 ], WithAttributeComponent);
45 it('should add attribute to template', async () => {
46 const component = WithAttributeComponent;
47 const props = {};
48 const argTypes = {};
49 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
50 expect(source).toEqual(`<doc-button foo></doc-button>`);
51 });
52 });
53 describe('with component with attribute and value selector', () => {
54 let WithAttributeValueComponent = class WithAttributeValueComponent {
55 };
56 WithAttributeValueComponent = __decorate([
57 (0, core_1.Component)({
58 selector: 'doc-button[foo="bar"]',
59 template: '<button></button>',
60 })
61 ], WithAttributeValueComponent);
62 it('should add attribute to template', async () => {
63 const component = WithAttributeValueComponent;
64 const props = {};
65 const argTypes = {};
66 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
67 expect(source).toEqual(`<doc-button foo="bar"></doc-button>`);
68 });
69 });
70 describe('with component with attribute only selector', () => {
71 let WithAttributeOnlyComponent = class WithAttributeOnlyComponent {
72 };
73 WithAttributeOnlyComponent = __decorate([
74 (0, core_1.Component)({
75 selector: '[foo]',
76 template: '<button></button>',
77 })
78 ], WithAttributeOnlyComponent);
79 it('should create a div and add attribute to template', async () => {
80 const component = WithAttributeOnlyComponent;
81 const props = {};
82 const argTypes = {};
83 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
84 expect(source).toEqual(`<div foo></div>`);
85 });
86 });
87 describe('with component with void element and attribute selector', () => {
88 let VoidElementWithAttributeComponent = class VoidElementWithAttributeComponent {
89 };
90 VoidElementWithAttributeComponent = __decorate([
91 (0, core_1.Component)({
92 selector: 'input[foo]',
93 template: '<button></button>',
94 })
95 ], VoidElementWithAttributeComponent);
96 it('should create without separate closing tag', async () => {
97 const component = VoidElementWithAttributeComponent;
98 const props = {};
99 const argTypes = {};
100 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
101 expect(source).toEqual(`<input foo />`);
102 });
103 });
104 describe('with component with attribute and value only selector', () => {
105 let WithAttributeOnlyComponent = class WithAttributeOnlyComponent {
106 };
107 WithAttributeOnlyComponent = __decorate([
108 (0, core_1.Component)({
109 selector: '[foo="bar"]',
110 template: '<button></button>',
111 })
112 ], WithAttributeOnlyComponent);
113 it('should create a div and add attribute to template', async () => {
114 const component = WithAttributeOnlyComponent;
115 const props = {};
116 const argTypes = {};
117 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
118 expect(source).toEqual(`<div foo="bar"></div>`);
119 });
120 });
121 describe('with component with void element, attribute and value only selector', () => {
122 let VoidElementWithAttributeComponent = class VoidElementWithAttributeComponent {
123 };
124 VoidElementWithAttributeComponent = __decorate([
125 (0, core_1.Component)({
126 selector: 'input[foo="bar"]',
127 template: '<button></button>',
128 })
129 ], VoidElementWithAttributeComponent);
130 it('should create and add attribute to template without separate closing tag', async () => {
131 const component = VoidElementWithAttributeComponent;
132 const props = {};
133 const argTypes = {};
134 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
135 expect(source).toEqual(`<input foo="bar" />`);
136 });
137 });
138 describe('with component with class selector', () => {
139 let WithClassComponent = class WithClassComponent {
140 };
141 WithClassComponent = __decorate([
142 (0, core_1.Component)({
143 selector: 'doc-button.foo',
144 template: '<button></button>',
145 })
146 ], WithClassComponent);
147 it('should add class to template', async () => {
148 const component = WithClassComponent;
149 const props = {};
150 const argTypes = {};
151 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
152 expect(source).toEqual(`<doc-button class="foo"></doc-button>`);
153 });
154 });
155 describe('with component with class only selector', () => {
156 let WithClassComponent = class WithClassComponent {
157 };
158 WithClassComponent = __decorate([
159 (0, core_1.Component)({
160 selector: '.foo',
161 template: '<button></button>',
162 })
163 ], WithClassComponent);
164 it('should create a div and add attribute to template', async () => {
165 const component = WithClassComponent;
166 const props = {};
167 const argTypes = {};
168 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
169 expect(source).toEqual(`<div class="foo"></div>`);
170 });
171 });
172 describe('with component with multiple selectors', () => {
173 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
174 };
175 WithMultipleSelectorsComponent = __decorate([
176 (0, core_1.Component)({
177 selector: 'doc-button, doc-button2',
178 template: '<button></button>',
179 })
180 ], WithMultipleSelectorsComponent);
181 it('should use the first selector', async () => {
182 const component = WithMultipleSelectorsComponent;
183 const props = {};
184 const argTypes = {};
185 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
186 expect(source).toEqual(`<doc-button></doc-button>`);
187 });
188 });
189 describe('with component with multiple selectors starting with attribute', () => {
190 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
191 };
192 WithMultipleSelectorsComponent = __decorate([
193 (0, core_1.Component)({
194 selector: 'doc-button[foo], doc-button2',
195 template: '<button></button>',
196 })
197 ], WithMultipleSelectorsComponent);
198 it('should use the first selector', async () => {
199 const component = WithMultipleSelectorsComponent;
200 const props = {};
201 const argTypes = {};
202 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
203 expect(source).toEqual(`<doc-button foo></doc-button>`);
204 });
205 });
206 describe('with component with multiple selectors starting with attribute and value', () => {
207 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
208 };
209 WithMultipleSelectorsComponent = __decorate([
210 (0, core_1.Component)({
211 selector: 'doc-button[foo="bar"], doc-button2',
212 template: '<button></button>',
213 })
214 ], WithMultipleSelectorsComponent);
215 it('should use the first selector', async () => {
216 const component = WithMultipleSelectorsComponent;
217 const props = {};
218 const argTypes = {};
219 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
220 expect(source).toEqual(`<doc-button foo="bar"></doc-button>`);
221 });
222 });
223 describe('with component with multiple selectors including 2 attributes and a class', () => {
224 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
225 };
226 WithMultipleSelectorsComponent = __decorate([
227 (0, core_1.Component)({
228 selector: 'doc-button, button[foo], .button[foo], button[baz]',
229 template: '<button></button>',
230 })
231 ], WithMultipleSelectorsComponent);
232 it('should use the first selector', async () => {
233 const component = WithMultipleSelectorsComponent;
234 const props = {};
235 const argTypes = {};
236 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
237 expect(source).toEqual(`<doc-button></doc-button>`);
238 });
239 });
240 describe('with component with multiple selectors with line breaks', () => {
241 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
242 };
243 WithMultipleSelectorsComponent = __decorate([
244 (0, core_1.Component)({
245 selector: `doc-button,
246 doc-button2`,
247 template: '<button></button>',
248 })
249 ], WithMultipleSelectorsComponent);
250 it('should use the first selector', async () => {
251 const component = WithMultipleSelectorsComponent;
252 const props = {};
253 const argTypes = {};
254 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
255 expect(source).toEqual(`<doc-button></doc-button>`);
256 });
257 });
258 describe('with component with multiple selectors starting with attribute only with line breaks', () => {
259 let WithMultipleSelectorsComponent = class WithMultipleSelectorsComponent {
260 };
261 WithMultipleSelectorsComponent = __decorate([
262 (0, core_1.Component)({
263 selector: `[foo],
264 doc-button2`,
265 template: '<button></button>',
266 })
267 ], WithMultipleSelectorsComponent);
268 it('should use the first selector', async () => {
269 const component = WithMultipleSelectorsComponent;
270 const props = {};
271 const argTypes = {};
272 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
273 expect(source).toEqual(`<div foo></div>`);
274 });
275 });
276 describe('no argTypes', () => {
277 it('should generate tag-only template with no props', () => {
278 const component = input_component_1.InputComponent;
279 const props = {};
280 const argTypes = {};
281 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
282 expect(source).toEqual(`<doc-button></doc-button>`);
283 });
284 it('With props should generate tag with properties', () => {
285 const component = input_component_1.InputComponent;
286 const props = {
287 isDisabled: true,
288 label: 'Hello world',
289 accent: input_component_1.ButtonAccent.High,
290 counter: 4,
291 };
292 const argTypes = {};
293 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
294 expect(source).toEqual(`<doc-button [counter]="4" [accent]="'High'" [isDisabled]="true" [label]="'Hello world'"></doc-button>`);
295 });
296 it('With props should generate tag with outputs', () => {
297 const component = input_component_1.InputComponent;
298 const props = {
299 isDisabled: true,
300 label: 'Hello world',
301 onClick: ($event) => { },
302 };
303 const argTypes = {};
304 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
305 expect(source).toEqual(`<doc-button [isDisabled]="true" [label]="'Hello world'" (onClick)="onClick($event)"></doc-button>`);
306 });
307 it('should generate correct property for overridden name for Input', () => {
308 const component = input_component_1.InputComponent;
309 const props = {
310 color: '#ffffff',
311 };
312 const argTypes = {};
313 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
314 expect(source).toEqual(`<doc-button [color]="'#ffffff'"></doc-button>`);
315 });
316 });
317 describe('with argTypes (from compodoc)', () => {
318 it('Should handle enum as strongly typed enum', () => {
319 const component = input_component_1.InputComponent;
320 const props = {
321 isDisabled: false,
322 label: 'Hello world',
323 accent: input_component_1.ButtonAccent.High,
324 };
325 const argTypes = {
326 accent: {
327 control: {
328 options: ['Normal', 'High'],
329 type: 'radio',
330 },
331 defaultValue: undefined,
332 table: {
333 category: 'inputs',
334 },
335 type: {
336 name: 'enum',
337 required: true,
338 value: [],
339 },
340 },
341 };
342 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
343 expect(source).toEqual(`<doc-button [accent]="'High'" [isDisabled]="false" [label]="'Hello world'"></doc-button>`);
344 });
345 it('Should handle enum without values as string', () => {
346 const component = input_component_1.InputComponent;
347 const props = {
348 isDisabled: false,
349 label: 'Hello world',
350 accent: input_component_1.ButtonAccent.High,
351 };
352 const argTypes = {
353 accent: {
354 control: {
355 options: ['Normal', 'High'],
356 type: 'radio',
357 },
358 defaultValue: undefined,
359 table: {
360 category: 'inputs',
361 },
362 type: {
363 name: 'object',
364 required: true,
365 value: {},
366 },
367 },
368 };
369 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, argTypes);
370 expect(source).toEqual(`<doc-button [accent]="'High'" [isDisabled]="false" [label]="'Hello world'"></doc-button>`);
371 });
372 it('Should handle objects correctly', () => {
373 const component = input_component_1.InputComponent;
374 const someDataObject = {
375 one: 'Hello world',
376 two: true,
377 three: [
378 `a string literal with "double quotes"`,
379 `a string literal with 'single quotes'`,
380 'a single quoted string with "double quotes"',
381 "a double quoted string with 'single quotes'",
382 // eslint-disable-next-line prettier/prettier
383 'a single quoted string with escaped \'single quotes\'',
384 // eslint-disable-next-line prettier/prettier
385 "a double quoted string with escaped \"double quotes\"",
386 // eslint-disable-next-line no-useless-escape
387 `a string literal with \'escaped single quotes\'`,
388 // eslint-disable-next-line no-useless-escape
389 `a string literal with \"escaped double quotes\"`,
390 ],
391 };
392 const props = {
393 isDisabled: false,
394 label: 'Hello world',
395 someDataObject,
396 };
397 const source = (0, ComputesTemplateFromComponent_1.computesTemplateSourceFromComponent)(component, props, null);
398 // Ideally we should stringify the object, but that could cause the story to break because of unescaped values in the JSON object.
399 // This will have to do for now
400 expect(source).toEqual(`<doc-button [isDisabled]="false" [label]="'Hello world'" [someDataObject]="{one: 'Hello world', two: true, three: ['a string literal with \\'double quotes\\'', 'a string literal with \\'single quotes\\'', 'a single quoted string with \\'double quotes\\'', 'a double quoted string with \\'single quotes\\'', 'a single quoted string with escaped \\'single quotes\\'', 'a double quoted string with escaped \\'double quotes\\'', 'a string literal with \\'escaped single quotes\\'', 'a string literal with \\'escaped double quotes\\'']}"></doc-button>`);
401 });
402 });
403});