-
Notifications
You must be signed in to change notification settings - Fork 118
/
kv-rich-text.component.spec.ts
172 lines (136 loc) · 4.87 KB
/
kv-rich-text.component.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { KvRichTextComponent } from './kv-rich-text.component';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MockNotificationService } from '../../../tests/mocks/notification';
import { FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { TooltipModule } from 'ngx-bootstrap';
import { NotificationService } from '../../services/notification.service';
import { ClipboardModule, ClipboardService } from 'ngx-clipboard';
describe('KvRichTextComponent', () => {
let component: KvRichTextComponent;
let fixture: ComponentFixture<KvRichTextComponent>;
const notificationService = new MockNotificationService();
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
KvRichTextComponent
],
imports: [
FormsModule,
ReactiveFormsModule,
ClipboardModule,
TooltipModule.forRoot()
],
providers: [
{ provide: NotificationService, useValue: notificationService },
ClipboardService
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(KvRichTextComponent);
component = fixture.componentInstance;
});
it('should be created', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
describe('Valid and invalid state (no custom validation)', () => {
it('Should be valid', () => {
component.text = 'foo=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = 'foo=bar\nfoo1=foo2';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = '=';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = '=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = 'foo=';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
});
it('Should be invalid', () => {
component.text = 'foo';
fixture.detectChanges();
expect(component).toBeTruthy();
expect(component.isInvalid).toBeTruthy();
component.text = 'foo=bar\nfoo1';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
});
});
describe('Valid and invalid state with custom validation(s) on the key/value', () => {
beforeEach(() => {
component.validators.value = [];
component.validators.key = [];
});
it('Validator on the key', () => {
component.validators.key = [Validators.required];
component.text = 'foo=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = 'foo=';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = '=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
component.text = '=';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
});
it('Validator on the value', () => {
component.validators.value = [Validators.required];
component.text = 'foo=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = '=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = 'foo=';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
component.text = '=';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
});
it('Validator on the key and the value', () => {
component.validators.key = [Validators.required];
component.validators.value = [Validators.required];
component.text = 'foo=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeFalsy();
component.text = '=bar';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
component.text = 'foo=';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
component.text = '=';
fixture.detectChanges();
expect(component.isInvalid).toBeTruthy();
});
});
describe('Import a file and copy clipboard', () => {
it('should import a file', (done) => {
const event = { target: { files: [new Blob(['a=a'])] } };
component.fileChange(event);
setTimeout(() => {
fixture.detectChanges();
expect(component.form.get('textarea').value).toContain('a=a');
done();
}, 1000);
});
it('should copy the content and display a success notification', () => {
component.form.get('textarea').setValue('foo=bar');
fixture.detectChanges();
component.copyClipboard();
fixture.detectChanges();
expect(notificationService.testSuccess[0]).toContain('The content have been copied to your clipboard.');
});
});
});