test(Pipes): assert Date, Json & Slice are non pure

This commit is contained in:
Victor Berchet 2015-10-27 12:43:34 -07:00
parent 2f1f83a186
commit 40586152d1
3 changed files with 57 additions and 25 deletions

View File

@ -12,6 +12,7 @@ import {
import {DatePipe} from 'angular2/core'; import {DatePipe} from 'angular2/core';
import {DateWrapper} from 'angular2/src/core/facade/lang'; import {DateWrapper} from 'angular2/src/core/facade/lang';
import {PipeResolver} from 'angular2/src/core/linker/pipe_resolver';
export function main() { export function main() {
describe("DatePipe", () => { describe("DatePipe", () => {
@ -23,6 +24,10 @@ export function main() {
pipe = new DatePipe(); pipe = new DatePipe();
}); });
it('should be marked as non-pure', () => {
expect(new PipeResolver().resolve(DatePipe).pure).toEqual(false);
});
describe("supports", () => { describe("supports", () => {
it("should support date", () => { expect(pipe.supports(date)).toBe(true); }); it("should support date", () => { expect(pipe.supports(date)).toBe(true); });
it("should support int", () => { expect(pipe.supports(123456789)).toBe(true); }); it("should support int", () => { expect(pipe.supports(123456789)).toBe(true); });

View File

@ -10,11 +10,12 @@ import {
AsyncTestCompleter, AsyncTestCompleter,
inject, inject,
proxy, proxy,
SpyObject SpyObject,
TestComponentBuilder
} from 'angular2/testing_internal'; } from 'angular2/testing_internal';
import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/core/facade/lang'; import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/core/facade/lang';
import {JsonPipe} from 'angular2/core'; import {JsonPipe, Component} from 'angular2/core';
export function main() { export function main() {
describe("JsonPipe", () => { describe("JsonPipe", () => {
@ -22,7 +23,6 @@ export function main() {
var inceptionObj; var inceptionObj;
var inceptionObjString; var inceptionObjString;
var pipe; var pipe;
var collection: number[];
function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); } function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); }
@ -33,7 +33,6 @@ export function main() {
pipe = new JsonPipe(); pipe = new JsonPipe();
collection = [];
}); });
describe("transform", () => { describe("transform", () => {
@ -51,26 +50,29 @@ export function main() {
var dream2 = normalize(Json.stringify(inceptionObj)); var dream2 = normalize(Json.stringify(inceptionObj));
expect(dream1).toEqual(dream2); expect(dream1).toEqual(dream2);
}); });
it("should return same ref when nothing has changed since the last call", () => {
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
}); });
describe('integration', () => {
it('should work with mutable objects',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.createAsync(TestComp).then((rootTC) => {
let mutable: number[] = [1];
rootTC.debugElement.componentInstance.data = mutable;
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText("[\n 1\n]");
it("should return a new value when something changed but the ref hasn't", () => { mutable.push(2);
var stringCollection = '[]'; rootTC.detectChanges();
var stringCollectionWith1 = '[\n' + expect(rootTC.debugElement.nativeElement).toHaveText("[\n 1,\n 2\n]");
' 1' +
'\n]';
expect(pipe.transform(collection)).toEqual(stringCollection); async.done();
collection.push(1);
expect(pipe.transform(collection)).toEqual(stringCollectionWith1);
}); });
}));
}); });
}); });
} }
@Component({selector: 'test-comp', template: '{{data | json}}', pipes: [JsonPipe]})
class TestComp {
data: any;
}

View File

@ -7,14 +7,17 @@ import {
expect, expect,
beforeEach, beforeEach,
afterEach, afterEach,
browserDetection browserDetection,
inject,
TestComponentBuilder,
AsyncTestCompleter
} from 'angular2/testing_internal'; } from 'angular2/testing_internal';
import {SlicePipe} from 'angular2/src/core/pipes'; import {SlicePipe, Component} from 'angular2/core';
export function main() { export function main() {
describe("SlicePipe", () => { describe("SlicePipe", () => {
var list; var list: number[];
var str; var str;
var pipe; var pipe;
@ -85,5 +88,27 @@ export function main() {
}); });
describe('integration', () => {
it('should work with mutable arrays',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
tcb.createAsync(TestComp).then((rootTC) => {
let mutable: number[] = [1, 2];
rootTC.debugElement.componentInstance.data = mutable;
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText('2');
mutable.push(3);
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText('2,3');
async.done();
});
}));
});
}); });
} }
@Component({selector: 'test-comp', template: '{{(data | slice:1).join(",") }}', pipes: [SlicePipe]})
class TestComp {
data: any;
}