Skip to content

Commit 16f627b

Browse files
committed
pipe tests added
1 parent e7daed1 commit 16f627b

4 files changed

Lines changed: 112 additions & 26 deletions

File tree

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
1-
# Testin Pipes
1+
# Testing Pipes
22

3+
> You can test pipes without the Angular testing utilities
4+
5+
```bash
6+
npx ng g p titlecase
7+
```
8+
9+
Update `titlecase.pipe.ts`
10+
11+
```ts
12+
import { Pipe, PipeTransform } from "@angular/core";
13+
14+
@Pipe({
15+
name: "titlecase",
16+
pure: true,
17+
})
18+
export class TitlecasePipe implements PipeTransform {
19+
transform(input: string): string {
20+
return input.length === 0
21+
? ""
22+
: input.replace(
23+
/\w\S*/g,
24+
(txt) => txt[0].toUpperCase() + txt.slice(1).toLowerCase()
25+
);
26+
}
27+
}
28+
```
29+
30+
Update `titlecase.pipe.spec.ts`
31+
32+
```ts
33+
import { TitlecasePipe } from './titlecase.pipe';
34+
35+
describe('TitlecasePipe', () => {
36+
const pipe = new TitlecasePipe();
37+
38+
it('transforms "abc" to "Abc"', () => {
39+
expect(pipe.transform('abc')).toBe('Abc');
40+
});
41+
42+
it('transforms "abc def" to "Abc Def"', () => {
43+
expect(pipe.transform('abc def')).toBe('Abc Def');
44+
});
45+
});
46+
47+
```
48+
49+
```bash
50+
npx ng test --include app/titlecase.pipe.spec.ts
51+
```
52+
53+
## Writing DOM tests to support a pipe test
54+
55+
Previous tests are working in isolation. They can't tell if the `TitleCasePipe` is working properly as applied in the application components.
56+
57+
### Exercise
58+
59+
Create a component that uses `HighlightDirective` and create a test that ensures that the directive is applied.
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { TestBed } from '@angular/core/testing';
2-
import { AppComponent } from './app.component';
1+
// import { TestBed } from '@angular/core/testing';
2+
// import { AppComponent } from './app.component';
33

4-
describe('AppComponent', () => {
5-
beforeEach(async () => {
6-
await TestBed.configureTestingModule({
7-
imports: [AppComponent],
8-
}).compileComponents();
9-
});
4+
// describe('AppComponent', () => {
5+
// beforeEach(async () => {
6+
// await TestBed.configureTestingModule({
7+
// imports: [AppComponent],
8+
// }).compileComponents();
9+
// });
1010

11-
it('should create the app', () => {
12-
const fixture = TestBed.createComponent(AppComponent);
13-
const app = fixture.componentInstance;
14-
expect(app).toBeTruthy();
15-
});
11+
// it('should create the app', () => {
12+
// const fixture = TestBed.createComponent(AppComponent);
13+
// const app = fixture.componentInstance;
14+
// expect(app).toBeTruthy();
15+
// });
1616

17-
it(`should have the 'application' title`, () => {
18-
const fixture = TestBed.createComponent(AppComponent);
19-
const app = fixture.componentInstance;
20-
expect(app.title).toEqual('application');
21-
});
17+
// it(`should have the 'application' title`, () => {
18+
// const fixture = TestBed.createComponent(AppComponent);
19+
// const app = fixture.componentInstance;
20+
// expect(app.title).toEqual('application');
21+
// });
2222

23-
it('should render title', () => {
24-
const fixture = TestBed.createComponent(AppComponent);
25-
fixture.detectChanges();
26-
const compiled = fixture.nativeElement as HTMLElement;
27-
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, application');
28-
});
29-
});
23+
// it('should render title', () => {
24+
// const fixture = TestBed.createComponent(AppComponent);
25+
// fixture.detectChanges();
26+
// const compiled = fixture.nativeElement as HTMLElement;
27+
// expect(compiled.querySelector('h1')?.textContent).toContain('Hello, application');
28+
// });
29+
// });
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TitlecasePipe } from './titlecase.pipe';
2+
3+
describe('TitlecasePipe', () => {
4+
const pipe = new TitlecasePipe();
5+
6+
it('transforms "abc" to "Abc"', () => {
7+
expect(pipe.transform('abc')).toBe('Abc');
8+
});
9+
10+
it('transforms "abc def" to "Abc Def"', () => {
11+
expect(pipe.transform('abc def')).toBe('Abc Def');
12+
});
13+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'titlecase',
5+
pure: true,
6+
})
7+
export class TitlecasePipe implements PipeTransform {
8+
transform(input: string): string {
9+
return input.length === 0
10+
? ''
11+
: input.replace(
12+
/\w\S*/g,
13+
(txt) => txt[0].toUpperCase() + txt.slice(1).toLowerCase()
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)