Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit bacdbe0

Browse files
committed
fix(tests): Use async test API instead of testing Futures
1 parent 9868db2 commit bacdbe0

5 files changed

+93
-92
lines changed

test/_specs.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ class Logger implements List {
157157

158158
List<Function> _asyncQueue = [];
159159

160-
nextTurn() {
160+
nextTurn([bool runUntilEmpty = false]) {
161161
// copy the queue as it may change.
162162
var toRun = new List.from(_asyncQueue);
163163
_asyncQueue = [];
164164
toRun.forEach((fn) => fn());
165+
166+
if (runUntilEmpty && !_asyncQueue.isEmpty) {
167+
nextTurn(runUntilEmpty);
168+
}
165169
}
166170

167171
async(Function fn) =>

test/_specs_spec.dart

+16
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ main() {
8888
})();
8989
});
9090

91+
it('should run all the async calls if asked', () {
92+
var log = [];
93+
async(() {
94+
new Future.value('s')
95+
.then((_) {
96+
log.add('firstThen');
97+
new Future.value('t').then((_) {
98+
log.add('2ndThen');
99+
});
100+
});
101+
expect(log.join(' ')).toEqual('');
102+
nextTurn(true);
103+
expect(log.join(' ')).toEqual('firstThen 2ndThen');
104+
})();
105+
});
106+
91107

92108
it('should complain if you dangle callbacks', () {
93109
expect(() {

test/compiler_spec.dart

+23-35
Original file line numberDiff line numberDiff line change
@@ -397,47 +397,45 @@ main() {
397397
module.directive(PublishMeComponent);
398398
}));
399399

400-
it('should create a simple component', inject(() {
400+
it('should create a simple component', async(inject(() {
401401
$rootScope.name = 'OUTTER';
402402
$rootScope.sep = '-';
403403
var element = $(r'<div>{{name}}{{sep}}{{$id}}:<simple>{{name}}{{sep}}{{$id}}</simple></div>');
404404
BlockType blockType = $compile(element);
405405
Block block = blockType(injector, element);
406406
$rootScope.$digest();
407407

408-
SimpleComponent.lastTemplateLoader.template.then(expectAsync1((_) {
409-
expect(element.textWithShadow()).toEqual('OUTTER-_1:INNER_2(OUTTER-_1)');
410-
}));
411-
}));
408+
nextTurn();
409+
expect(element.textWithShadow()).toEqual('OUTTER-_1:INNER_2(OUTTER-_1)');
410+
})));
412411

413-
it('should create a component that can access parent scope', inject(() {
412+
it('should create a component that can access parent scope', async(inject(() {
414413
$rootScope.fromParent = "should not be used";
415414
$rootScope.val = "poof";
416415
var element = $('<parent-expression from-parent=val></parent-expression>');
417416

418417
$compile(element)(injector, element);
419418

420-
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
421-
expect(renderedText(element)).toEqual('inside poof');
422-
}));
423-
}));
419+
nextTurn();
420+
expect(renderedText(element)).toEqual('inside poof');
421+
})));
424422

425-
it('should behave nicely if a mapped attribute is missing', inject(() {
423+
it('should behave nicely if a mapped attribute is missing', async(inject(() {
426424
var element = $('<parent-expression></parent-expression>');
427425
$compile(element)(injector, element);
428-
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
429-
expect(renderedText(element)).toEqual('inside ');
430-
}));
431-
}));
432426

433-
it('should behave nicely if a mapped attribute evals to null', inject(() {
427+
nextTurn();
428+
expect(renderedText(element)).toEqual('inside ');
429+
})));
430+
431+
it('should behave nicely if a mapped attribute evals to null', async(inject(() {
434432
$rootScope.val = null;
435433
var element = $('<parent-expression fromParent=val></parent-expression>');
436434
$compile(element)(injector, element);
437-
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
438-
expect(renderedText(element)).toEqual('inside ');
439-
}));
440-
}));
435+
436+
nextTurn();
437+
expect(renderedText(element)).toEqual('inside ');
438+
})));
441439

442440
it('should create a component with IO', inject(() {
443441
var element = $(r'<div><io attr="A" expr="name" ondone="done=true"></io></div>');
@@ -483,14 +481,14 @@ main() {
483481
}, throwsA(contains('No provider found for LocalAttrDirective! (resolving LocalAttrDirective)')));
484482
}));
485483

