Newer
Older
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MatIconModule } from '@angular/material/icon';
import { SimpleChange } from '@angular/core';
import { AlertComponent } from './alert.component';
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let component: AlertComponent;
let fixture: ComponentFixture<AlertComponent>;
let returnedTexts: string[] = [];
const changeAttributeText = (value: string) => {
const previousValue = component.text;
component.text = value;
if (previousValue !== value) {
component.ngOnChanges({ text: new SimpleChange(previousValue, value, false) });
}
};
const changeAttributeCustomtext = (value: string) => {
const previousValue = component.customtext;
component.customtext = value;
if (previousValue !== value) {
component.ngOnChanges({ customtext: new SimpleChange(previousValue, value, false) });
}
};
const changeAttributeReplacements = (value: string[]) => {
const previousValue = component.replacements;
component.replacements = value;
if (JSON.stringify(previousValue) !== JSON.stringify(value)) {
component.ngOnChanges({ replacements: new SimpleChange(previousValue, value, false) });
}
};
const updateCustomText = (key: string, value: string): void => {
// eslint-disable-next-line @typescript-eslint/dot-notation
component['cts']['addCustomTexts']({ [key]: value });
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatIconModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AlertComponent);
component = fixture.componentInstance;
returnedTexts = [];
component.displayText$.subscribe(text => returnedTexts.push(text));
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should update on input change', () => {
changeAttributeText('A');
changeAttributeText('B');
changeAttributeText('B');
changeAttributeText('C');
expect(returnedTexts).toEqual(['A', 'B', 'C']);
});
it('should highlight ticks', () => {
changeAttributeText('A `in ticks` A');
changeAttributeText('B');
changeAttributeText('C `in ticks` C');
expect(returnedTexts).toEqual([
'A <span class=\'highlight\'>in ticks</span> A',
'B',
'C <span class=\'highlight\'>in ticks</span> C'
]);
});
it('should use and update the customtext if given', () => {
changeAttributeText('text value');
updateCustomText('customtext key', 'customtext value');
changeAttributeCustomtext('customtext key');
changeAttributeText('text value change is ignored as long as customtext is given and an existing key');
updateCustomText('customtext key', 'customtext value updated');
changeAttributeCustomtext('missing key');
updateCustomText('missing key', 'missing key got a value');
changeAttributeText('text value is used as defaultvalue for customtext');
updateCustomText('empty key', '');
changeAttributeCustomtext('empty key');
updateCustomText('customtext key', 'replacement: %s');
changeAttributeCustomtext('customtext key');
changeAttributeReplacements(['something']);
changeAttributeReplacements(['something else']);
updateCustomText('customtext key', 'replacement in ticks: `%s`');
// returnedTexts.forEach(s => console.log(s));
expect(returnedTexts).toEqual([
'text value',
'customtext value',
'customtext value updated',
'text value change is ignored as long as customtext is given and an existing key',
'missing key got a value',
'text value is used as defaultvalue for customtext',
'replacement: %s',
'replacement: something',
'replacement: something else',
'replacement in ticks: <span class=\'highlight\'>something else</span>'
]);
});
});