-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathclass-fold_spec.ts
94 lines (82 loc) · 3.22 KB
/
class-fold_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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line:no-implicit-dependencies
import { tags } from '@angular-devkit/core';
import { transformJavascript } from '../helpers/transform-javascript';
import { getFoldFileTransformer } from './class-fold';
const transform = (content: string) => transformJavascript(
{ content, getTransforms: [getFoldFileTransformer], typeCheck: true }).content;
describe('class-fold', () => {
describe('es5', () => {
it('folds static properties into class', () => {
const staticProperty = 'Clazz.prop = 1;';
const input = tags.stripIndent`
var Clazz = (function () { function Clazz() { } return Clazz; }());
${staticProperty}
`;
const output = tags.stripIndent`
var Clazz = (function () { function Clazz() { }
${staticProperty} return Clazz; }());
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('folds multiple static properties into class', () => {
const staticProperty = 'Clazz.prop = 1;';
const anotherStaticProperty = 'Clazz.anotherProp = 2;';
const input = tags.stripIndent`
var Clazz = (function () { function Clazz() { } return Clazz; }());
${staticProperty}
${anotherStaticProperty}
`;
const output = tags.stripIndent`
var Clazz = (function () { function Clazz() { }
${staticProperty} ${anotherStaticProperty} return Clazz; }());
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
});
describe('es2015', () => {
it('folds static properties in IIFE', () => {
const input = tags.stripIndent`
export class TemplateRef { }
TemplateRef.__NG_ELEMENT_ID__ = () => SWITCH_TEMPLATE_REF_FACTORY(TemplateRef, ElementRef);
`;
const output = tags.stripIndent`
export const TemplateRef = /*@__PURE__*/ function () {
class TemplateRef { }
TemplateRef.__NG_ELEMENT_ID__ = () => SWITCH_TEMPLATE_REF_FACTORY(TemplateRef, ElementRef);
return TemplateRef;
}();
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it('folds multiple static properties into class', () => {
const input = tags.stripIndent`
export class TemplateRef { }
TemplateRef.__NG_ELEMENT_ID__ = () => SWITCH_TEMPLATE_REF_FACTORY(TemplateRef, ElementRef);
TemplateRef.somethingElse = true;
`;
const output = tags.stripIndent`
export const TemplateRef = /*@__PURE__*/ function () {
class TemplateRef {
}
TemplateRef.__NG_ELEMENT_ID__ = () => SWITCH_TEMPLATE_REF_FACTORY(TemplateRef, ElementRef);
TemplateRef.somethingElse = true;
return TemplateRef;
}();
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});
it(`doesn't wrap classes without static properties in IIFE`, () => {
const input = tags.stripIndent`
export class TemplateRef { }
`;
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${input}`);
});
});
});