486-
it('should publish component controller into the scope', inject(() {
484+
it('should publish component controller into the scope', async(inject(() {
487485
var element = $(r'<div><publish-me></publish-me></div>');
488486
$compile(element)(injector, element);
489487
$rootScope.$apply();
490-
PublishMeComponent.lastTemplateLoader.template.then(expectAsync1((_) {
491-
expect(element.textWithShadow()).toEqual('WORKED');
492-
}));
493-
}));
488+
489+
nextTurn();
490+
expect(element.textWithShadow()).toEqual('WORKED');
491+
})));
494492
});
495493

496494
describe('controller scoping', () {
@@ -514,10 +512,8 @@ main() {
514512

515513
class SimpleComponent {
516514
static String $template = r'{{name}}{{sep}}{{$id}}(<content>SHADOW-CONTENT</content>)';
517-
static TemplateLoader lastTemplateLoader;
518515
SimpleComponent(Scope scope, TemplateLoader templateLoader) {
519516
scope.name = 'INNER';
520-
lastTemplateLoader = templateLoader;
521517
}
522518
}
523519

@@ -541,19 +537,11 @@ class CamelCaseMapComponent {
541537
class ParentExpressionComponent {
542538
static String $template = '<div>inside {{fromParent()}}</div>';
543539
static Map $map = {"fromParent": "&"};
544-
static TemplateLoader lastTemplateLoader;
545-
ParentExpressionComponent(Scope shadowScope, TemplateLoader templateLoader) {
546-
lastTemplateLoader = templateLoader;
547-
}
548540
}
549541

550542
class PublishMeComponent {
551543
static String $template = r'<content>{{ctrlName.value}}</content>';
552544
static String $publishAs = 'ctrlName';
553-
static TemplateLoader lastTemplateLoader;
554545

555546
String value = 'WORKED';
556-
PublishMeComponent(TemplateLoader templateLoader) {
557-
lastTemplateLoader = templateLoader;
558-
}
559547
}

test/shadow_root_options_spec.dart

+13-17
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,31 @@ main() {
4545
}));
4646

4747
describe('shadow dom options', () {
48-
it('should respect the apply-author-style option', inject(() {
48+
it('should respect the apply-author-style option', async(inject(() {
4949
var element = $(
5050
'<style>div { border: 3px solid green }</style>' +
5151
'<apply-author-style>not included</apply-author-style>' +
5252
'<default-options>not included</default-options>');
5353
element.forEach((elt) { document.body.append(elt); }); // we need the computed style.
5454
$compile(element)(injector, element);
55-
ApplyAuthorStyleComponent.lastTemplateLoader.then(expectAsync1((_) {
56-
expect(element[1].shadowRoot.query('div').getComputedStyle().border).toContain('3px solid');
57-
}));
58-
DefaultOptionsComponent.lastTemplateLoader.then(expectAsync1((_) {
59-
// ""0px none"" is the default style.
60-
expect(element[2].shadowRoot.query('div').getComputedStyle().border).toContain('0px none');
61-
}));
62-
}));
6355

64-
it('should respect the reset-style-inheritance option', inject(() {
56+
nextTurn();
57+
expect(element[1].shadowRoot.query('div').getComputedStyle().border).toContain('3px solid');
58+
// ""0px none"" is the default style.
59+
expect(element[2].shadowRoot.query('div').getComputedStyle().border).toContain('0px none');
60+
})));
61+
62+
it('should respect the reset-style-inheritance option', async(inject(() {
6563
var element = $(
6664
'<style>body { font-size: 20px; }</style>' + // font-size inherit's by default
6765
'<reset-style-inheritance>not included</reset-style-inheritance>' +
6866
'<default-options>not included</default-options>');
6967
element.forEach((elt) { document.body.append(elt); }); // we need the computed style.
7068
$compile(element)(injector, element);
71-
ResetStyleInheritanceComponent.lastTemplateLoader.then(expectAsync1((_) {
72-
expect(element[1].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('16px');
73-
}));
74-
DefaultOptionsComponent.lastTemplateLoader.then(expectAsync1((_) {
75-
expect(element[2].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('20px');
76-
}));
77-
}));
69+
70+
nextTurn();
71+
expect(element[1].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('16px');
72+
expect(element[2].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('20px');
73+
})));
7874
});
7975
}

test/templateurl_spec.dart

+36-39
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class HtmlAndCssComponent {
2222
class InlineWithCssComponent {
2323
static String $template = '<div>inline!</div>';
2424
static String $cssUrl = 'simple.css';
25-
static TemplateLoader lastTemplateLoader;
26-
InlineWithCssComponent(TemplateLoader templateLoader) {
27-
lastTemplateLoader = templateLoader;
28-
}
2925
}
3026

3127
class OnlyCssComponent {
@@ -79,55 +75,58 @@ main() {
7975
$http.assertAllGetsCalled();
8076
}));
8177

82-
it('should replace element with template from url', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
78+
it('should replace element with template from url', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
8379
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');
8480

8581
var element = $('<div><simple-url log>ignore</simple-url><div>');
8682
$compile(element)(injector, element);
8783

88-
$http.flush().then(expectAsync1((data) {
89-
expect(renderedText(element)).toEqual('Simple!');
90-
// Note: There is no ordering. It is who ever comes off the wire first!
91-
expect(log.result()).toEqual('LOG; SIMPLE');
92-
}));
93-
}));
84+
$http.flush();
85+
nextTurn(true);
9486

95-
it('should load template from URL once', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
87+
expect(renderedText(element)).toEqual('Simple!');
88+
// Note: There is no ordering. It is who ever comes off the wire first!
89+
expect(log.result()).toEqual('LOG; SIMPLE');
90+
})));
91+
92+
it('should load template from URL once', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
9693
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>', times: 2);
9794

9895
var element = $('<div><simple-url log>ignore</simple-url><simple-url log>ignore</simple-url><div>');
9996
$compile(element)(injector, element);
10097

101-
$http.flush().then(expectAsync1((data) {
102-
expect(renderedText(element)).toEqual('Simple!Simple!');
103-
// Note: There is no ordering. It is who ever comes off the wire first!
104-
expect(log.result()).toEqual('LOG; LOG; SIMPLE; SIMPLE');
105-
}));
106-
}));
98+
$http.flush();
99+
nextTurn(true);
107100

