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

fix(tests): Use async test API instead of testing Futures #47

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion test/_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,15 @@ class Logger implements List {

List<Function> _asyncQueue = [];

nextTurn() {
nextTurn([bool runUntilEmpty = false]) {
// copy the queue as it may change.
var toRun = new List.from(_asyncQueue);
_asyncQueue = [];
toRun.forEach((fn) => fn());

if (runUntilEmpty && !_asyncQueue.isEmpty) {
nextTurn(runUntilEmpty);
}
}

async(Function fn) =>
Expand Down
16 changes: 16 additions & 0 deletions test/_specs_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ main() {
})();
});

it('should run all the async calls if asked', () {
var log = [];
async(() {
new Future.value('s')
.then((_) {
log.add('firstThen');
new Future.value('t').then((_) {
log.add('2ndThen');
});
});
expect(log.join(' ')).toEqual('');
nextTurn(true);
expect(log.join(' ')).toEqual('firstThen 2ndThen');
})();
});


it('should complain if you dangle callbacks', () {
expect(() {
Expand Down
58 changes: 23 additions & 35 deletions test/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,47 +397,45 @@ main() {
module.directive(PublishMeComponent);
}));

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

SimpleComponent.lastTemplateLoader.template.then(expectAsync1((_) {
expect(element.textWithShadow()).toEqual('OUTTER-_1:INNER_2(OUTTER-_1)');
}));
}));
nextTurn();
expect(element.textWithShadow()).toEqual('OUTTER-_1:INNER_2(OUTTER-_1)');
})));

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

$compile(element)(injector, element);

ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
expect(renderedText(element)).toEqual('inside poof');
}));
}));
nextTurn();
expect(renderedText(element)).toEqual('inside poof');
})));

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

it('should behave nicely if a mapped attribute evals to null', inject(() {
nextTurn();
expect(renderedText(element)).toEqual('inside ');
})));

it('should behave nicely if a mapped attribute evals to null', async(inject(() {
$rootScope.val = null;
var element = $('<parent-expression fromParent=val></parent-expression>');
$compile(element)(injector, element);
ParentExpressionComponent.lastTemplateLoader.template.then(expectAsync1((_) {
expect(renderedText(element)).toEqual('inside ');
}));
}));

nextTurn();
expect(renderedText(element)).toEqual('inside ');
})));

it('should create a component with IO', inject(() {
var element = $(r'<div><io attr="A" expr="name" ondone="done=true"></io></div>');
Expand Down Expand Up @@ -471,14 +469,14 @@ main() {
}, throwsA(contains('No provider found for LocalAttrDirective! (resolving LocalAttrDirective)')));
}));

it('should publish component controller into the scope', inject(() {
it('should publish component controller into the scope', async(inject(() {
var element = $(r'<div><publish-me></publish-me></div>');
$compile(element)(injector, element);
$rootScope.$apply();
PublishMeComponent.lastTemplateLoader.template.then(expectAsync1((_) {
expect(element.textWithShadow()).toEqual('WORKED');
}));
}));

nextTurn();
expect(element.textWithShadow()).toEqual('WORKED');
})));
});

