forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle_compiler_spec.ts
More file actions
201 lines (175 loc) · 7.71 KB
/
style_compiler_spec.ts
File metadata and controls
201 lines (175 loc) · 7.71 KB
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
import {
ddescribe,
describe,
xdescribe,
it,
iit,
xit,
expect,
beforeEach,
afterEach,
AsyncTestCompleter,
inject
} from 'angular2/test_lib';
import {IS_DART} from '../platform';
import {SpyXHR} from '../core/spies';
import {CONST_EXPR, isPresent, BaseException} from 'angular2/src/core/facade/lang';
import {PromiseWrapper, Promise} from 'angular2/src/core/facade/async';
import {evalModule} from './eval_module';
import {StyleCompiler} from 'angular2/src/compiler/style_compiler';
import {UrlResolver} from 'angular2/src/core/services/url_resolver';
import {
DirectiveMetadata,
TemplateMetadata,
TypeMetadata,
ViewEncapsulation
} from 'angular2/src/compiler/api';
// Attention: These module names have to correspond to real modules!
const MODULE_NAME = 'angular2/test/compiler/style_compiler_spec';
const IMPORT_ABS_MODULE_NAME = 'angular2/test/compiler/style_compiler_import';
const IMPORT_REL_MODULE_NAME = './style_compiler_import';
// Note: Not a real module, only used via mocks.
const IMPORT_ABS_MODULE_NAME_WITH_IMPORT =
'angular2/test/compiler/style_compiler_transitive_import';
export function main() {
describe('StyleCompiler', () => {
var compiler: StyleCompiler;
var xhr;
beforeEach(() => {
xhr = <any>new SpyXHR();
compiler = new StyleCompiler(xhr, new UrlResolver());
});
function comp(styles: string[], styleAbsUrls: string[], encapsulation: ViewEncapsulation):
DirectiveMetadata {
return new DirectiveMetadata({
type: new TypeMetadata({id: 23, typeUrl: 'someUrl'}),
template: new TemplateMetadata(
{styles: styles, styleAbsUrls: styleAbsUrls, encapsulation: encapsulation})
});
}
describe('compileComponentRuntime', () => {
function runTest(styles: string[], styleAbsUrls: string[], encapsulation: ViewEncapsulation,
expectedStyles: string[]) {
return inject([AsyncTestCompleter], (async) => {
// Note: Can't use MockXHR as the xhr is called recursively,
// so we can't trigger flush.
xhr.spy('get').andCallFake((url) => {
var response;
if (url == IMPORT_ABS_MODULE_NAME) {
response = 'span {color: blue}';
} else if (url == IMPORT_ABS_MODULE_NAME_WITH_IMPORT) {
response = `a {color: green}@import ${IMPORT_REL_MODULE_NAME};`;
} else {
throw new BaseException(`Unexpected url ${url}`);
}
return PromiseWrapper.resolve(response);
});
compiler.compileComponentRuntime(comp(styles, styleAbsUrls, encapsulation))
.then((value) => {
expect(value).toEqual(expectedStyles);
async.done();
});
});
}
describe('no shim', () => {
var encapsulation = ViewEncapsulation.None;
it('should compile plain css rules',
runTest(['div {color: red}', 'span {color: blue}'], [], encapsulation,
['div {color: red}', 'span {color: blue}']));
it('should allow to import rules',
runTest(['div {color: red}'], [IMPORT_ABS_MODULE_NAME], encapsulation,
['div {color: red}', 'span {color: blue}']));
it('should allow to import rules transitively',
runTest(['div {color: red}'], [IMPORT_ABS_MODULE_NAME_WITH_IMPORT], encapsulation,
['div {color: red}', 'a {color: green}', 'span {color: blue}']));
});
describe('with shim', () => {
var encapsulation = ViewEncapsulation.Emulated;
it('should compile plain css rules',
runTest(
['div {\ncolor: red;\n}', 'span {\ncolor: blue;\n}'], [], encapsulation,
['div[_ngcontent-23] {\ncolor: red;\n}', 'span[_ngcontent-23] {\ncolor: blue;\n}']));
it('should allow to import rules',
runTest(
['div {\ncolor: red;\n}'], [IMPORT_ABS_MODULE_NAME], encapsulation,
['div[_ngcontent-23] {\ncolor: red;\n}', 'span[_ngcontent-23] {\ncolor: blue;\n}']));
it('should allow to import rules transitively',
runTest(['div {\ncolor: red;\n}'], [IMPORT_ABS_MODULE_NAME_WITH_IMPORT], encapsulation, [
'div[_ngcontent-23] {\ncolor: red;\n}',
'a[_ngcontent-23] {\ncolor: green;\n}',
'span[_ngcontent-23] {\ncolor: blue;\n}'
]));
});
});
describe('compileComponentCodeGen', () => {
function runTest(styles: string[], styleAbsUrls: string[], encapsulation: ViewEncapsulation,
expectedStyles: string[]) {
return inject([AsyncTestCompleter], (async) => {
var sourceModule =
compiler.compileComponentCodeGen(comp(styles, styleAbsUrls, encapsulation));
evalModule(testableModule(sourceModule.source), sourceModule.imports, null)
.then((value) => {
expect(value).toEqual(expectedStyles);
async.done();
});
});
}
describe('no shim', () => {
var encapsulation = ViewEncapsulation.None;
it('should compile plain css ruless',
runTest(['div {color: red}', 'span {color: blue}'], [], encapsulation,
['div {color: red}', 'span {color: blue}']));
it('should compile css rules with newlines and quotes',
runTest(['div\n{"color": \'red\'}'], [], encapsulation, ['div\n{"color": \'red\'}']));
it('should allow to import rules',
runTest(['div {color: red}'], [IMPORT_ABS_MODULE_NAME], encapsulation,
['div {color: red}', 'span {color: blue}']));
});
describe('with shim', () => {
var encapsulation = ViewEncapsulation.Emulated;
it('should compile plain css ruless',
runTest(
['div {\ncolor: red;\n}', 'span {\ncolor: blue;\n}'], [], encapsulation,
['div[_ngcontent-23] {\ncolor: red;\n}', 'span[_ngcontent-23] {\ncolor: blue;\n}']));
it('should allow to import rules',
runTest(
['div {color: red}'], [IMPORT_ABS_MODULE_NAME], encapsulation,
['div[_ngcontent-23] {\ncolor: red;\n}', 'span[_ngcontent-23] {\ncolor: blue;\n}']));
});
});
describe('compileStylesheetCodeGen', () => {
function runTest(style: string, expectedStyles: string[], expectedShimStyles: string[]) {
return inject([AsyncTestCompleter], (async) => {
var sourceModules = compiler.compileStylesheetCodeGen(MODULE_NAME, style);
PromiseWrapper.all(sourceModules.map(sourceModule =>
evalModule(testableModule(sourceModule.source),
sourceModule.imports, null)))
.then((values) => {
expect(values[0]).toEqual(expectedStyles);
expect(values[1]).toEqual(expectedShimStyles);
async.done();
});
});
}
it('should compile plain css rules', runTest('div {color: red;}', ['div {color: red;}'],
['div[_ngcontent-%COMP%] {\ncolor: red;\n}']));
it('should allow to import rules with relative paths',
runTest(`div {color: red}@import ${IMPORT_REL_MODULE_NAME};`,
['div {color: red}', 'span {color: blue}'], [
'div[_ngcontent-%COMP%] {\ncolor: red;\n}',
'span[_ngcontent-%COMP%] {\ncolor: blue;\n}'
]));
});
});
}
function testableModule(sourceModule: string) {
if (IS_DART) {
return `${sourceModule}
run(_) { return STYLES; }
`;
} else {
return `${sourceModule}
exports.run = function(_) { return STYLES; };
`;
}
}