108-
it('should load a CSS file into a style', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
101+
expect(renderedText(element)).toEqual('Simple!Simple!');
102+
// Note: There is no ordering. It is who ever comes off the wire first!
103+
expect(log.result()).toEqual('LOG; LOG; SIMPLE; SIMPLE');
104+
})));
105+
106+
it('should load a CSS file into a style', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
109107
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');
110108

111109
var element = $('<div><html-and-css log>ignore</html-and-css><div>');
112110
$compile(element)(injector, element);
113111

114-
$http.flush().then(expectAsync1((data) {
115-
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
116-
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
117-
'<style>@import "simple.css"</style><div log="SIMPLE">Simple!</div>'
118-
);
119-
// Note: There is no ordering. It is who ever comes off the wire first!
120-
expect(log.result()).toEqual('LOG; SIMPLE');
121-
}));
122-
}));
112+
$http.flush();
113+
nextTurn(true);
123114

124-
it('should load a CSS file with a \$template', inject((Compiler $compile, Scope $rootScope, Injector injector) {
115+
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
116+
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
117+
'<style>@import "simple.css"</style><div log="SIMPLE">Simple!</div>'
118+
);
119+
// Note: There is no ordering. It is who ever comes off the wire first!
120+
expect(log.result()).toEqual('LOG; SIMPLE');
121+
})));
122+
123+
it('should load a CSS file with a \$template', async(inject((Compiler $compile, Scope $rootScope, Injector injector) {
125124
var element = $('<div><inline-with-css log>ignore</inline-with-css><div>');
126125
$compile(element)(injector, element);
127-
InlineWithCssComponent.lastTemplateLoader.template.then(expectAsync1((_) {
128-
expect(renderedText(element)).toEqual('@import "simple.css"inline!');
129-
}));
130-
}));
126+
127+
nextTurn(true);
128+
expect(renderedText(element)).toEqual('@import "simple.css"inline!');
129+
})));
131130

132131
it('should load a CSS with no template', inject((Compiler $compile, Scope $rootScope, Injector injector) {
133132
var element = $('<div><only-css log>ignore</only-css><div>');
@@ -136,7 +135,7 @@ main() {
136135
expect(renderedText(element)).toEqual('@import "simple.css"');
137136
}));
138137

139-
it('should load the CSS before the template is loaded', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Injector injector) {
138+
it('should load the CSS before the template is loaded', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Injector injector) {
140139
$http.expectGET('simple.html', '<div>Simple!</div>');
141140

142141
var element = $('<html-and-css>ignore</html-and-css>');
@@ -145,10 +144,8 @@ main() {
145144
// The HTML is not loaded yet, but the CSS @import should be in the DOM.
146145
expect(renderedText(element)).toEqual('@import "simple.css"');
147146

148-
$http.flush().then(expectAsync1((data) {
149-
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
150-
}));
151-
}));
147+
nextTurn(true);
148+
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
149+
})));
152150
});
153151
}
154-

0 commit comments

Comments
 (0)