/
yn.pipe.spec.ts
93 lines (84 loc) · 2.91 KB
/
yn.pipe.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { AlainThemeModule } from '../../theme.module';
import { YNMode } from './yn.pipe';
describe('Pipe: yn', () => {
let fixture: ComponentFixture<TestComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AlainThemeModule.forRoot()],
declarations: [TestComponent],
});
});
describe('', () => {
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
});
[
{ value: true, result: `是` },
{ value: false, result: `否` },
{
value: true,
result: `好`,
yes: '好',
no: '坏',
},
{
value: false,
result: `坏`,
yes: '好',
no: '坏',
},
].forEach((item: any) => {
it(`${item.value.toString()} muse be ${item.result}`, () => {
fixture.componentInstance.value = item.value;
fixture.componentInstance.yes = item.yes;
fixture.componentInstance.no = item.no;
fixture.detectChanges();
expect((fixture.debugElement.query(By.css('#result')).nativeElement as HTMLElement).innerHTML).toContain(item.result);
});
});
describe('#mode', () => {
it('with text', () => {
fixture.componentInstance.mode = 'text';
fixture.componentInstance.value = true;
fixture.detectChanges();
expect(fixture.debugElement.queryAll(By.css('svg')).length).toBe(0);
fixture.componentInstance.value = false;
fixture.detectChanges();
expect(fixture.debugElement.queryAll(By.css('svg')).length).toBe(0);
});
it('with full', () => {
fixture.componentInstance.mode = 'full';
fixture.componentInstance.value = true;
fixture.detectChanges();
let html = (fixture.debugElement.query(By.css('#result')).nativeElement as HTMLElement).innerHTML;
expect(html).toContain('<svg');
expect(html).not.toContain(`title="`);
// when false
fixture.componentInstance.value = false;
fixture.detectChanges();
html = (fixture.debugElement.query(By.css('#result')).nativeElement as HTMLElement).innerHTML;
expect(html).toContain('<svg');
expect(html).not.toContain(`title="`);
});
});
});
it('should be used default config', () => {
TestBed.overrideTemplate(TestComponent, `<div id="result" [innerHTML]="value | yn"></div>`);
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect((fixture.debugElement.query(By.css('#result')).nativeElement as HTMLElement).innerHTML).toContain(`是`);
});
});
@Component({
template: ` <div id="result" [innerHTML]="value | yn: yes:no:mode"></div> `,
})
class TestComponent {
value = true;
yes: string;
no: string;
mode: YNMode;
}