Skip to content

Commit 3f39aaf

Browse files
Fabian Gosebrinkjuristr
Fabian Gosebrink
authored andcommitted
feat(pipes): add example for testing a filter pipe
1 parent 079981f commit 3f39aaf

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ All the scenarios are listed here below and nicely linked to the source file.
4141
_Learn how to test async operations using the `async()` as well as `fakeAsync()` functions._
4242
- [Mocking and remote http calls](./src/app/services/remote.service.spec.ts)
4343
_Learn how to mock external dependencies, such as use the `MockBackend` provided by Angular to respond to http calls._
44+
1. [**Testing Pipes**](./src/app/pipes)
45+
- [custom filter pipe](./src/app/pipes/filter.pipe.spec.ts)
4446
1. [**Custom Matchers and Utilities**](./src/app/utils)
4547
- [Create your own custom Jasmine matchers](./src/app/utils/custom-matchers.ts)
4648

src/app/pipes/filter.pipe.spec.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { FilterPipe } from './filter.pipe';
2+
3+
describe('FilterPipe', () => {
4+
5+
let filterPipe: FilterPipe;
6+
7+
// synchronous beforeEach
8+
beforeEach(() => {
9+
filterPipe = new FilterPipe();
10+
});
11+
12+
it('should be instanciated', () => {
13+
expect(filterPipe).toBeDefined();
14+
});
15+
16+
it('should return empty array if no items given', () => {
17+
18+
let items = null;
19+
20+
let filtered = filterPipe.transform(items, 'name', 'Hans');
21+
22+
expect(filtered.length).toBe(0);
23+
expect(filtered).toEqual([]);
24+
});
25+
26+
it('should return items if no field is given', () => {
27+
28+
let items = [];
29+
items.push({ id: 1, name: 'Hans' });
30+
31+
let filtered = filterPipe.transform(items, null, 'Hans');
32+
33+
expect(filtered).toEqual(items);
34+
});
35+
36+
it('should return items if no value is given', () => {
37+
38+
let items = [];
39+
items.push({ id: 1, name: 'Hans' });
40+
41+
let filtered = filterPipe.transform(items, 'name', null);
42+
43+
expect(filtered).toEqual(items);
44+
});
45+
46+
it('should filter correctly', () => {
47+
48+
let items = [];
49+
50+
items.push({ id: 1, name: 'Hans' });
51+
items.push({ id: 2, name: 'Franz' });
52+
items.push({ id: 3, name: 'Kurt' });
53+
items.push({ id: 4, name: 'Gustav' });
54+
55+
let filtered = filterPipe.transform(items, 'name', 'Hans');
56+
57+
expect(filtered.length).toBeGreaterThan(0);
58+
expect(filtered.length).toBe(1);
59+
});
60+
61+
it('should filter two items', () => {
62+
63+
let items = [];
64+
65+
items.push({ id: 1, name: 'Hans' });
66+
items.push({ id: 2, name: 'Hans' });
67+
items.push({ id: 3, name: 'Kurt' });
68+
items.push({ id: 4, name: 'Gustav' });
69+
70+
let filtered = filterPipe.transform(items, 'name', 'Hans');
71+
72+
expect(filtered.length).toBe(2);
73+
});
74+
});

src/app/pipes/filter.pipe.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Injectable, Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'filter'
5+
})
6+
7+
@Injectable()
8+
export class FilterPipe implements PipeTransform {
9+
transform(items: any[], field: string, value: string): any[] {
10+
if (!items) {
11+
return [];
12+
}
13+
if (!field || !value) {
14+
return items;
15+
}
16+
17+
return items.filter(singleItem => singleItem[field].toLowerCase().includes(value.toLowerCase()));
18+
}
19+
}

0 commit comments

Comments
 (0)