Skip to content

Commit e59fa5a

Browse files
committed
test: complete tests for join and truncate words pipes
1 parent 89be47c commit e59fa5a

File tree

2 files changed

+93
-23
lines changed

2 files changed

+93
-23
lines changed
Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
11
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';
26

37
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+
});
2133
});
2234

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);
2649
});
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+
);
2766
});
2867
});
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import { TruncateWordsPipe } from './truncate-words.pipe';
22

33
describe('TruncateWordsPipe', () => {
4-
it('create an instance', () => {
5-
const pipe = new TruncateWordsPipe();
6-
expect(pipe).toBeTruthy();
4+
describe('transform', () => {
5+
let pipe: TruncateWordsPipe;
6+
const testCases = [
7+
{
8+
expectation: 'should truncate text',
9+
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
10+
wordCount: 4,
11+
expected: 'Lorem ipsum dolor sit...'
12+
},
13+
{
14+
expectation: 'should truncate text shorter than word count ',
15+
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
16+
wordCount: 20,
17+
expected: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
18+
},
19+
{
20+
expectation: 'should truncate text with custom suffix',
21+
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
22+
wordCount: 5,
23+
suffix: '--',
24+
expected: 'Lorem ipsum dolor sit amet,--'
25+
},
26+
{ expectation: 'should not truncate text if it is empty', value: '', wordCount: 7, suffix: '___', expected: '' }
27+
];
28+
29+
beforeEach(() => {
30+
pipe = new TruncateWordsPipe();
31+
});
32+
33+
testCases.forEach((testCase) => {
34+
it(testCase.expectation, () => {
35+
expect(pipe.transform(testCase.value, testCase.wordCount, testCase?.suffix)).toBe(testCase.expected);
36+
});
37+
});
738
});
839
});

0 commit comments

Comments
 (0)