feat(change_detection): reimplement change detection
This commit is contained in:
477
modules/change_detection/test/change_detection_spec.js
Normal file
477
modules/change_detection/test/change_detection_spec.js
Normal file
@ -0,0 +1,477 @@
|
||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'test_lib/test_lib';
|
||||
|
||||
import {isPresent, isBlank, isJsObject, BaseException, FunctionWrapper} from 'facade/lang';
|
||||
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'facade/collection';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {reflector} from 'reflection/reflection';
|
||||
import {arrayChangesAsString, kvChangesAsString} from './util';
|
||||
|
||||
import {ProtoChangeDetector, ChangeDispatcher, DynamicChangeDetector, ChangeDetectionError,
|
||||
ContextWithVariableBindings}
|
||||
from 'change_detection/change_detection';
|
||||
|
||||
|
||||
export function main() {
|
||||
function ast(exp:string, location:string = 'location') {
|
||||
var parser = new Parser(new Lexer());
|
||||
return parser.parseBinding(exp, location);
|
||||
}
|
||||
|
||||
function createChangeDetector(memo:string, exp:string, context = null, formatters = null,
|
||||
structural = false) {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast(exp), memo, memo, structural);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var cd = pcd.instantiate(dispatcher, formatters);
|
||||
cd.setContext(context);
|
||||
|
||||
return {"changeDetector" : cd, "dispatcher" : dispatcher};
|
||||
}
|
||||
|
||||
function executeWatch(memo:string, exp:string, context = null, formatters = null,
|
||||
content = false) {
|
||||
var res = createChangeDetector(memo, exp, context, formatters, content);
|
||||
res["changeDetector"].detectChanges();
|
||||
return res["dispatcher"].log;
|
||||
}
|
||||
|
||||
describe('change_detection', () => {
|
||||
it('should do simple watching', () => {
|
||||
var person = new Person("misko");
|
||||
var c = createChangeDetector('name', 'name', person);
|
||||
var cd = c["changeDetector"];
|
||||
var dispatcher = c["dispatcher"];
|
||||
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['name=misko']);
|
||||
|
||||
dispatcher.clear();
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual([]);
|
||||
|
||||
person.name = "Misko";
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['name=Misko']);
|
||||
});
|
||||
|
||||
it("should support literals", () => {
|
||||
expect(executeWatch('const', '10')).toEqual(['const=10']);
|
||||
expect(executeWatch('const', '"str"')).toEqual(['const=str']);
|
||||
});
|
||||
|
||||
it('simple chained property access', () => {
|
||||
var address = new Address('Grenoble');
|
||||
var person = new Person('Victor', address);
|
||||
|
||||
expect(executeWatch('address.city', 'address.city', person))
|
||||
.toEqual(['address.city=Grenoble']);
|
||||
});
|
||||
|
||||
it("should support method calls", () => {
|
||||
var person = new Person('Victor');
|
||||
expect(executeWatch('m', 'sayHi("Jim")', person)).toEqual(['m=Hi, Jim']);
|
||||
});
|
||||
|
||||
it("should support function calls", () => {
|
||||
var td = new TestData(() => (a) => a);
|
||||
expect(executeWatch('value', 'a()(99)', td)).toEqual(['value=99']);
|
||||
});
|
||||
|
||||
it("should support chained method calls", () => {
|
||||
var person = new Person('Victor');
|
||||
var td = new TestData(person);
|
||||
expect(executeWatch('m', 'a.sayHi("Jim")', td)).toEqual(['m=Hi, Jim']);
|
||||
});
|
||||
|
||||
it("should support literal array", () => {
|
||||
var c = createChangeDetector('array', '[1,2]');
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues).toEqual([[[1,2]]]);
|
||||
|
||||
c = createChangeDetector('array', '[1,a]', new TestData(2));
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues).toEqual([[[1,2]]]);
|
||||
});
|
||||
|
||||
it("should support literal maps", () => {
|
||||
var c = createChangeDetector('map', '{z:1}');
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues[0][0]['z']).toEqual(1);
|
||||
|
||||
c = createChangeDetector('map', '{z:a}', new TestData(1));
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues[0][0]['z']).toEqual(1);
|
||||
});
|
||||
|
||||
it("should support binary operations", () => {
|
||||
expect(executeWatch('exp', '10 + 2')).toEqual(['exp=12']);
|
||||
expect(executeWatch('exp', '10 - 2')).toEqual(['exp=8']);
|
||||
|
||||
expect(executeWatch('exp', '10 * 2')).toEqual(['exp=20']);
|
||||
expect(executeWatch('exp', '10 / 2')).toEqual([`exp=${5.0}`]); //dart exp=5.0, js exp=5
|
||||
expect(executeWatch('exp', '11 % 2')).toEqual(['exp=1']);
|
||||
|
||||
expect(executeWatch('exp', '1 == 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '1 != 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '1 < 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '2 > 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '1 <= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 <= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 <= 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '2 >= 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 >= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '1 >= 2')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', 'true && true')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', 'true && false')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', 'true || false')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', 'false || false')).toEqual(['exp=false']);
|
||||
});
|
||||
|
||||
it("should support negate", () => {
|
||||
expect(executeWatch('exp', '!true')).toEqual(['exp=false']);
|
||||
expect(executeWatch('exp', '!!true')).toEqual(['exp=true']);
|
||||
});
|
||||
|
||||
it("should support conditionals", () => {
|
||||
expect(executeWatch('m', '1 < 2 ? 1 : 2')).toEqual(['m=1']);
|
||||
expect(executeWatch('m', '1 > 2 ? 1 : 2')).toEqual(['m=2']);
|
||||
});
|
||||
|
||||
describe("keyed access", () => {
|
||||
it("should support accessing a list item", () => {
|
||||
expect(executeWatch('array[0]', '["foo", "bar"][0]')).toEqual(['array[0]=foo']);
|
||||
});
|
||||
|
||||
it("should support accessing a map item", () => {
|
||||
expect(executeWatch('map[foo]', '{"foo": "bar"}["foo"]')).toEqual(['map[foo]=bar']);
|
||||
});
|
||||
});
|
||||
|
||||
it("should support formatters", () => {
|
||||
var formatters = MapWrapper.createFromPairs([
|
||||
['uppercase', (v) => v.toUpperCase()],
|
||||
['wrap', (v, before, after) => `${before}${v}${after}`]]);
|
||||
expect(executeWatch('str', '"aBc" | uppercase', null, formatters)).toEqual(['str=ABC']);
|
||||
expect(executeWatch('str', '"b" | wrap:"a":"c"', null, formatters)).toEqual(['str=abc']);
|
||||
});
|
||||
|
||||
describe("group changes", () => {
|
||||
it("should notify the dispatcher when a group of records changes", () => {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast("1 + 2"), "memo", 1);
|
||||
pcd.addAst(ast("10 + 20"), "memo", 1);
|
||||
pcd.addAst(ast("100 + 200"), "memo2", 2);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var cd = pcd.instantiate(dispatcher, null);
|
||||
|
||||
cd.detectChanges();
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual([[3, 30], [300]]);
|
||||
});
|
||||
|
||||
it("should update every instance of a group individually", () => {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast("1 + 2"), "memo", "memo");
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var cd = new DynamicChangeDetector(dispatcher, null, []);
|
||||
cd.addChild(pcd.instantiate(dispatcher, null));
|
||||
cd.addChild(pcd.instantiate(dispatcher, null));
|
||||
|
||||
cd.detectChanges();
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual([[3], [3]]);
|
||||
});
|
||||
|
||||
it("should notify the dispatcher before switching to the next group", () => {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast("a()"), "a", 1);
|
||||
pcd.addAst(ast("b()"), "b", 2);
|
||||
pcd.addAst(ast("c()"), "c", 2);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var cd = pcd.instantiate(dispatcher, null);
|
||||
|
||||
var tr = new TestRecord();
|
||||
tr.a = () => {dispatcher.logValue('InvokeA'); return 'a'};
|
||||
tr.b = () => {dispatcher.logValue('InvokeB'); return 'b'};
|
||||
tr.c = () => {dispatcher.logValue('InvokeC'); return 'c'};
|
||||
cd.setContext(tr);
|
||||
|
||||
cd.detectChanges();
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual(['InvokeA', ['a'], 'InvokeB', 'InvokeC', ['b', 'c']]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("enforce no new changes", () => {
|
||||
it("should throw when a record gets changed after it has been checked", () => {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast("a"), "a", 1);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var cd = pcd.instantiate(dispatcher, null);
|
||||
cd.setContext(new TestData('value'));
|
||||
|
||||
expect(() => {
|
||||
cd.checkNoChanges();
|
||||
}).toThrowError(new RegExp("Expression 'a in location' has changed after it was checked"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("error handling", () => {
|
||||
it("should wrap exceptions into ChangeDetectionError", () => {
|
||||
var pcd = new ProtoChangeDetector();
|
||||
pcd.addAst(ast('invalidProp', 'someComponent'), "a", 1);
|
||||
|
||||
var cd = pcd.instantiate(new TestDispatcher(), null);
|
||||
|
||||
try {
|
||||
cd.detectChanges();
|
||||
|
||||
throw new BaseException("fail");
|
||||
} catch (e) {
|
||||
expect(e).toBeAnInstanceOf(ChangeDetectionError);
|
||||
expect(e.location).toEqual("invalidProp in someComponent");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("collections", () => {
|
||||
it("should support null values", () => {
|
||||
var context = new TestData(null);
|
||||
|
||||
var c = createChangeDetector('a', 'a', context, null, true);
|
||||
var cd = c["changeDetector"];
|
||||
var dispatcher = c["dispatcher"];
|
||||
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['a=null']);
|
||||
dispatcher.clear();
|
||||
|
||||
//cd.detectChanges();
|
||||
//expect(dispatcher.log).toEqual([]);
|
||||
|
||||
context.a = [0];
|
||||
cd.detectChanges();
|
||||
|
||||
expect(dispatcher.log).toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['0[null->0]'],
|
||||
additions: ['0[null->0]']
|
||||
})
|
||||
]);
|
||||
dispatcher.clear();
|
||||
|
||||
context.a = null;
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['a=null']);
|
||||
});
|
||||
|
||||
describe("list", () => {
|
||||
it("should support list changes", () => {
|
||||
var context = new TestData([1, 2]);
|
||||
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['1[null->0]', '2[null->1]'],
|
||||
additions: ['1[null->0]', '2[null->1]']
|
||||
})]);
|
||||
});
|
||||
|
||||
it("should handle reference changes", () => {
|
||||
var context = new TestData([1, 2]);
|
||||
var objs = createChangeDetector("a", "a", context, null, true);
|
||||
var cd = objs["changeDetector"];
|
||||
var dispatcher = objs["dispatcher"];
|
||||
cd.detectChanges();
|
||||
dispatcher.clear();
|
||||
|
||||
context.a = [2, 1];
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['2[1->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]', '2[1->0]'],
|
||||
moves: ['2[1->0]', '1[0->1]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("map", () => {
|
||||
it("should support map changes", () => {
|
||||
var map = MapWrapper.create();
|
||||
MapWrapper.set(map, "foo", "bar");
|
||||
var context = new TestData(map);
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['foo[null->bar]'],
|
||||
additions: ['foo[null->bar]']
|
||||
})]);
|
||||
});
|
||||
|
||||
it("should handle reference changes", () => {
|
||||
var map = MapWrapper.create();
|
||||
MapWrapper.set(map, "foo", "bar");
|
||||
var context = new TestData(map);
|
||||
var objs = createChangeDetector("a", "a", context, null, true);
|
||||
var cd = objs["changeDetector"];
|
||||
var dispatcher = objs["dispatcher"];
|
||||
cd.detectChanges();
|
||||
dispatcher.clear();
|
||||
|
||||
context.a = MapWrapper.create();
|
||||
MapWrapper.set(context.a, "bar", "foo");
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['bar[null->foo]'],
|
||||
previous: ['foo[bar->null]'],
|
||||
additions: ['bar[null->foo]'],
|
||||
removals: ['foo[bar->null]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
|
||||
if (isJsObject({})) {
|
||||
describe("js objects", () => {
|
||||
it("should support object changes", () => {
|
||||
var map = {"foo": "bar"};
|
||||
var context = new TestData(map);
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['foo[null->bar]'],
|
||||
additions: ['foo[null->bar]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("ContextWithVariableBindings", () => {
|
||||
it('should read a field from ContextWithVariableBindings', () => {
|
||||
var locals = new ContextWithVariableBindings(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
|
||||
expect(executeWatch('key', 'key', locals))
|
||||
.toEqual(['key=value']);
|
||||
});
|
||||
|
||||
it('should handle nested ContextWithVariableBindings', () => {
|
||||
var nested = new ContextWithVariableBindings(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
var locals = new ContextWithVariableBindings(nested, MapWrapper.create());
|
||||
|
||||
expect(executeWatch('key', 'key', locals))
|
||||
.toEqual(['key=value']);
|
||||
});
|
||||
|
||||
it("should fall back to a regular field read when ContextWithVariableBindings " +
|
||||
"does not have the requested field", () => {
|
||||
var locals = new ContextWithVariableBindings(new Person("Jim"),
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
|
||||
expect(executeWatch('name', 'name', locals))
|
||||
.toEqual(['name=Jim']);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class TestRecord {
|
||||
a;
|
||||
b;
|
||||
c;
|
||||
}
|
||||
|
||||
class Person {
|
||||
name:string;
|
||||
age:number;
|
||||
address:Address;
|
||||
constructor(name:string, address:Address = null) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
sayHi(m) {
|
||||
return `Hi, ${m}`;
|
||||
}
|
||||
|
||||
toString():string {
|
||||
var address = this.address == null ? '' : ' address=' + this.address.toString();
|
||||
|
||||
return 'name=' + this.name + address;
|
||||
}
|
||||
}
|
||||
|
||||
class Address {
|
||||
city:string;
|
||||
constructor(city:string) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
toString():string {
|
||||
return this.city;
|
||||
}
|
||||
}
|
||||
|
||||
class TestData {
|
||||
a;
|
||||
|
||||
constructor(a) {
|
||||
this.a = a;
|
||||
}
|
||||
}
|
||||
|
||||
class TestDispatcher extends ChangeDispatcher {
|
||||
log:List;
|
||||
loggedValues:List;
|
||||
onChange:Function;
|
||||
|
||||
constructor() {
|
||||
this.log = null;
|
||||
this.loggedValues = null;
|
||||
this.onChange = (_, __) => {};
|
||||
this.clear();
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.log = ListWrapper.create();
|
||||
this.loggedValues = ListWrapper.create();
|
||||
}
|
||||
|
||||
logValue(value) {
|
||||
ListWrapper.push(this.loggedValues, value);
|
||||
}
|
||||
|
||||
onRecordChange(group, updates:List) {
|
||||
var value = updates[0].change.currentValue;
|
||||
var memento = updates[0].bindingMemento;
|
||||
ListWrapper.push(this.log, memento + '=' + this._asString(value));
|
||||
|
||||
var values = ListWrapper.map(updates, (r) => r.change.currentValue);
|
||||
ListWrapper.push(this.loggedValues, values);
|
||||
|
||||
this.onChange(group, updates);
|
||||
}
|
||||
|
||||
|
||||
_asString(value) {
|
||||
return (isBlank(value) ? 'null' : value.toString());
|
||||
}
|
||||
}
|
@ -1,555 +0,0 @@
|
||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
|
||||
|
||||
import {isPresent, isBlank, isJsObject, BaseException} from 'facade/lang';
|
||||
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ContextWithVariableBindings} from 'change_detection/parser/context_with_variable_bindings';
|
||||
import {arrayChangesAsString, kvChangesAsString} from './util';
|
||||
|
||||
import {
|
||||
ChangeDetector,
|
||||
ProtoRecordRange,
|
||||
RecordRange,
|
||||
ChangeDispatcher,
|
||||
ProtoRecord,
|
||||
ChangeDetectionError
|
||||
} from 'change_detection/change_detector';
|
||||
|
||||
import {Record} from 'change_detection/record';
|
||||
|
||||
export function main() {
|
||||
function ast(exp:string, location:string = 'location') {
|
||||
var parser = new Parser(new Lexer());
|
||||
return parser.parseBinding(exp, location);
|
||||
}
|
||||
|
||||
function createChangeDetector(memo:string, exp:string, context = null, formatters = null,
|
||||
content = false) {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast(exp), memo, memo, content);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var rr = prr.instantiate(dispatcher, formatters);
|
||||
rr.setContext(context);
|
||||
|
||||
var cd = new ChangeDetector(rr);
|
||||
|
||||
return {"changeDetector" : cd, "dispatcher" : dispatcher};
|
||||
}
|
||||
|
||||
function executeWatch(memo:string, exp:string, context = null, formatters = null,
|
||||
content = false) {
|
||||
var res = createChangeDetector(memo, exp, context, formatters, content);
|
||||
res["changeDetector"].detectChanges();
|
||||
return res["dispatcher"].log;
|
||||
}
|
||||
|
||||
describe('change_detection', () => {
|
||||
describe('ChangeDetection', () => {
|
||||
function createRange(dispatcher, ast, group) {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast, "memo", group);
|
||||
return prr.instantiate(dispatcher, null);
|
||||
}
|
||||
|
||||
function detectChangesInRange(recordRange) {
|
||||
var cd = new ChangeDetector(recordRange);
|
||||
cd.detectChanges();
|
||||
}
|
||||
|
||||
it('should do simple watching', () => {
|
||||
var person = new Person("misko");
|
||||
|
||||
var c = createChangeDetector('name', 'name', person);
|
||||
var cd = c["changeDetector"];
|
||||
var dispatcher = c["dispatcher"];
|
||||
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['name=misko']);
|
||||
|
||||
dispatcher.clear();
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual([]);
|
||||
|
||||
person.name = "Misko";
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(['name=Misko']);
|
||||
});
|
||||
|
||||
it('should support chained properties', () => {
|
||||
var address = new Address('Grenoble');
|
||||
var person = new Person('Victor', address);
|
||||
|
||||
expect(executeWatch('address.city', 'address.city', person))
|
||||
.toEqual(['address.city=Grenoble']);
|
||||
});
|
||||
|
||||
it("should support method calls", () => {
|
||||
var person = new Person('Victor');
|
||||
expect(executeWatch('m', 'sayHi("Jim")', person)).toEqual(['m=Hi, Jim']);
|
||||
});
|
||||
|
||||
it("should support function calls", () => {
|
||||
var td = new TestData(() => (a) => a);
|
||||
expect(executeWatch('value', 'a()(99)', td)).toEqual(['value=99']);
|
||||
});
|
||||
|
||||
it("should support chained method calls", () => {
|
||||
var person = new Person('Victor');
|
||||
var td = new TestData(person);
|
||||
expect(executeWatch('m', 'a.sayHi("Jim")', td)).toEqual(['m=Hi, Jim']);
|
||||
});
|
||||
|
||||
it("should support literals", () => {
|
||||
expect(executeWatch('const', '10')).toEqual(['const=10']);
|
||||
expect(executeWatch('const', '"str"')).toEqual(['const=str']);
|
||||
});
|
||||
|
||||
it("should support literal array", () => {
|
||||
var c = createChangeDetector('array', '[1,2]');
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues).toEqual([[[1,2]]]);
|
||||
|
||||
c = createChangeDetector('array', '[1,a]', new TestData(2));
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues).toEqual([[[1,2]]]);
|
||||
});
|
||||
|
||||
it("should support literal maps", () => {
|
||||
var c = createChangeDetector('map', '{z:1}');
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues[0][0]['z']).toEqual(1);
|
||||
|
||||
c = createChangeDetector('map', '{z:a}', new TestData(1));
|
||||
c["changeDetector"].detectChanges();
|
||||
expect(c["dispatcher"].loggedValues[0][0]['z']).toEqual(1);
|
||||
});
|
||||
|
||||
it("should support binary operations", () => {
|
||||
expect(executeWatch('exp', '10 + 2')).toEqual(['exp=12']);
|
||||
expect(executeWatch('exp', '10 - 2')).toEqual(['exp=8']);
|
||||
|
||||
expect(executeWatch('exp', '10 * 2')).toEqual(['exp=20']);
|
||||
expect(executeWatch('exp', '10 / 2')).toEqual([`exp=${5.0}`]); //dart exp=5.0, js exp=5
|
||||
expect(executeWatch('exp', '11 % 2')).toEqual(['exp=1']);
|
||||
|
||||
expect(executeWatch('exp', '1 == 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '1 != 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '1 < 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '2 > 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 < 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '1 <= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 <= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 <= 1')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', '2 >= 1')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '2 >= 2')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', '1 >= 2')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', 'true && true')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', 'true && false')).toEqual(['exp=false']);
|
||||
|
||||
expect(executeWatch('exp', 'true || false')).toEqual(['exp=true']);
|
||||
expect(executeWatch('exp', 'false || false')).toEqual(['exp=false']);
|
||||
});
|
||||
|
||||
it("should support negate", () => {
|
||||
expect(executeWatch('exp', '!true')).toEqual(['exp=false']);
|
||||
expect(executeWatch('exp', '!!true')).toEqual(['exp=true']);
|
||||
});
|
||||
|
||||
it("should support conditionals", () => {
|
||||
expect(executeWatch('m', '1 < 2 ? 1 : 2')).toEqual(['m=1']);
|
||||
expect(executeWatch('m', '1 > 2 ? 1 : 2')).toEqual(['m=2']);
|
||||
});
|
||||
|
||||
describe("keyed access", () => {
|
||||
it("should support accessing a list item", () => {
|
||||
expect(executeWatch('array[0]', '["foo", "bar"][0]')).toEqual(['array[0]=foo']);
|
||||
});
|
||||
it("should support accessing a map item", () => {
|
||||
expect(executeWatch('map[foo]', '{"foo": "bar"}["foo"]')).toEqual(['map[foo]=bar']);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatters", () => {
|
||||
it("should support formatters", () => {
|
||||
var formatters = MapWrapper.createFromPairs([
|
||||
['uppercase', (v) => v.toUpperCase()],
|
||||
['wrap', (v, before, after) => `${before}${v}${after}`]]);
|
||||
expect(executeWatch('str', '"aBc" | uppercase', null, formatters)).toEqual(['str=ABC']);
|
||||
expect(executeWatch('str', '"b" | wrap:"a":"c"', null, formatters)).toEqual(['str=abc']);
|
||||
});
|
||||
|
||||
it("should rerun formatters only when arguments change", () => {
|
||||
var counter = 0;
|
||||
var formatters = MapWrapper.createFromPairs([
|
||||
['formatter', (_) => {counter += 1; return 'value'}]
|
||||
]);
|
||||
|
||||
var person = new Person('Jim');
|
||||
|
||||
var c = createChangeDetector('formatter', 'name | formatter', person, formatters);
|
||||
var cd = c['changeDetector'];
|
||||
|
||||
cd.detectChanges();
|
||||
expect(counter).toEqual(1);
|
||||
|
||||
cd.detectChanges();
|
||||
expect(counter).toEqual(1);
|
||||
|
||||
person.name = 'bob';
|
||||
cd.detectChanges();
|
||||
expect(counter).toEqual(2);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("ContextWithVariableBindings", () => {
|
||||
it('should read a field from ContextWithVariableBindings', () => {
|
||||
var locals = new ContextWithVariableBindings(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
|
||||
expect(executeWatch('key', 'key', locals))
|
||||
.toEqual(['key=value']);
|
||||
});
|
||||
|
||||
it('should handle nested ContextWithVariableBindings', () => {
|
||||
var nested = new ContextWithVariableBindings(null,
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
var locals = new ContextWithVariableBindings(nested, MapWrapper.create());
|
||||
|
||||
expect(executeWatch('key', 'key', locals))
|
||||
.toEqual(['key=value']);
|
||||
});
|
||||
|
||||
it("should fall back to a regular field read when ContextWithVariableBindings " +
|
||||
"does not have the requested field", () => {
|
||||
var locals = new ContextWithVariableBindings(new Person("Jim"),
|
||||
MapWrapper.createFromPairs([["key", "value"]]));
|
||||
|
||||
expect(executeWatch('name', 'name', locals))
|
||||
.toEqual(['name=Jim']);
|
||||
});
|
||||
});
|
||||
|
||||
describe("collections", () => {
|
||||
it("should support null values", () => {
|
||||
var context = new TestData(null);
|
||||
var c = createChangeDetector('a', 'a', context, null, true);
|
||||
var cd = c["changeDetector"];
|
||||
var dsp = c["dispatcher"];
|
||||
|
||||
cd.detectChanges();
|
||||
expect(dsp.log).toEqual(['a=null']);
|
||||
dsp.clear();
|
||||
|
||||
cd.detectChanges();
|
||||
expect(dsp.log).toEqual([]);
|
||||
|
||||
context.a = [0];
|
||||
cd.detectChanges();
|
||||
|
||||
expect(dsp.log).toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['0[null->0]'],
|
||||
additions: ['0[null->0]']
|
||||
})
|
||||
]);
|
||||
dsp.clear();
|
||||
|
||||
context.a = null;
|
||||
cd.detectChanges();
|
||||
expect(dsp.log).toEqual(['a=null']);
|
||||
});
|
||||
|
||||
it("should throw if not collection / null", () => {
|
||||
var context = new TestData("not collection / null");
|
||||
var c = createChangeDetector('a', 'a', context, null, true);
|
||||
expect(() => c["changeDetector"].detectChanges())
|
||||
.toThrowError(new RegExp("Collection records must be array like, map like or null"));
|
||||
});
|
||||
|
||||
describe("list", () => {
|
||||
it("should support list changes", () => {
|
||||
var context = new TestData([1, 2]);
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['1[null->0]', '2[null->1]'],
|
||||
additions: ['1[null->0]', '2[null->1]']
|
||||
})]);
|
||||
});
|
||||
|
||||
it("should handle reference changes", () => {
|
||||
var context = new TestData([1, 2]);
|
||||
var objs = createChangeDetector("a", "a", context, null, true);
|
||||
var cd = objs["changeDetector"];
|
||||
var dispatcher = objs["dispatcher"];
|
||||
cd.detectChanges();
|
||||
dispatcher.clear();
|
||||
|
||||
context.a = [2, 1];
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(["a=" +
|
||||
arrayChangesAsString({
|
||||
collection: ['2[1->0]', '1[0->1]'],
|
||||
previous: ['1[0->1]', '2[1->0]'],
|
||||
moves: ['2[1->0]', '1[0->1]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("map", () => {
|
||||
it("should support map changes", () => {
|
||||
var map = MapWrapper.create();
|
||||
MapWrapper.set(map, "foo", "bar");
|
||||
var context = new TestData(map);
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['foo[null->bar]'],
|
||||
additions: ['foo[null->bar]']
|
||||
})]);
|
||||
});
|
||||
|
||||
it("should handle reference changes", () => {
|
||||
var map = MapWrapper.create();
|
||||
MapWrapper.set(map, "foo", "bar");
|
||||
var context = new TestData(map);
|
||||
var objs = createChangeDetector("a", "a", context, null, true);
|
||||
var cd = objs["changeDetector"];
|
||||
var dispatcher = objs["dispatcher"];
|
||||
cd.detectChanges();
|
||||
dispatcher.clear();
|
||||
|
||||
context.a = MapWrapper.create();
|
||||
MapWrapper.set(context.a, "bar", "foo");
|
||||
cd.detectChanges();
|
||||
expect(dispatcher.log).toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['bar[null->foo]'],
|
||||
previous: ['foo[bar->null]'],
|
||||
additions: ['bar[null->foo]'],
|
||||
removals: ['foo[bar->null]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
|
||||
if (isJsObject({})) {
|
||||
describe("js objects", () => {
|
||||
it("should support object changes", () => {
|
||||
var map = {"foo": "bar"};
|
||||
var context = new TestData(map);
|
||||
expect(executeWatch("a", "a", context, null, true))
|
||||
.toEqual(["a=" +
|
||||
kvChangesAsString({
|
||||
map: ['foo[null->bar]'],
|
||||
additions: ['foo[null->bar]']
|
||||
})]);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("adding new ranges", () => {
|
||||
var dispatcher;
|
||||
|
||||
beforeEach(() => {
|
||||
dispatcher = new TestDispatcher();
|
||||
});
|
||||
|
||||
/**
|
||||
* Tests that we can add a new range after the current
|
||||
* record has been disabled. The new range must be processed
|
||||
* during the same change detection run.
|
||||
*/
|
||||
it("should work when disabling the last enabled record", () => {
|
||||
var rr = createRange(dispatcher, ast("1"), 1);
|
||||
|
||||
dispatcher.onChange = (group, _) => {
|
||||
if (group === 1) { // to prevent infinite loop
|
||||
var rangeToAppend = createRange(dispatcher, ast("2"), 2);
|
||||
rr.addRange(rangeToAppend);
|
||||
}
|
||||
};
|
||||
|
||||
detectChangesInRange(rr);
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual([[1], [2]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("group changes", () => {
|
||||
it("should notify the dispatcher when a group of records changes", () => {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast("1 + 2"), "memo", 1);
|
||||
prr.addRecordsFromAST(ast("10 + 20"), "memo", 1);
|
||||
prr.addRecordsFromAST(ast("100 + 200"), "memo2", 2);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var rr = prr.instantiate(dispatcher, null);
|
||||
|
||||
detectChangesInRange(rr);
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual([[3, 30], [300]]);
|
||||
});
|
||||
|
||||
it("should update every instance of a group individually", () => {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast("1 + 2"), "memo", "memo");
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var rr = new RecordRange(null, dispatcher);
|
||||
rr.addRange(prr.instantiate(dispatcher, null));
|
||||
rr.addRange(prr.instantiate(dispatcher, null));
|
||||
|
||||
detectChangesInRange(rr);
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual([[3], [3]]);
|
||||
});
|
||||
|
||||
it("should notify the dispatcher before switching to the next group", () => {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast("a()"), "a", 1);
|
||||
prr.addRecordsFromAST(ast("b()"), "b", 2);
|
||||
prr.addRecordsFromAST(ast("c()"), "c", 2);
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var rr = prr.instantiate(dispatcher, null);
|
||||
|
||||
var tr = new TestRecord();
|
||||
tr.a = () => {dispatcher.logValue('InvokeA'); return 'a'};
|
||||
tr.b = () => {dispatcher.logValue('InvokeB'); return 'b'};
|
||||
tr.c = () => {dispatcher.logValue('InvokeC'); return 'c'};
|
||||
rr.setContext(tr);
|
||||
|
||||
detectChangesInRange(rr);
|
||||
|
||||
expect(dispatcher.loggedValues).toEqual(['InvokeA', ['a'], 'InvokeB', 'InvokeC', ['b', 'c']]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("enforce no new changes", () => {
|
||||
it("should throw when a record gets changed after it has been checked", () => {
|
||||
var prr = new ProtoRecordRange();
|
||||
prr.addRecordsFromAST(ast("a"), "a", 1);
|
||||
prr.addRecordsFromAST(ast("b()"), "b", 2);
|
||||
|
||||
var tr = new TestRecord();
|
||||
tr.a = "a";
|
||||
tr.b = () => {tr.a = "newA";};
|
||||
|
||||
var dispatcher = new TestDispatcher();
|
||||
var rr = prr.instantiate(dispatcher, null);
|
||||
rr.setContext(tr);
|
||||
|
||||
expect(() => {
|
||||
var cd = new ChangeDetector(rr, true);
|
||||
cd.detectChanges();
|
||||
}).toThrowError(new RegExp("Expression 'a in location' has changed after it was checked"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("error handling", () => {
|
||||
it("should wrap exceptions into ChangeDetectionError", () => {
|
||||
try {
|
||||
var rr = createRange(new TestDispatcher(), ast("invalidProp", "someComponent"), 1);
|
||||
detectChangesInRange(rr);
|
||||
|
||||
throw new BaseException("fail");
|
||||
} catch (e) {
|
||||
expect(e).toBeAnInstanceOf(ChangeDetectionError);
|
||||
expect(e.location).toEqual("invalidProp in someComponent");
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class TestRecord {
|
||||
a;
|
||||
b;
|
||||
c;
|
||||
}
|
||||
|
||||
class Person {
|
||||
name:string;
|
||||
address:Address;
|
||||
constructor(name:string, address:Address = null) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
sayHi(m) {
|
||||
return `Hi, ${m}`;
|
||||
}
|
||||
|
||||
toString():string {
|
||||
var address = this.address == null ? '' : ' address=' + this.address.toString();
|
||||
|
||||
return 'name=' + this.name + address;
|
||||
}
|
||||
}
|
||||
|
||||
class Address {
|
||||
city:string;
|
||||
constructor(city:string) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
toString():string {
|
||||
return this.city;
|
||||
}
|
||||
}
|
||||
|
||||
class TestData {
|
||||
a;
|
||||
constructor(a) {
|
||||
this.a = a;
|
||||
}
|
||||
}
|
||||
|
||||
class TestDispatcher extends ChangeDispatcher {
|
||||
log:List;
|
||||
loggedValues:List;
|
||||
onChange:Function;
|
||||
|
||||
constructor() {
|
||||
this.log = null;
|
||||
this.loggedValues = null;
|
||||
this.onChange = (_, __) => {};
|
||||
this.clear();
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.log = ListWrapper.create();
|
||||
this.loggedValues = ListWrapper.create();
|
||||
}
|
||||
|
||||
logValue(value) {
|
||||
ListWrapper.push(this.loggedValues, value);
|
||||
}
|
||||
|
||||
onRecordChange(group, records:List) {
|
||||
var value = records[0].currentValue;
|
||||
var dest = records[0].protoRecord.dest;
|
||||
ListWrapper.push(this.log, dest + '=' + this._asString(value));
|
||||
|
||||
var values = ListWrapper.map(records, (r) => r.currentValue);
|
||||
ListWrapper.push(this.loggedValues, values);
|
||||
|
||||
this.onChange(group, records);
|
||||
}
|
||||
|
||||
_asString(value) {
|
||||
return (isBlank(value) ? 'null' : value.toString());
|
||||
}
|
||||
}
|
@ -1,364 +0,0 @@
|
||||
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
||||
import {isPresent} from 'facade/lang';
|
||||
|
||||
import {
|
||||
ChangeDetector,
|
||||
ProtoRecordRange,
|
||||
RecordRange,
|
||||
ProtoRecord,
|
||||
RECORD_TYPE_CONST
|
||||
} from 'change_detection/change_detector';
|
||||
|
||||
import {Record} from 'change_detection/record';
|
||||
|
||||
export function main() {
|
||||
var lookupName = (names, item) =>
|
||||
ListWrapper.last(
|
||||
ListWrapper.find(names, (pair) => pair[0] === item));
|
||||
|
||||
function enabledRecordsInReverseOrder(rr:RecordRange, names:List) {
|
||||
var reversed = [];
|
||||
var record = rr.findLastEnabledRecord();
|
||||
while (isPresent(record)) {
|
||||
ListWrapper.push(reversed, lookupName(names, record));
|
||||
record = record.prevEnabled;
|
||||
}
|
||||
return reversed;
|
||||
}
|
||||
|
||||
function enabledRecords(rr:RecordRange, names:List) {
|
||||
var res = [];
|
||||
var record = rr.findFirstEnabledRecord();
|
||||
while (isPresent(record)) {
|
||||
ListWrapper.push(res, lookupName(names, record));
|
||||
record = record.nextEnabled;
|
||||
}
|
||||
|
||||
// check that all links are set properly in both directions
|
||||
var reversed = enabledRecordsInReverseOrder(rr, names);
|
||||
expect(res).toEqual(ListWrapper.reversed(reversed));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function createRecord(rr) {
|
||||
return new Record(rr, new ProtoRecord(null, 0, null, null, null, null, null, null), null);
|
||||
}
|
||||
|
||||
describe('record range', () => {
|
||||
it('should add records', () => {
|
||||
var rr = new RecordRange(null, null);
|
||||
var record1 = createRecord(rr);
|
||||
var record2 = createRecord(rr);
|
||||
|
||||
rr.addRecord(record1);
|
||||
rr.addRecord(record2);
|
||||
|
||||
expect(enabledRecords(rr, [
|
||||
[record1, 'record1'],
|
||||
[record2, 'record2']
|
||||
])).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
describe('adding/removing record ranges', () => {
|
||||
var parent, child1, child2, child3;
|
||||
var childRecord1, childRecord2, childRecord3;
|
||||
var recordNames;
|
||||
|
||||
beforeEach(() => {
|
||||
parent = new RecordRange(null, null);
|
||||
|
||||
child1 = new RecordRange(null, null);
|
||||
childRecord1 = createRecord(child1);
|
||||
child1.addRecord(childRecord1);
|
||||
|
||||
child2 = new RecordRange(null, null);
|
||||
childRecord2 = createRecord(child2);
|
||||
child2.addRecord(childRecord2);
|
||||
|
||||
child3 = new RecordRange(null, null);
|
||||
childRecord3 = createRecord(child3);
|
||||
child3.addRecord(childRecord3);
|
||||
|
||||
recordNames = [
|
||||
[childRecord1, 'record1'],
|
||||
[childRecord2, 'record2'],
|
||||
[childRecord3, 'record3']
|
||||
];
|
||||
});
|
||||
|
||||
it('should add record ranges', () => {
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
it('should handle adding an empty range', () => {
|
||||
var emptyRange = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
child1.addRange(emptyRange);
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
it('should handle enabling/disabling an empty range', () => {
|
||||
var emptyRange = new RecordRange(null, null);
|
||||
emptyRange.disable();
|
||||
emptyRange.enable();
|
||||
|
||||
expect(enabledRecords(emptyRange, recordNames)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle adding a range into an empty range', () => {
|
||||
var emptyRange = new RecordRange(null, null);
|
||||
parent.addRange(emptyRange);
|
||||
parent.addRange(child2);
|
||||
|
||||
emptyRange.addRange(child1);
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
it('should add nested record ranges', () => {
|
||||
parent.addRange(child1);
|
||||
child1.addRange(child2);
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
it('should remove record ranges', () => {
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
|
||||
child1.remove();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record2']);
|
||||
|
||||
child2.remove();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should remove an empty record range', () => {
|
||||
var emptyRange = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
parent.addRange(emptyRange);
|
||||
parent.addRange(child2);
|
||||
|
||||
emptyRange.remove();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record2']);
|
||||
});
|
||||
|
||||
it('should remove a record range surrounded by other ranges', () => {
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
parent.addRange(child3);
|
||||
|
||||
child2.remove();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record3']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('enabling/disabling records', () => {
|
||||
var rr;
|
||||
var record1, record2, record3, record4;
|
||||
var recordNames;
|
||||
|
||||
beforeEach(() => {
|
||||
rr = new RecordRange(null, null);
|
||||
record1 = createRecord(rr);
|
||||
record2 = createRecord(rr);
|
||||
record3 = createRecord(rr);
|
||||
record4 = createRecord(rr);
|
||||
|
||||
recordNames = [
|
||||
[record1, 'record1'],
|
||||
[record2, 'record2'],
|
||||
[record3, 'record3'],
|
||||
[record4, 'record4']
|
||||
];
|
||||
});
|
||||
|
||||
it('should disable a single record', () => {
|
||||
rr.addRecord(record1);
|
||||
|
||||
record1.disable();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should enable a single record', () => {
|
||||
rr.addRecord(record1);
|
||||
record1.disable();
|
||||
|
||||
record1.enable();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual(['record1']);
|
||||
});
|
||||
|
||||
it('should disable a record', () => {
|
||||
rr.addRecord(record1);
|
||||
rr.addRecord(record2);
|
||||
rr.addRecord(record3);
|
||||
rr.addRecord(record4);
|
||||
|
||||
record2.disable();
|
||||
record3.disable();
|
||||
|
||||
expect(record2.isDisabled()).toBeTruthy();
|
||||
expect(record3.isDisabled()).toBeTruthy();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual(['record1', 'record4']);
|
||||
});
|
||||
|
||||
it('should enable a record', () => {
|
||||
rr.addRecord(record1);
|
||||
rr.addRecord(record2);
|
||||
rr.addRecord(record3);
|
||||
rr.addRecord(record4);
|
||||
record2.disable();
|
||||
record3.disable();
|
||||
|
||||
record2.enable();
|
||||
record3.enable();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual(['record1', 'record2', 'record3', 'record4']);
|
||||
});
|
||||
|
||||
it('should disable a single record in a range', () => {
|
||||
var rr1 = new RecordRange(null, null);
|
||||
rr1.addRecord(record1);
|
||||
|
||||
var rr2 = new RecordRange(null, null);
|
||||
rr2.addRecord(record2);
|
||||
|
||||
var rr3 = new RecordRange(null, null);
|
||||
rr3.addRecord(record3);
|
||||
|
||||
rr.addRange(rr1);
|
||||
rr.addRange(rr2);
|
||||
rr.addRange(rr3);
|
||||
|
||||
record2.disable();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual(['record1', 'record3']);
|
||||
|
||||
record2.enable();
|
||||
|
||||
expect(enabledRecords(rr, recordNames)).toEqual(['record1', 'record2', 'record3']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('enabling/disabling record ranges', () => {
|
||||
var child1, child2, child3, child4;
|
||||
var record1, record2, record3, record4;
|
||||
var recordNames;
|
||||
|
||||
beforeEach(() => {
|
||||
child1 = new RecordRange(null, null);
|
||||
record1 = createRecord(child1);
|
||||
child1.addRecord(record1);
|
||||
|
||||
child2 = new RecordRange(null, null);
|
||||
record2 = createRecord(child2);
|
||||
child2.addRecord(record2);
|
||||
|
||||
child3 = new RecordRange(null, null);
|
||||
record3 = createRecord(child3);
|
||||
child3.addRecord(record3);
|
||||
|
||||
child4 = new RecordRange(null, null);
|
||||
record4 = createRecord(child4);
|
||||
child4.addRecord(record4);
|
||||
|
||||
recordNames = [
|
||||
[record1, 'record1'],
|
||||
[record2, 'record2'],
|
||||
[record3, 'record3'],
|
||||
[record4, 'record4']
|
||||
];
|
||||
});
|
||||
|
||||
it('should disable a single record range', () => {
|
||||
var parent = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
|
||||
child1.disable();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual([]);
|
||||
});
|
||||
|
||||
it('should enable a single record range', () => {
|
||||
var parent = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
|
||||
child1.disable();
|
||||
|
||||
child1.enable();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1']);
|
||||
});
|
||||
|
||||
it('should disable a record range', () => {
|
||||
var parent = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
parent.addRange(child3);
|
||||
parent.addRange(child4);
|
||||
|
||||
child2.disable();
|
||||
child3.disable();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual(['record1', 'record4']);
|
||||
});
|
||||
|
||||
it('should enable a record range', () => {
|
||||
var parent = new RecordRange(null, null);
|
||||
parent.addRange(child1);
|
||||
parent.addRange(child2);
|
||||
parent.addRange(child3);
|
||||
parent.addRange(child4);
|
||||
|
||||
child2.disable();
|
||||
child2.disable();
|
||||
|
||||
child2.enable();
|
||||
child3.enable();
|
||||
|
||||
expect(enabledRecords(parent, recordNames)).toEqual([
|
||||
'record1', 'record2', 'record3', 'record4'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("inspect", () => {
|
||||
it("should return the description of the record", () => {
|
||||
var proto = new ProtoRecord(null, RECORD_TYPE_CONST, 1, 0, "name", null, "group", "expression");
|
||||
var record = new Record(null, proto, null);
|
||||
|
||||
var i = record.inspect();
|
||||
expect(i.description).toContain("const, name, enabled");
|
||||
});
|
||||
|
||||
it("should return the description of the records in the range", () => {
|
||||
var proto = new ProtoRecord(null, RECORD_TYPE_CONST, 1, 0, "name", null, "group", "expression");
|
||||
var record = new Record(null, proto, null);
|
||||
var range = new RecordRange(null, null);
|
||||
range.addRecord(record);
|
||||
|
||||
var i = range.inspect();;
|
||||
expect(i.length).toEqual(1);
|
||||
expect(i[0]).toContain("const, name, enabled");
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user