2014-10-02 09:14:32 -04:00
|
|
|
import {describe, it, xit, expect} from 'test_lib/test_lib';
|
|
|
|
|
|
|
|
import {List, ListWrapper} from 'facade/collection';
|
2014-10-28 12:22:38 -04:00
|
|
|
import {ImplicitReceiver, FieldRead} from 'change_detection/parser/ast';
|
|
|
|
import {ClosureMap} from 'change_detection/parser/closure_map';
|
2014-10-02 09:14:32 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetector,
|
|
|
|
ProtoWatchGroup,
|
|
|
|
WatchGroup,
|
2014-10-28 12:22:38 -04:00
|
|
|
WatchGroupDispatcher,
|
|
|
|
ProtoRecord
|
2014-10-02 09:14:32 -04:00
|
|
|
} from 'change_detection/change_detector';
|
|
|
|
|
|
|
|
import {Record} from 'change_detection/record';
|
|
|
|
|
|
|
|
export function main() {
|
2014-10-28 12:22:38 -04:00
|
|
|
function ast(exp:string) {
|
|
|
|
var parts = exp.split(".");
|
|
|
|
var cm = new ClosureMap();
|
|
|
|
return ListWrapper.reduce(parts, function (ast, fieldName) {
|
|
|
|
return new FieldRead(ast, fieldName, cm.getter(fieldName));
|
|
|
|
}, new ImplicitReceiver());
|
|
|
|
}
|
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
describe('change_detection', function() {
|
|
|
|
describe('ChangeDetection', function() {
|
|
|
|
it('should do simple watching', function() {
|
|
|
|
var person = new Person('misko', 38);
|
|
|
|
var pwg = new ProtoWatchGroup();
|
2014-10-28 12:22:38 -04:00
|
|
|
pwg.watch(ast('name'), 'name');
|
|
|
|
pwg.watch(ast('age'), 'age');
|
2014-10-02 09:14:32 -04:00
|
|
|
var dispatcher = new LoggingDispatcher();
|
|
|
|
var wg = pwg.instantiate(dispatcher);
|
|
|
|
wg.setContext(person);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
var cd = new ChangeDetector(wg);
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['name=misko', 'age=38']);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
dispatcher.clear();
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual([]);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
person.age = 1;
|
|
|
|
person.name = "Misko";
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['name=Misko', 'age=1']);
|
|
|
|
});
|
2014-10-27 12:57:36 -04:00
|
|
|
|
|
|
|
it('should watch chained properties', function() {
|
|
|
|
var address = new Address('Grenoble');
|
|
|
|
var person = new Person('Victor', 36, address);
|
|
|
|
var pwg = new ProtoWatchGroup();
|
2014-10-28 12:22:38 -04:00
|
|
|
pwg.watch(ast('address.city'), 'address.city', false);
|
2014-10-27 12:57:36 -04:00
|
|
|
var dispatcher = new LoggingDispatcher();
|
|
|
|
var wg = pwg.instantiate(dispatcher);
|
|
|
|
wg.setContext(person);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
var cd = new ChangeDetector(wg);
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['address.city=Grenoble']);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
dispatcher.clear();
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual([]);
|
2014-10-28 12:22:38 -04:00
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
address.city = 'Mountain View';
|
|
|
|
cd.detectChanges();
|
|
|
|
expect(dispatcher.log).toEqual(['address.city=Mountain View']);
|
|
|
|
});
|
|
|
|
|
2014-10-02 09:14:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class Person {
|
2014-10-27 12:57:36 -04:00
|
|
|
constructor(name:string, age:number, address:Address = null) {
|
2014-10-02 09:14:32 -04:00
|
|
|
this.name = name;
|
|
|
|
this.age = age;
|
2014-10-27 12:57:36 -04:00
|
|
|
this.address = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString():string {
|
|
|
|
var address = this.address == null ? '' : ' address=' + this.address.toString();
|
|
|
|
|
|
|
|
return 'name=' + this.name +
|
|
|
|
' age=' + this.age.toString() +
|
|
|
|
address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Address {
|
|
|
|
constructor(city:string) {
|
|
|
|
this.city = city;
|
2014-10-02 09:14:32 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 12:57:36 -04:00
|
|
|
toString():string {
|
|
|
|
return this.city;
|
2014-10-02 09:14:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LoggingDispatcher extends WatchGroupDispatcher {
|
|
|
|
constructor() {
|
|
|
|
this.log = null;
|
|
|
|
this.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.log = ListWrapper.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
onRecordChange(record:Record, context) {
|
|
|
|
ListWrapper.push(this.log, context + '=' + record.currentValue.toString());
|
|
|
|
}
|
|
|
|
}
|