2015-02-05 16:08:05 -05:00
|
|
|
import {describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
|
2015-02-12 17:56:41 -05:00
|
|
|
import {KeyValueChanges} from 'angular2/src/change_detection/pipes/keyvalue_changes';
|
2015-02-05 16:08:05 -05:00
|
|
|
import {NumberWrapper, isJsObject} from 'angular2/src/facade/lang';
|
|
|
|
import {MapWrapper} from 'angular2/src/facade/collection';
|
2015-04-19 15:43:56 -04:00
|
|
|
import {kvChangesAsString} from '../util';
|
2014-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
// todo(vicb): Update the code & tests for object equality
|
|
|
|
export function main() {
|
2014-11-24 12:42:53 -05:00
|
|
|
describe('keyvalue_changes', function() {
|
|
|
|
describe('KeyValueChanges', function() {
|
2014-10-29 16:56:31 -04:00
|
|
|
var changes;
|
2015-06-17 19:21:40 -04:00
|
|
|
var m: Map<any, any>;
|
2014-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2014-11-24 12:42:53 -05:00
|
|
|
changes = new KeyValueChanges();
|
2015-06-17 19:21:40 -04:00
|
|
|
m = new Map();
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
afterEach(() => { changes = null; });
|
2014-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
it('should detect additions', () => {
|
|
|
|
changes.check(m);
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('a', 1);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString({map: ['a[null->1]'], additions: ['a[null->1]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('b', 2);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString(
|
|
|
|
{map: ['a', 'b[null->2]'], previous: ['a'], additions: ['b[null->2]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle changing key/values correctly', () => {
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set(1, 10);
|
|
|
|
m.set(2, 20);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set(2, 10);
|
|
|
|
m.set(1, 20);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
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]']
|
|
|
|
}));
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
2015-03-26 14:19:10 -04:00
|
|
|
it('should expose previous and current value', () => {
|
|
|
|
var previous, current;
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set(1, 10);
|
2015-03-26 14:19:10 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set(1, 20);
|
2015-03-26 14:19:10 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
2015-06-03 16:42:57 -04:00
|
|
|
changes.forEachChangedItem((record) => {
|
|
|
|
previous = record.previousValue;
|
|
|
|
current = record.currentValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(previous).toEqual(10);
|
2015-03-26 14:19:10 -04:00
|
|
|
expect(current).toEqual(20);
|
|
|
|
});
|
|
|
|
|
2014-10-29 16:56:31 -04:00
|
|
|
it('should do basic map watching', () => {
|
|
|
|
changes.check(m);
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('a', 'A');
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('b', 'B');
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString(
|
|
|
|
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('b', 'BB');
|
|
|
|
m.set('d', 'D');
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
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]']
|
|
|
|
}));
|
2014-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
MapWrapper.delete(m, 'b');
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
2015-06-03 16:42:57 -04:00
|
|
|
.toEqual(kvChangesAsString(
|
|
|
|
{map: ['a', 'd'], previous: ['a', 'b[BB->null]', 'd'], removals: ['b[BB->null]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
|
2015-06-18 12:59:51 -04:00
|
|
|
m.clear();
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString(
|
|
|
|
{previous: ['a[A->null]', 'd[D->null]'], removals: ['a[A->null]', 'd[D->null]']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should test string by value rather than by reference (DART)', () => {
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('foo', 'bar');
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
var f = 'f';
|
|
|
|
var oo = 'oo';
|
|
|
|
var b = 'b';
|
|
|
|
var ar = 'ar';
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set(f + oo, b + ar);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not see a NaN value as a change (JS)', () => {
|
2015-06-17 19:21:40 -04:00
|
|
|
m.set('foo', NumberWrapper.NaN);
|
2014-10-29 16:56:31 -04:00
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString()).toEqual(kvChangesAsString({map: ['foo'], previous: ['foo']}));
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
2014-11-24 12:42:53 -05:00
|
|
|
|
|
|
|
// JS specific tests (JS Objects)
|
|
|
|
if (isJsObject({})) {
|
|
|
|
describe('JsObject changes', () => {
|
|
|
|
it('should support JS Object', () => {
|
2015-02-24 11:39:10 -05:00
|
|
|
expect(KeyValueChanges.supportsObj({})).toBeTruthy();
|
|
|
|
expect(KeyValueChanges.supportsObj("not supported")).toBeFalsy();
|
|
|
|
expect(KeyValueChanges.supportsObj(0)).toBeFalsy();
|
|
|
|
expect(KeyValueChanges.supportsObj(null)).toBeFalsy();
|
2014-11-24 12:42:53 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should do basic object watching', () => {
|
2015-06-17 19:21:40 -04:00
|
|
|
let m = {};
|
2014-11-24 12:42:53 -05:00
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
m['a'] = 'A';
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString({map: ['a[null->A]'], additions: ['a[null->A]']}));
|
2014-11-24 12:42:53 -05:00
|
|
|
|
|
|
|
m['b'] = 'B';
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString(
|
|
|
|
{map: ['a', 'b[null->B]'], previous: ['a'], additions: ['b[null->B]']}));
|
2014-11-24 12:42:53 -05:00
|
|
|
|
|
|
|
m['b'] = 'BB';
|
|
|
|
m['d'] = 'D';
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
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]']
|
|
|
|
}));
|
2014-11-24 12:42:53 -05:00
|
|
|
|
|
|
|
m = {};
|
|
|
|
m['a'] = 'A';
|
|
|
|
m['d'] = 'D';
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString({
|
|
|
|
map: ['a', 'd'],
|
|
|
|
previous: ['a', 'b[BB->null]', 'd'],
|
|
|
|
removals: ['b[BB->null]']
|
|
|
|
}));
|
2014-11-24 12:42:53 -05:00
|
|
|
|
|
|
|
m = {};
|
|
|
|
changes.check(m);
|
2015-05-26 20:12:38 -04:00
|
|
|
expect(changes.toString())
|
|
|
|
.toEqual(kvChangesAsString({
|
|
|
|
previous: ['a[A->null]', 'd[D->null]'],
|
|
|
|
removals: ['a[A->null]', 'd[D->null]']
|
|
|
|
}));
|
2014-11-24 12:42:53 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|