feat(query): notify on changes

This commit is contained in:
vsavkin
2015-06-12 09:45:31 -07:00
parent 73d152506b
commit 5bfcca2d5b
11 changed files with 129 additions and 7 deletions

View File

@ -324,6 +324,12 @@ export function main() {
});
});
it('should notify the dispatcher on all changes done', () => {
var val = _createChangeDetector('name', new Person('bob'));
val.changeDetector.detectChanges();
expect(val.dispatcher.onAllChangesDoneCalled).toEqual(true);
});
describe('updating directives', () => {
var directive1;
var directive2;
@ -975,6 +981,7 @@ class FakeDirectives {
class TestDispatcher extends ChangeDispatcher {
log: List<string>;
loggedValues: List<any>;
onAllChangesDoneCalled: boolean = false;
constructor() {
super();
@ -984,6 +991,7 @@ class TestDispatcher extends ChangeDispatcher {
clear() {
this.log = ListWrapper.create();
this.loggedValues = ListWrapper.create();
this.onAllChangesDoneCalled = true;
}
notifyOnBinding(binding, value) {
@ -991,6 +999,8 @@ class TestDispatcher extends ChangeDispatcher {
ListWrapper.push(this.loggedValues, value);
}
notifyOnAllChangesDone() { this.onAllChangesDoneCalled = true; }
_asString(value) { return (isBlank(value) ? 'null' : value.toString()); }
}

View File

@ -12,6 +12,8 @@ import {
beforeEach,
SpyObject,
proxy,
inject,
AsyncTestCompleter,
el,
containsRegexp
} from 'angular2/test_lib';
@ -730,7 +732,7 @@ export function main() {
expect(d.dependency).toBeAnInstanceOf(SimpleDirective);
});
it("should throw when a depenency cannot be resolved", () => {
it("should throw when a dependency cannot be resolved", () => {
expect(() => injector(ListWrapper.concat([NeedsDirectiveFromParent], extraBindings)))
.toThrowError(containsRegexp(
`No provider for ${stringify(SimpleDirective) }! (${stringify(NeedsDirectiveFromParent) } -> ${stringify(SimpleDirective) })`));
@ -818,7 +820,7 @@ export function main() {
});
describe("lifecycle", () => {
it("should call onDestroy on directives subscribed to this event", function() {
it("should call onDestroy on directives subscribed to this event", () => {
var inj = injector(ListWrapper.concat(
[DirectiveBinding.createFromType(DirectiveWithDestroy,
new dirAnn.Directive({lifecycle: [onDestroy]}))],
@ -828,13 +830,45 @@ export function main() {
expect(destroy.onDestroyCounter).toBe(1);
});
it("should work with services", function() {
it("should work with services", () => {
var inj = injector(ListWrapper.concat(
[DirectiveBinding.createFromType(
SimpleDirective, new dirAnn.Directive({hostInjector: [SimpleService]}))],
extraBindings));
inj.dehydrate();
});
it("should notify queries", inject([AsyncTestCompleter], (async) => {
var inj = injector(ListWrapper.concat([NeedsQuery], extraBindings));
var query = inj.get(NeedsQuery).query;
query.add(new CountingDirective()); // this marks the query as dirty
query.onChange(() => async.done());
inj.onAllChangesDone();
}));
it("should not notify inherited queries", inject([AsyncTestCompleter], (async) => {
var child = parentChildInjectors(ListWrapper.concat([NeedsQuery], extraBindings), []);
var query = child.parent.get(NeedsQuery).query;
var calledOnChange = false;
query.onChange(() => {
// make sure the callback is called only once
expect(calledOnChange).toEqual(false);
expect(query.length).toEqual(2);
calledOnChange = true;
async.done()
});
query.add(new CountingDirective());
child.onAllChangesDone(); // this does not notify the query
query.add(new CountingDirective());
child.parent.onAllChangesDone();
}));
});
describe("dynamicallyCreateComponent", () => {

View File

@ -18,6 +18,7 @@ import {QueryList} from 'angular2/core';
import {Query, Component, Directive, View} from 'angular2/annotations';
import {NgIf, NgFor} from 'angular2/angular2';
import {ListWrapper} from 'angular2/src/facade/collection';
import {BrowserDomAdapter} from 'angular2/src/dom/browser_adapter';
@ -120,6 +121,55 @@ export function main() {
async.done();
});
}));
it('should notify query on change',
inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => {
var template = '<needs-query-desc #q>' +
'<div text="1"></div>' +
'<div *ng-if="shouldShow" text="2"></div>' +
'</needs-query-desc>';
tb.createView(MyComp, {html: template})
.then((view) => {
var q = view.rawView.locals.get("q");
view.detectChanges();
q.query.onChange(() => {
expect(q.query.first.text).toEqual("1");
expect(q.query.last.text).toEqual("2");
async.done();
});
view.context.shouldShow = true;
view.detectChanges();
});
}));
it("should notify child's query before notifying parent's query",
inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => {
var template = '<needs-query-desc #q1>' +
'<needs-query-desc #q2>' +
'<div text="1"></div>' +
'</needs-query-desc>' +
'</needs-query-desc>';
tb.createView(MyComp, {html: template})
.then((view) => {
var q1 = view.rawView.locals.get("q1");
var q2 = view.rawView.locals.get("q2");
var firedQ2 = false;
q2.query.onChange(() => { firedQ2 = true; });
q1.query.onChange(() => {
expect(firedQ2).toBe(true);
async.done();
});
view.detectChanges();
});
}));
});
}

View File

@ -51,7 +51,9 @@ class _MyComponent_ChangeDetector0 extends _gen.AbstractChangeDetector {
_alreadyChecked = true;
}
void callOnAllChangesDone() {}
void callOnAllChangesDone() {
_dispatcher.notifyOnAllChangesDone();
}
void hydrate(MyComponent context, locals, directives) {
mode = 'ALWAYS_CHECK';