2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-19 15:51:01 -04:00
|
|
|
import {CommonModule, SlicePipe} from '@angular/common';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {Component} from '@angular/core';
|
2016-08-15 16:52:57 -04:00
|
|
|
import {TestBed, async} from '@angular/core/testing';
|
2016-06-24 15:41:49 -04:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/browser_util';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
2015-08-30 20:04:48 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('SlicePipe', () => {
|
2015-10-27 15:43:34 -04:00
|
|
|
var list: number[];
|
2016-06-17 13:57:32 -04:00
|
|
|
var str: string;
|
|
|
|
var pipe: SlicePipe;
|
2015-08-30 20:04:48 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
list = [1, 2, 3, 4, 5];
|
|
|
|
str = 'tuvwxyz';
|
|
|
|
pipe = new SlicePipe();
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('supports', () => {
|
2016-06-17 13:57:32 -04:00
|
|
|
it('should support strings', () => { expect(() => pipe.transform(str, 0)).not.toThrow(); });
|
|
|
|
it('should support lists', () => { expect(() => pipe.transform(list, 0)).not.toThrow(); });
|
2015-08-30 20:04:48 -04:00
|
|
|
|
2016-06-17 13:57:32 -04:00
|
|
|
it('should not support other objects',
|
|
|
|
() => { expect(() => pipe.transform({}, 0)).toThrow(); });
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('transform', () => {
|
2015-08-30 20:04:48 -04:00
|
|
|
|
2016-06-17 13:57:32 -04:00
|
|
|
it('should return null if the value is null',
|
|
|
|
() => { expect(pipe.transform(null, 1)).toBe(null); });
|
2016-02-18 11:14:40 -05:00
|
|
|
|
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', () => {
|
2016-08-19 15:51:01 -04:00
|
|
|
|
|
|
|
@Component({selector: 'test-comp', template: '{{(data | slice:1).join(",") }}'})
|
|
|
|
class TestComp {
|
|
|
|
data: any;
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:52:57 -04:00
|
|
|
beforeEach(() => {
|
2016-08-19 15:51:01 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [TestComp], imports: [CommonModule]});
|
2016-08-15 16:52:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with mutable arrays', async(() => {
|
|
|
|
let fixture = TestBed.createComponent(TestComp);
|
|
|
|
let mutable: number[] = [1, 2];
|
|
|
|
fixture.debugElement.componentInstance.data = mutable;
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('2');
|
|
|
|
|
|
|
|
mutable.push(3);
|
|
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('2,3');
|
|
|
|
}));
|
2015-10-27 15:43:34 -04:00
|
|
|
});
|
2015-08-30 20:04:48 -04:00
|
|
|
});
|
2015-10-27 15:43:34 -04:00
|
|
|
}
|