Skip to content

Commit bf609f0

Browse files
committed
refactor(tests): exctract createElement into a helper
1 parent c5b0baf commit bf609f0

20 files changed

+206
-268
lines changed

modules/core/test/compiler/compiler_spec.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, beforeEach, it, expect, ddescribe, iit} from 'test_lib/test_lib';
1+
import {describe, beforeEach, it, expect, ddescribe, iit, el} from 'test_lib/test_lib';
22
import {DOM} from 'facade/dom';
33
import {List} from 'facade/collection';
44

@@ -32,19 +32,19 @@ export function main() {
3232
var compiler = createCompiler( (parent, current, control) => {
3333
current.inheritedProtoView = rootProtoView;
3434
});
35-
compiler.compile(MainComponent, createElement('<div></div>')).then( (protoView) => {
35+
compiler.compile(MainComponent, el('<div></div>')).then( (protoView) => {
3636
expect(protoView).toBe(rootProtoView);
3737
done();
3838
});
3939
});
4040

4141
it('should use the given element', (done) => {
42-
var el = createElement('<div></div>');
42+
var element = el('<div></div>');
4343
var compiler = createCompiler( (parent, current, control) => {
4444
current.inheritedProtoView = new ProtoView(current.element, null);
4545
});
46-
compiler.compile(MainComponent, el).then( (protoView) => {
47-
expect(protoView.element).toBe(el);
46+
compiler.compile(MainComponent, element).then( (protoView) => {
47+
expect(protoView.element).toBe(element);
4848
done();
4949
});
5050
});
@@ -60,7 +60,7 @@ export function main() {
6060
});
6161

6262
it('should load nested components', (done) => {
63-
var mainEl = createElement('<div></div>');
63+
var mainEl = el('<div></div>');
6464
var compiler = createCompiler( (parent, current, control) => {
6565
current.inheritedProtoView = new ProtoView(current.element, null);
6666
current.inheritedElementBinder = current.inheritedProtoView.bindElement(null);
@@ -77,14 +77,14 @@ export function main() {
7777
});
7878

7979
it('should cache components', (done) => {
80-
var el = createElement('<div></div>');
80+
var element = el('<div></div>');
8181
var compiler = createCompiler( (parent, current, control) => {
8282
current.inheritedProtoView = new ProtoView(current.element, null);
8383
});
8484
var firstProtoView;
85-
compiler.compile(MainComponent, el).then( (protoView) => {
85+
compiler.compile(MainComponent, element).then( (protoView) => {
8686
firstProtoView = protoView;
87-
return compiler.compile(MainComponent, el);
87+
return compiler.compile(MainComponent, element);
8888
}).then( (protoView) => {
8989
expect(firstProtoView).toBe(protoView);
9090
done();
@@ -151,7 +151,3 @@ class MockStep extends CompileStep {
151151
this.processClosure(parent, current, control);
152152
}
153153
}
154-
155-
function createElement(html) {
156-
return DOM.createTemplate(html).content.firstChild;
157-
}

modules/core/test/compiler/integration_spec.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, xit, it, expect, beforeEach, ddescribe, iit} from 'test_lib/test_lib';
1+
import {describe, xit, it, expect, beforeEach, ddescribe, iit, el} from 'test_lib/test_lib';
22

33
import {DOM} from 'facade/dom';
44

@@ -33,7 +33,7 @@ export function main() {
3333
}
3434

3535
it('should consume text node changes', (done) => {
36-
compiler.compile(MyComp, createElement('<div>{{ctxProp}}</div>')).then((pv) => {
36+
compiler.compile(MyComp, el('<div>{{ctxProp}}</div>')).then((pv) => {
3737
createView(pv);
3838
ctx.ctxProp = 'Hello World!';
3939

@@ -44,7 +44,7 @@ export function main() {
4444
});
4545

4646
it('should consume element binding changes', (done) => {
47-
compiler.compile(MyComp, createElement('<div [id]="ctxProp"></div>')).then((pv) => {
47+
compiler.compile(MyComp, el('<div [id]="ctxProp"></div>')).then((pv) => {
4848
createView(pv);
4949

5050
ctx.ctxProp = 'Hello World!';
@@ -56,7 +56,7 @@ export function main() {
5656
});
5757

5858
it('should consume directive watch expression change.', (done) => {
59-
compiler.compile(MyComp, createElement('<div my-dir [elprop]="ctxProp"></div>')).then((pv) => {
59+
compiler.compile(MyComp, el('<div my-dir [elprop]="ctxProp"></div>')).then((pv) => {
6060
createView(pv);
6161

6262
ctx.ctxProp = 'Hello World!';
@@ -69,7 +69,7 @@ export function main() {
6969
});
7070

7171
it('should support nested components.', (done) => {
72-
compiler.compile(MyComp, createElement('<child-cmp></child-cmp>')).then((pv) => {
72+
compiler.compile(MyComp, el('<child-cmp></child-cmp>')).then((pv) => {
7373
createView(pv);
7474

7575
cd.detectChanges();
@@ -80,7 +80,7 @@ export function main() {
8080
});
8181

8282
it('should support template directives via `<template>` elements.', (done) => {
83-
compiler.compile(MyComp, createElement('<div><template let-some-tmpl="greeting"><copy-me>{{greeting}}</copy-me></template></div>')).then((pv) => {
83+
compiler.compile(MyComp, el('<div><template let-some-tmpl="greeting"><copy-me>{{greeting}}</copy-me></template></div>')).then((pv) => {
8484
createView(pv);
8585

8686
cd.detectChanges();
@@ -95,7 +95,7 @@ export function main() {
9595
});
9696

9797
it('should support template directives via `template` attribute.', (done) => {
98-
compiler.compile(MyComp, createElement('<div><copy-me template="some-tmpl #greeting">{{greeting}}</copy-me></div>')).then((pv) => {
98+
compiler.compile(MyComp, el('<div><copy-me template="some-tmpl #greeting">{{greeting}}</copy-me></div>')).then((pv) => {
9999
createView(pv);
100100

101101
cd.detectChanges();
@@ -111,7 +111,7 @@ export function main() {
111111
});
112112

113113
it('should emulate content tag', (done) => {
114-
var el = `<emulated-shadow-dom-component>` +
114+
var temp = `<emulated-shadow-dom-component>` +
115115
`<div>Light</div>` +
116116
`<div template="trivial-template">DOM</div>` +
117117
`</emulated-shadow-dom-component>`;
@@ -122,7 +122,7 @@ export function main() {
122122
return view;
123123
}
124124

125-
compiler.compile(MyComp, createElement(el)).
125+
compiler.compile(MyComp, el(temp)).
126126
then(createView).
127127
then((view) => {
128128
expect(DOM.getText(view.nodes[0])).toEqual('Before LightDOM After');
@@ -207,8 +207,3 @@ class MyService {
207207
this.greeting = 'hello';
208208
}
209209
}
210-
211-
212-
function createElement(html) {
213-
return DOM.createTemplate(html).content.firstChild;
214-
}

modules/core/test/compiler/pipeline/directive_parser_spec.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {describe, beforeEach, it, expect, iit, ddescribe} from 'test_lib/test_lib';
1+
import {describe, beforeEach, it, expect, iit, ddescribe, el} from 'test_lib/test_lib';
22
import {isPresent} from 'facade/lang';
33
import {ListWrapper, MapWrapper, StringMapWrapper} from 'facade/collection';
44
import {DirectiveParser} from 'core/compiler/pipeline/directive_parser';
@@ -50,126 +50,126 @@ export function main() {
5050
}
5151

5252
it('should not add directives if they are not used', () => {
53-
var results = createPipeline().process(createElement('<div></div>'));
53+
var results = createPipeline().process(el('<div></div>'));
5454
expect(results[0].decoratorDirectives).toBe(null);
5555
expect(results[0].componentDirective).toBe(null);
5656
expect(results[0].templateDirective).toBe(null);
5757
});
5858

5959
describe('component directives', () => {
6060
it('should detect them in attributes', () => {
61-
var results = createPipeline().process(createElement('<div some-comp></div>'));
61+
var results = createPipeline().process(el('<div some-comp></div>'));
6262
expect(results[0].componentDirective).toEqual(reader.read(SomeComponent));
6363
});
6464

6565
it('should detect them in property bindings', () => {
6666
var pipeline = createPipeline({propertyBindings: {
6767
'some-comp': 'someExpr'
6868
}});
69-
var results = pipeline.process(createElement('<div></div>'));
69+
var results = pipeline.process(el('<div></div>'));
7070
expect(results[0].componentDirective).toEqual(reader.read(SomeComponent));
7171
});
7272

7373
it('should detect them in variable bindings', () => {
7474
var pipeline = createPipeline({variableBindings: {
7575
'some-comp': 'someExpr'
7676
}});
77-
var results = pipeline.process(createElement('<div></div>'));
77+
var results = pipeline.process(el('<div></div>'));
7878
expect(results[0].componentDirective).toEqual(reader.read(SomeComponent));
7979
});
8080

8181
it('should not allow multiple component directives on the same element', () => {
8282
expect( () => {
8383
createPipeline().process(
84-
createElement('<div some-comp some-comp2></div>')
84+
el('<div some-comp some-comp2></div>')
8585
);
8686
}).toThrowError('Only one component directive per element is allowed!');
8787
});
8888

8989
it('should not allow component directives on <template> elements', () => {
9090
expect( () => {
9191
createPipeline().process(
92-
createElement('<template some-comp></template>')
92+
el('<template some-comp></template>')
9393
);
9494
}).toThrowError('Only template directives are allowed on <template> elements!');
9595
});
9696
});
9797

9898
describe('template directives', () => {
9999
it('should detect them in attributes', () => {
100-
var results = createPipeline().process(createElement('<template some-templ></template>'));
100+
var results = createPipeline().process(el('<template some-templ></template>'));
101101
expect(results[0].templateDirective).toEqual(reader.read(SomeTemplate));
102102
});
103103

104104
it('should detect them in property bindings', () => {
105105
var pipeline = createPipeline({propertyBindings: {
106106
'some-templ': 'someExpr'
107107
}});
108-
var results = pipeline.process(createElement('<template></template>'));
108+
var results = pipeline.process(el('<template></template>'));
109109
expect(results[0].templateDirective).toEqual(reader.read(SomeTemplate));
110110
});
111111

112112
it('should detect them in variable bindings', () => {
113113
var pipeline = createPipeline({variableBindings: {
114114
'some-templ': 'someExpr'
115115
}});
116-
var results = pipeline.process(createElement('<template></template>'));
116+
var results = pipeline.process(el('<template></template>'));
117117
expect(results[0].templateDirective).toEqual(reader.read(SomeTemplate));
118118
});
119119

120120
it('should not allow multiple template directives on the same element', () => {
121121
expect( () => {
122122
createPipeline().process(
123-
createElement('<template some-templ some-templ2></template>')
123+
el('<template some-templ some-templ2></template>')
124124
);
125125
}).toThrowError('Only one template directive per element is allowed!');
126126
});
127127

128128
it('should not allow template directives on non <template> elements', () => {
129129
expect( () => {
130130
createPipeline().process(
131-
createElement('<div some-templ></div>')
131+
el('<div some-templ></div>')
132132
);
133133
}).toThrowError('Template directives need to be placed on <template> elements or elements with template attribute!');
134134
});
135135
});
136136

137137
describe('decorator directives', () => {
138138
it('should detect them in attributes', () => {
139-
var results = createPipeline().process(createElement('<div some-decor></div>'));
139+
var results = createPipeline().process(el('<div some-decor></div>'));
140140
expect(results[0].decoratorDirectives).toEqual([reader.read(SomeDecorator)]);
141141
});
142142

143143
it('should detect them in property bindings', () => {
144144
var pipeline = createPipeline({propertyBindings: {
145145
'some-decor': 'someExpr'
146146
}});
147-
var results = pipeline.process(createElement('<div></div>'));
147+
var results = pipeline.process(el('<div></div>'));
148148
expect(results[0].decoratorDirectives).toEqual([reader.read(SomeDecorator)]);
149149
});
150150

151151
it('should compile children by default', () => {
152-
var results = createPipeline().process(createElement('<div some-decor></div>'));
152+
var results = createPipeline().process(el('<div some-decor></div>'));
153153
expect(results[0].compileChildren).toEqual(true);
154154
});
155155

156156
it('should stop compiling children when specified in the decorator config', () => {
157-
var results = createPipeline().process(createElement('<div some-decor-ignoring-children></div>'));
157+
var results = createPipeline().process(el('<div some-decor-ignoring-children></div>'));
158158
expect(results[0].compileChildren).toEqual(false);
159159
});
160160

161161
it('should detect them in variable bindings', () => {
162162
var pipeline = createPipeline({variableBindings: {
163163
'some-decor': 'someExpr'
164164
}});
165-
var results = pipeline.process(createElement('<div></div>'));
165+
var results = pipeline.process(el('<div></div>'));
166166
expect(results[0].decoratorDirectives).toEqual([reader.read(SomeDecorator)]);
167167
});
168168

169169
it('should not allow decorator directives on <template> elements', () => {
170170
expect( () => {
171171
createPipeline().process(
172-
createElement('<template some-decor></template>')
172+
el('<template some-decor></template>')
173173
);
174174
}).toThrowError('Only template directives are allowed on <template> elements!');
175175
});
@@ -226,7 +226,3 @@ class SomeComponent2 {}
226226
})
227227
})
228228
class MyComp {}
229-
230-
function createElement(html) {
231-
return DOM.createTemplate(html).content.firstChild;
232-
}

0 commit comments

Comments
 (0)