angular-cn/modules/angular2/test/change_detection/pipes/keyvalue_changes_spec.ts

191 lines
5.9 KiB
TypeScript
Raw Normal View History

import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
import {KeyValueChanges} from 'angular2/src/change_detection/pipes/keyvalue_changes';
import {NumberWrapper, isJsObject} from 'angular2/src/facade/lang';
import {MapWrapper} from 'angular2/src/facade/collection';
import {kvChangesAsString} from '../util';
// todo(vicb): Update the code & tests for object equality
export function main() {
describe('keyvalue_changes', function() {
describe('KeyValueChanges', function() {
var changes;
var m: Map<any, any>;
beforeEach(() => {
changes = new KeyValueChanges();
m = new Map();
});
afterEach(() => { changes = null; });
it('should detect additions', () => {
changes.check(m);
m.set('a', 1);
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({map: ['a[null->1]'], additions: ['a[null->1]']}));
m.set('b', 2);
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString(
{map: ['a', 'b[null->2]'], previous: ['a'], additions: ['b[null->2]']}));
});
it('should handle changing key/values correctly', () => {
m.set(1, 10);
m.set(2, 20);
changes.check(m);
m.set(2, 10);
m.set(1, 20);
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({
map: ['1[10->20]', '2[20->10]'],
previous: ['1[10->20]', '2[20->10]'],
changes: ['1[10->20]', '2[20->10]']
}));
});
it('should expose previous and current value', () => {
var previous, current;
m.set(1, 10);
changes.check(m);
m.set(1, 20);
changes.check(m);
changes.forEachChangedItem((record) => {
previous = record.previousValue;
current = record.currentValue;
});
expect(previous).toEqual(10);
expect(current).toEqual(20);
});
it('should do basic map watching', () => {
changes.check(m);
m.set('a', 'A');
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
m.set('b', 'B');
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString(
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
m.set('b', 'BB');
m.set('d', 'D');
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({
map: ['a', 'b[B->BB]', 'd[null->D]'],
previous: ['a', 'b[B->BB]'],
additions: ['d[null->D]'],
changes: ['b[B->BB]']
}));
MapWrapper.delete(m, 'b');
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString(
{map: ['a', 'd'], previous: ['a', 'b[BB->null]', 'd'], removals: ['b[BB->null]']}));
m.clear();
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString(
{previous: ['a[A->null]', 'd[D->null]'], removals: ['a[A->null]', 'd[D->null]']}));
});
it('should test string by value rather than by reference (DART)', () => {
m.set('foo', 'bar');
changes.check(m);
var f = 'f';
var oo = 'oo';
var b = 'b';
var ar = 'ar';
m.set(f + oo, b + ar);
changes.check(m);
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
});
it('should not see a NaN value as a change (JS)', () => {
m.set('foo', NumberWrapper.NaN);
changes.check(m);
changes.check(m);
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
});
// JS specific tests (JS Objects)
if (isJsObject({})) {
describe('JsObject changes', () => {
it('should support JS Object', () => {
expect(KeyValueChanges.supportsObj({})).toBeTruthy();
expect(KeyValueChanges.supportsObj("not supported")).toBeFalsy();
expect(KeyValueChanges.supportsObj(0)).toBeFalsy();
expect(KeyValueChanges.supportsObj(null)).toBeFalsy();
});
it('should do basic object watching', () => {
let m = {};
changes.check(m);
m['a'] = 'A';
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
m['b'] = 'B';
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString(
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
m['b'] = 'BB';
m['d'] = 'D';
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({
map: ['a', 'b[B->BB]', 'd[null->D]'],
previous: ['a', 'b[B->BB]'],
additions: ['d[null->D]'],
changes: ['b[B->BB]']
}));
m = {};
m['a'] = 'A';
m['d'] = 'D';
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({
map: ['a', 'd'],
previous: ['a', 'b[BB->null]', 'd'],
removals: ['b[BB->null]']
}));
m = {};
changes.check(m);
expect(changes.toString())
.toEqual(kvChangesAsString({
previous: ['a[A->null]', 'd[D->null]'],
removals: ['a[A->null]', 'd[D->null]']
}));
});
});
}
});
});
}