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;
|
|
|
|
var m;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2014-11-24 12:42:53 -05:00
|
|
|
changes = new KeyValueChanges();
|
2014-10-29 16:56:31 -04:00
|
|
|
m = MapWrapper.create();
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
MapWrapper.set(m, 'a', 1);
|
|
|
|
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
|
|
|
|
|
|
|
MapWrapper.set(m, 'b', 2);
|
|
|
|
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', () => {
|
|
|
|
MapWrapper.set(m, 1, 10);
|
|
|
|
MapWrapper.set(m, 2, 20);
|
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
MapWrapper.set(m, 2, 10);
|
|
|
|
MapWrapper.set(m, 1, 20);
|
|
|
|
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;
|
|
|
|
|
|
|
|
MapWrapper.set(m, 1, 10);
|
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
MapWrapper.set(m, 1, 20);
|
|
|
|
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);
|
|
|
|
|
|
|
|
MapWrapper.set(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-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
MapWrapper.set(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-10-29 16:56:31 -04:00
|
|
|
|
|
|
|
MapWrapper.set(m, 'b', 'BB');
|
|
|
|
MapWrapper.set(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-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
|
|
|
|
|
|
|
MapWrapper.clear(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-10-29 16:56:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should test string by value rather than by reference (DART)', () => {
|
|
|
|
MapWrapper.set(m, 'foo', 'bar');
|
|
|
|
changes.check(m);
|
|
|
|
|
|
|
|
var f = 'f';
|
|
|
|
var oo = 'oo';
|
|
|
|
var b = 'b';
|
|
|
|
var ar = 'ar';
|
|
|
|
|
|
|
|
MapWrapper.set(m, f + oo, b + ar);
|
|
|
|
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)', () => {
|
|
|
|
MapWrapper.set(m, 'foo', NumberWrapper.NaN);
|
|
|
|
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', () => {
|
|
|
|
m = {};
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|