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

feat(mocks): change MockHttpBackend to define assertions on flush. #1433

Closed
wants to merge 2 commits 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
388 changes: 272 additions & 116 deletions lib/mock/http_backend.dart

Large diffs are not rendered by default.

74 changes: 27 additions & 47 deletions test/core/templateurl_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,15 @@ _run({resolveUrls, staticMode}) {
Injector injector, VmTurnZone zone, MockHttpBackend backend,
DirectiveMap directives) {

backend
..whenGET('PREFIX:${prefix}simple.html').respond('<div log="SIMPLE">Simple!</div>')
..whenGET('PREFIX:${prefix}simple.css').respond('.hello{}');

var element = e('<div><html-and-css log>ignore</html-and-css><div>');
zone.run(() {
compile([element], directives)(rootScope, null, [element]);
});

backend.flush();
backend
..flushGET('PREFIX:${prefix}simple.css').respond('.hello{}')
..flushGET('PREFIX:${prefix}simple.html').respond('<div log="SIMPLE">Simple!</div>');

microLeap();

expect(element).toHaveText('.hello{}Simple!');
Expand All @@ -131,13 +130,10 @@ _run({resolveUrls, staticMode}) {
it('should replace element with template from url', async(
(Http http, Compiler compile, Scope rootScope, Logger log,
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
backend.expectGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!<img src="a.png"></div>');

var element = es('<div><simple-url log>ignore</simple-url><div>');
compile(element, directives)(rootScope, null, element);

microLeap();
backend.flush();
backend.flushGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
microLeap();

expect(element[0]).toHaveText('Simple!');
Expand All @@ -149,17 +145,14 @@ _run({resolveUrls, staticMode}) {
it('should load template from URL once', async(
(Http http, Compiler compile, Scope rootScope, Logger log,
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
backend.whenGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');

var element = es(
'<div>'
'<simple-url log>ignore</simple-url>'
'<simple-url log>ignore</simple-url>'
'<div>');
compile(element, directives)(rootScope, null, element);

microLeap();
backend.flush();
backend.flushGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
microLeap();

expect(element.first).toHaveText('Simple!Simple!');
Expand All @@ -172,15 +165,13 @@ _run({resolveUrls, staticMode}) {
it('should load a CSS file into a style', async(
(Http http, Compiler compile, Scope rootScope, Logger log,
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
backend
..expectGET('${prefix}simple.css').respond(200, '.hello{}')
..expectGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');

var element = e('<div><html-and-css log>ignore</html-and-css><div>');
compile([element], directives)(rootScope, null, [element]);

microLeap();
backend.flush();
backend
..flushGET('${prefix}simple.css').respond(200, '.hello{}')
..flushGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');

microLeap();

expect(element).toHaveText('.hello{}Simple!');
Expand All @@ -196,25 +187,23 @@ _run({resolveUrls, staticMode}) {
(Http http, Compiler compile, Scope rootScope, Injector injector,
MockHttpBackend backend, DirectiveMap directives) {
var element = es('<div><inline-with-css log>ignore</inline-with-css><div>');
backend.expectGET('${prefix}simple.css').respond(200, '.hello{}');
compile(element, directives)(rootScope, null, element);

backend.flushGET('${prefix}simple.css').respond(200, '.hello{}');
microLeap();
backend.flush();
microLeap();

expect(element[0]).toHaveText('.hello{}inline!');
}));

it('should ignore CSS load errors ', async(
(Http http, Compiler compile, Scope rootScope, Injector injector,
MockHttpBackend backend, DirectiveMap directives) {
var element = es('<div><inline-with-css log>ignore</inline-with-css><div>');
backend.expectGET('${prefix}simple.css').respond(500, 'some error');
compile(element, directives)(rootScope, null, element);

backend.flushGET('${prefix}simple.css').respond(500, 'some error');
microLeap();
backend.flush();
microLeap();

expect(element.first).toHaveText(
'/*\n'
'HTTP 500: some error\n'
Expand All @@ -226,28 +215,25 @@ _run({resolveUrls, staticMode}) {
(Http http, Compiler compile, Scope rootScope, Injector injector,
MockHttpBackend backend, DirectiveMap directives) {
var element = es('<div><only-css log>ignore</only-css><div>');
backend.expectGET('${prefix}simple.css').respond(200, '.hello{}');
compile(element, directives)(rootScope, null, element);

backend.flushGET('${prefix}simple.css').respond(200, '.hello{}');
microLeap();
backend.flush();
microLeap();

expect(element[0]).toHaveText('.hello{}');
}));

it('should load the CSS before the template is loaded', async(
(Http http, Compiler compile, Scope rootScope, Injector injector,
MockHttpBackend backend, DirectiveMap directives) {
backend
..expectGET('${prefix}simple.css').respond(200, '.hello{}')
..expectGET('${prefix}simple.html').respond(200, '<div>Simple!</div>');

var element = es('<html-and-css>ignore</html-and-css>');
compile(element, directives)(rootScope, null, element);

backend
..flushGET('${prefix}simple.css').respond(200, '.hello{}')
..flushGET('${prefix}simple.html').respond(200, '<div>Simple!</div>');
microLeap();
backend.flush();
microLeap();

expect(element.first).toHaveText('.hello{}Simple!');
}));
});
Expand All @@ -262,16 +248,13 @@ _run({resolveUrls, staticMode}) {
it('should load multiple CSS files into a style', async(
(Http http, Compiler compile, Scope rootScope, Logger log,
Injector injector, MockHttpBackend backend, DirectiveMap directives) {
backend
..expectGET('${prefix}simple.css').respond(200, '.hello{}')
..expectGET('${prefix}another.css').respond(200, '.world{}')
..expectGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');

var element = e('<div><html-and-css log>ignore</html-and-css><div>');
compile([element], directives)(rootScope, null, [element]);

microLeap();
backend.flush();
backend
..flushGET('${prefix}simple.css').respond(200, '.hello{}')
..flushGET('${prefix}another.css').respond(200, '.world{}')
..flushGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
microLeap();

expect(element).toHaveText('.hello{}.world{}Simple!');
Expand All @@ -295,15 +278,12 @@ _run({resolveUrls, staticMode}) {
it('should load css from the style cache for the second component', async(
(Http http, Compiler compile, MockHttpBackend backend, RootScope rootScope,
DirectiveMap directives, Injector injector) {
backend
..expectGET('${prefix}simple.css').respond(200, '.hello{}')
..expectGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');

var element = e('<div><html-and-css>ignore</html-and-css><div>');
compile([element], directives)(rootScope, null, [element]);

microLeap();
backend.flush();
backend
..flushGET('${prefix}simple.css').respond(200, '.hello{}')
..flushGET('${prefix}simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
microLeap();

expect(element.children[0].shadowRoot).toHaveHtml(
Expand Down
13 changes: 7 additions & 6 deletions test/core_dom/http_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,14 @@ void main() {


it('should do basic request', async(() {
backend.expect('GET', '/url').respond('');
http(url: '/url', method: 'GET');
flush();
backend.flushGET('/url').respond('');
}));


it('should pass data if specified', async(() {
backend.expect('POST', '/url', 'some-data').respond('');
http(url: '/url', method: 'POST', data: 'some-data');
flush();
backend.flushPOST('/url', 'some-data').respond('');
}));


Expand All @@ -89,13 +87,15 @@ void main() {
// we don't care about the data field.
backend.expect('POST', '/url', 'null').respond('');


expect(() {
http(url: '/url', method: 'POST');
backend.flush();
}).toThrow('with different data');

// satisfy the expectation for our afterEach's assert.
http(url: '/url', method: 'POST', data: 'null');
flush();
backend.flush();
}));

describe('backend', () {
Expand Down Expand Up @@ -975,8 +975,9 @@ void main() {
'synchronous', async(() {
backend.expect('POST', '/url', '').respond('');
http(url: '/url', method: 'POST', data: '');

expect(interceptorCalled).toBe(true);
expect(backend.responses.isEmpty).toBe(false); // request made immediately
expect(backend.requests.isEmpty).toBe(false); // request made immediately
flush();
}));
});
Expand Down
6 changes: 1 addition & 5 deletions test/core_dom/view_cache_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ main() {

expect(cache.fromHtml(HTML, directives)).toBe(firstFactory);

// Also for fromUrl
backend.whenGET('template.url').respond(200, HTML);

var httpFactory;
cache.fromUrl('template.url', directives, Uri.base).then((f) => httpFactory = f);

microLeap();
backend.flush();
backend.flushGET('template.url').respond(200, HTML);
microLeap();

expect(httpFactory).toBe(firstFactory);
Expand Down
73 changes: 26 additions & 47 deletions test/core_dom/web_platform_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ main() {
it('should scope styles to shadow dom across browsers.',
async((TestBed _, MockHttpBackend backend, WebPlatform platform) {

backend
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, 'span { background-color: red; '
'}')
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');

Element element = e('<span><test-wptc><span>ignore'
'</span></test-wptc></span>');

_.compile(element);

microLeap();
backend.flush();
microLeap();
backend
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, 'span {background-color: red;}')
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');

try {
document.body.append(element);
Expand All @@ -58,37 +53,27 @@ main() {
it('should not crash with an attribute selector; but wont work either..',
async((TestBed _, MockHttpBackend backend, WebPlatform platform) {

backend
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, 'span { background-color: red; '
'}')
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');

Element element = e('<span><test-wptca a><span>ignore'
'</span></test-wptca></span>');

_.compile(element);

microLeap();
backend.flush();
microLeap();
backend
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, 'span{background-color: red}')
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');
}));

it('should scope :host styles to the primary element.',
async((TestBed _, MockHttpBackend backend, WebPlatform platform) {

backend
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, ':host {'
'background-color: red; }')
..expectGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');

Element element = e('<span><test-wptc><span>ignore'
'</span></test-wptc></span>');

_.compile(element);

microLeap();
backend.flush();
microLeap();
backend
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/style.css').respond(200, ':host {background-color: red; }')
..flushGET('${TEST_SERVER_BASE_PREFIX}test/core_dom/template.html').respond(200, '<span>foo</span>');

try {
document.body.append(element);
Expand All @@ -109,21 +94,17 @@ main() {
xit('should scope ::content rules to component content.',
async((TestBed _, MockHttpBackend backend, WebPlatform platform) {

backend
..expectGET('style.css').respond(200,
"polyfill-next-selector { content: ':host span:not([:host])'; }"
"::content span { background-color: red; }")
..expectGET('template.html').respond(200,
'<span><content></content></span>');

Element element = e('<test-wptc><span>RED'
'</span></test-wptc>');

_.compile(element);

microLeap();
backend.flush();
microLeap();
backend
..flushGET('style.css').respond(200,
"polyfill-next-selector { content: ':host span:not([:host])'; }"
"::content span { background-color: red; }")
..flushGET('template.html').respond(200,
'<span><content></content></span>');

try {
document.body.append(element);
Expand All @@ -147,25 +128,23 @@ main() {
xit('should style into child shadow dom with ::shadow.',
async((TestBed _, MockHttpBackend backend, WebPlatform platform) {

backend
..expectGET('outer-style.css').respond(200, 'my-inner::shadow .foo {'
'background-color: red; }')
..expectGET('outer-html.html').respond(200,
'<my-inner><span class="foo">foo</span></my-inner>');


Element element = e('<my-outer></my-outer>');

_.compile(element);

microLeap();

backend
..flush()
..expectGET('inner-style.css').respond(200, '/* no style */')
..expectGET('inner-html.html').respond(200,
'<span class="foo"><content></content></span>');
..flushGET('outer-style.css').respond(200,
'my-inner::shadow .foo {background-color: red; }')
..flushGET('outer-html.html').respond(200,
'<my-inner><span class="foo">foo</span></my-inner>');

microLeap();
backend.flush();

backend
..flushGET('inner-style.css').respond(200, '/* no style */')
..flushGET('inner-html.html').respond(200,
'<span class="foo"><content></content></span>');

try {
document.body.append(element);
Expand Down
Loading