2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
inject,
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
|
|
|
import {} from '@angular/core/testing/testing_internal';
|
|
|
|
import {browserDetection} from '@angular/platform-browser/testing';
|
|
|
|
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
2015-08-30 20:04:48 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
import {SlicePipe} from '@angular/common';
|
2015-08-30 20:04:48 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("SlicePipe", () => {
|
2015-10-27 15:43:34 -04:00
|
|
|
var list: number[];
|
2016-06-08 18:45:15 -04:00
|
|
|
var str: any /** TODO #9100 */;
|
|
|
|
var pipe: any /** TODO #9100 */;
|
2015-08-30 20:04:48 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
list = [1, 2, 3, 4, 5];
|
|
|
|
str = 'tuvwxyz';
|
|
|
|
pipe = new SlicePipe();
|
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("supports", () => {
|
|
|
|
it("should support strings", () => { expect(pipe.supports(str)).toBe(true); });
|
|
|
|
it("should support lists", () => { expect(pipe.supports(list)).toBe(true); });
|
2015-08-30 20:04:48 -04:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
it("should not support other objects", () => {
|
2015-08-30 20:04:48 -04:00
|
|
|
expect(pipe.supports(new Object())).toBe(false);
|
|
|
|
expect(pipe.supports(null)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("transform", () => {
|
2015-08-30 20:04:48 -04:00
|
|
|
|
2016-02-18 11:14:40 -05:00
|
|
|
it('should return null if the value is null',
|
|
|
|
() => { expect(pipe.transform(null, [4, 2])).toBe(null); });
|
|
|
|
|
2015-08-30 20:04:48 -04:00
|
|
|
it('should return all items after START index when START is positive and END is omitted',
|
|
|
|
() => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, 3)).toEqual([4, 5]);
|
|
|
|
expect(pipe.transform(str, 3)).toEqual('wxyz');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return last START items when START is negative and END is omitted', () => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, -3)).toEqual([3, 4, 5]);
|
|
|
|
expect(pipe.transform(str, -3)).toEqual('xyz');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return all items between START and END index when START and END are positive',
|
|
|
|
() => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, 1, 3)).toEqual([2, 3]);
|
|
|
|
expect(pipe.transform(str, 1, 3)).toEqual('uv');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return all items between START and END from the end when START and END are negative',
|
|
|
|
() => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, -4, -2)).toEqual([2, 3]);
|
|
|
|
expect(pipe.transform(str, -4, -2)).toEqual('wx');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an empty value if START is greater than END', () => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, 4, 2)).toEqual([]);
|
|
|
|
expect(pipe.transform(str, 4, 2)).toEqual('');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an empty value if START greater than input length', () => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, 99)).toEqual([]);
|
|
|
|
expect(pipe.transform(str, 99)).toEqual('');
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
2015-09-27 18:38:30 -04:00
|
|
|
// Makes Edge to disconnect when running the full unit test campaign
|
|
|
|
// TODO: remove when issue is solved: https://github.com/angular/angular/issues/4756
|
|
|
|
if (!browserDetection.isEdge) {
|
|
|
|
it('should return entire input if START is negative and greater than input length', () => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, -99)).toEqual([1, 2, 3, 4, 5]);
|
|
|
|
expect(pipe.transform(str, -99)).toEqual('tuvwxyz');
|
2015-09-27 18:38:30 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not modify the input list', () => {
|
2016-04-22 18:33:32 -04:00
|
|
|
expect(pipe.transform(list, 2)).toEqual([3, 4, 5]);
|
2015-09-27 18:38:30 -04:00
|
|
|
expect(list).toEqual([1, 2, 3, 4, 5]);
|
|
|
|
});
|
|
|
|
}
|
2015-08-30 20:04:48 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-10-27 15:43:34 -04:00
|
|
|
describe('integration', () => {
|
|
|
|
it('should work with mutable arrays',
|
2016-06-09 14:04:15 -04:00
|
|
|
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async: AsyncTestCompleter) => {
|
2015-10-31 12:50:19 -04:00
|
|
|
tcb.createAsync(TestComp).then((fixture) => {
|
2015-10-27 15:43:34 -04:00
|
|
|
let mutable: number[] = [1, 2];
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.debugElement.componentInstance.data = mutable;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('2');
|
2015-10-27 15:43:34 -04:00
|
|
|
|
|
|
|
mutable.push(3);
|
2015-10-31 12:50:19 -04:00
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('2,3');
|
2015-10-27 15:43:34 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
2015-10-27 15:43:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'test-comp', template: '{{(data | slice:1).join(",") }}', pipes: [SlicePipe]})
|
|
|
|
class TestComp {
|
|
|
|
data: any;
|
|
|
|
}
|