|
1 | 1 | import { JoinPipe } from './join.pipe'; |
| 2 | +import { Component } from '@angular/core'; |
| 3 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 4 | +import { JoinModule } from './join.module'; |
| 5 | +import { By } from '@angular/platform-browser'; |
2 | 6 |
|
3 | 7 | describe('JoinPipe', () => { |
4 | | - let pipe: JoinPipe; |
5 | | - const testCases = [ |
6 | | - { expectation: 'join string array', array: ['test1', 'test2', 'test3'], expected: 'test1, test2, test3' }, |
7 | | - { |
8 | | - expectation: 'join string array with - separator', |
9 | | - array: ['test1', 'test2', 'test3'], |
10 | | - separator: '-', |
11 | | - expected: 'test1-test2-test3' |
12 | | - }, |
13 | | - { expectation: 'join number array', array: [1, -4, 100, 0], expected: '1, -4, 100, 0' }, |
14 | | - { expectation: 'join number array with / separator', array: [1, -4, 100, 0], separator: '/', expected: '1/-4/100/0' }, |
15 | | - { expectation: 'join empty array', array: [], expected: '' }, |
16 | | - { expectation: 'join empty array with - separator', array: [], separator: '-', expected: '' } |
17 | | - ]; |
18 | | - |
19 | | - beforeEach(() => { |
20 | | - pipe = new JoinPipe(); |
| 8 | + describe('transform', () => { |
| 9 | + let pipe: JoinPipe; |
| 10 | + const testCases = [ |
| 11 | + { expectation: 'should join string array', array: ['test1', 'test2', 'test3'], expected: 'test1, test2, test3' }, |
| 12 | + { |
| 13 | + expectation: 'should join string array with - separator', |
| 14 | + array: ['test1', 'test2', 'test3'], |
| 15 | + separator: '-', |
| 16 | + expected: 'test1-test2-test3' |
| 17 | + }, |
| 18 | + { expectation: 'should join number array', array: [1, -4, 100, 0], expected: '1, -4, 100, 0' }, |
| 19 | + { expectation: 'should join number array with / separator', array: [1, -4, 100, 0], separator: '/', expected: '1/-4/100/0' }, |
| 20 | + { expectation: 'should join empty array', array: [], expected: '' }, |
| 21 | + { expectation: 'should join empty array with - separator', array: [], separator: '-', expected: '' } |
| 22 | + ]; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + pipe = new JoinPipe(); |
| 26 | + }); |
| 27 | + |
| 28 | + testCases.forEach((testCase) => { |
| 29 | + it(testCase.expectation, () => { |
| 30 | + expect(pipe.transform(testCase.array, testCase?.separator)).toBe(testCase.expected); |
| 31 | + }); |
| 32 | + }); |
21 | 33 | }); |
22 | 34 |
|
23 | | - testCases.forEach((testCase) => { |
24 | | - it(testCase.expectation, () => { |
25 | | - expect(pipe.transform(testCase.array, testCase?.separator)).toBe(testCase.expected); |
| 35 | + describe('integration', () => { |
| 36 | + @Component({ |
| 37 | + selector: 'ngs-test-component', |
| 38 | + template: '<div>{{array | join}}</div>' |
| 39 | + }) |
| 40 | + class TestComponent { |
| 41 | + array: string[] | undefined; |
| 42 | + } |
| 43 | + |
| 44 | + let fixture: ComponentFixture<TestComponent>; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + TestBed.configureTestingModule({ declarations: [TestComponent], imports: [JoinModule] }); |
| 48 | + fixture = TestBed.createComponent(TestComponent); |
26 | 49 | }); |
| 50 | + |
| 51 | + it( |
| 52 | + 'should work with mutable arrays', |
| 53 | + waitForAsync(() => { |
| 54 | + const mutable = ['test1', 'test2']; |
| 55 | + fixture.componentInstance.array = mutable; |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + const div = fixture.debugElement.query(By.css('div')).nativeElement; |
| 59 | + expect(div.textContent).toBe('test1, test2'); |
| 60 | + |
| 61 | + mutable.push('test3'); |
| 62 | + fixture.detectChanges(); |
| 63 | + expect(div.textContent).toBe('test1, test2, test3'); |
| 64 | + }) |
| 65 | + ); |
27 | 66 | }); |
28 | 67 | }); |
0 commit comments