describe('controller scoping', () {
Expand All @@ -502,10 +500,8 @@ main() {

class SimpleComponent {
static String $template = r'{{name}}{{sep}}{{$id}}(<content>SHADOW-CONTENT</content>)';
static TemplateLoader lastTemplateLoader;
SimpleComponent(Scope scope, TemplateLoader templateLoader) {
scope.name = 'INNER';
lastTemplateLoader = templateLoader;
}
}

Expand All @@ -529,19 +525,11 @@ class CamelCaseMapComponent {
class ParentExpressionComponent {
static String $template = '<div>inside {{fromParent()}}</div>';
static Map $map = {"fromParent": "&"};
static TemplateLoader lastTemplateLoader;
ParentExpressionComponent(Scope shadowScope, TemplateLoader templateLoader) {
lastTemplateLoader = templateLoader;
}
}

class PublishMeComponent {
static String $template = r'<content>{{ctrlName.value}}</content>';
static String $publishAs = 'ctrlName';
static TemplateLoader lastTemplateLoader;

String value = 'WORKED';
PublishMeComponent(TemplateLoader templateLoader) {
lastTemplateLoader = templateLoader;
}
}
30 changes: 13 additions & 17 deletions test/shadow_root_options_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,31 @@ main() {
}));

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

it('should respect the reset-style-inheritance option', inject(() {
nextTurn();
expect(element[1].shadowRoot.query('div').getComputedStyle().border).toContain('3px solid');
// ""0px none"" is the default style.
expect(element[2].shadowRoot.query('div').getComputedStyle().border).toContain('0px none');
})));

it('should respect the reset-style-inheritance option', async(inject(() {
var element = $(
'<style>body { font-size: 20px; }</style>' + // font-size inherit's by default
'<reset-style-inheritance>not included</reset-style-inheritance>' +
'<default-options>not included</default-options>');
element.forEach((elt) { document.body.append(elt); }); // we need the computed style.
$compile(element)(injector, element);
ResetStyleInheritanceComponent.lastTemplateLoader.then(expectAsync1((_) {
expect(element[1].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('16px');
}));
DefaultOptionsComponent.lastTemplateLoader.then(expectAsync1((_) {
expect(element[2].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('20px');
}));
}));

nextTurn();
expect(element[1].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('16px');
expect(element[2].shadowRoot.query('div').getComputedStyle().fontSize).toEqual('20px');
})));
});
}
75 changes: 36 additions & 39 deletions test/templateurl_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ class HtmlAndCssComponent {
class InlineWithCssComponent {
static String $template = '<div>inline!</div>';
static String $cssUrl = 'simple.css';
static TemplateLoader lastTemplateLoader;
InlineWithCssComponent(TemplateLoader templateLoader) {
lastTemplateLoader = templateLoader;
}
}

class OnlyCssComponent {
Expand Down Expand Up @@ -79,55 +75,58 @@ main() {
$http.assertAllGetsCalled();
}));

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

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

$http.flush().then(expectAsync1((data) {
expect(renderedText(element)).toEqual('Simple!');
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
}));
}));
$http.flush();
nextTurn(true);

it('should load template from URL once', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
expect(renderedText(element)).toEqual('Simple!');
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
})));

it('should load template from URL once', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>', times: 2);

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

$http.flush().then(expectAsync1((data) {
expect(renderedText(element)).toEqual('Simple!Simple!');
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; LOG; SIMPLE; SIMPLE');
}));
}));
$http.flush();
nextTurn(true);

it('should load a CSS file into a style', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
expect(renderedText(element)).toEqual('Simple!Simple!');
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; LOG; SIMPLE; SIMPLE');
})));

it('should load a CSS file into a style', async(inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');

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

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

it('should load a CSS file with a \$template', inject((Compiler $compile, Scope $rootScope, Injector injector) {
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(
'<style>@import "simple.css"</style><div log="SIMPLE">Simple!</div>'
);
// Note: There is no ordering. It is who ever comes off the wire first!
expect(log.result()).toEqual('LOG; SIMPLE');
})));

it('should load a CSS file with a \$template', async(inject((Compiler $compile, Scope $rootScope, Injector injector) {
var element = $('<div><inline-with-css log>ignore</inline-with-css><div>');
$compile(element)(injector, element);
InlineWithCssComponent.lastTemplateLoader.template.then(expectAsync1((_) {
expect(renderedText(element)).toEqual('@import "simple.css"inline!');
}));
}));

nextTurn(true);
expect(renderedText(element)).toEqual('@import "simple.css"inline!');
})));

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

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

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

$http.flush().then(expectAsync1((data) {
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
}));
}));
nextTurn(true);
expect(renderedText(element)).toEqual('@import "simple.css"Simple!');
})));
});
}