/
ontology-form.component.spec.ts
184 lines (142 loc) · 6.19 KB
/
ontology-form.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
173
174
175
176
177
178
179
180
181
182
183
184
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { KnoraApiConnection, MockProjects } from '@dasch-swiss/dsp-js';
import { AppInitService, DspActionModule } from '@dasch-swiss/dsp-ui';
import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import { CacheService } from 'src/app/main/cache/cache.service';
import { DspApiConfigToken, DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { TestConfig } from 'test.config';
import { OntologyFormComponent } from './ontology-form.component';
describe('OntologyFormComponent', () => {
let ontologyFormComponent: OntologyFormComponent;
let ontologyFormFixture: ComponentFixture<OntologyFormComponent>;
const formBuilder: FormBuilder = new FormBuilder();
const cacheServiceSpy = jasmine.createSpyObj('CacheService', ['get']);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
OntologyFormComponent
],
imports: [
BrowserAnimationsModule,
DspActionModule,
HttpClientTestingModule,
MatFormFieldModule,
MatInputModule,
ReactiveFormsModule,
RouterTestingModule,
TranslateModule.forRoot()
],
providers: [
// reference the new instance of formBuilder from above
{ provide: FormBuilder, useValue: formBuilder },
AppInitService,
{
provide: DspApiConfigToken,
useValue: TestConfig.ApiConfig
},
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
},
{
provide: CacheService,
useValue: cacheServiceSpy
}
]
})
.compileComponents();
}));
beforeEach(() => {
// mock cache service for currentOntology
const cacheSpy = TestBed.inject(CacheService);
(cacheSpy as jasmine.SpyObj<CacheService>).get.and.callFake(
() => {
const response = MockProjects.mockProject();
return of(response);
}
);
ontologyFormFixture = TestBed.createComponent(OntologyFormComponent);
ontologyFormComponent = ontologyFormFixture.componentInstance;
ontologyFormComponent.projectCode = '00FF';
// ontologyFormComponent.iri = 'http://0.0.0.0:3333/ontology/0001/anything/v2';
ontologyFormComponent.existingOntologyNames = ['images'];
ontologyFormComponent.ngOnInit();
ontologyFormFixture.detectChanges();
});
it('should create', () => {
expect(ontologyFormComponent).toBeTruthy();
});
it('should render input elements', () => {
const compiled = ontologyFormFixture.debugElement;
const nameInput = compiled.query(By.css('.ontology-name input'));
const labelInput = compiled.query(By.css('.ontology-label input'));
const commentInput = compiled.query(By.css('.ontology-comment textarea'));
expect(nameInput).toBeTruthy();
expect(labelInput).toBeTruthy();
expect(commentInput).toBeTruthy();
});
it('should test form validity with allowed name', () => {
const form = ontologyFormComponent.ontologyForm;
expect(form.valid).toBeFalsy();
const nameInput = form.controls.name;
nameInput.setValue('biblio');
expect(form.valid).toBeTruthy();
});
it('should test form validity with forbidden names', () => {
const form = ontologyFormComponent.ontologyForm;
expect(form.valid).toBeFalsy();
const nameInput = form.controls.name;
nameInput.setValue('ontology');
expect(form.valid).toBeFalsy();
nameInput.setValue('images');
expect(form.valid).toBeFalsy();
});
it('should test form validity with not allowed names', () => {
const form = ontologyFormComponent.ontologyForm;
expect(form.valid).toBeFalsy();
const nameInput = form.controls.name;
nameInput.setValue('v2onto');
expect(form.valid).toBeFalsy();
nameInput.setValue('my-onto');
expect(form.valid).toBeTruthy();
nameInput.setValue('my_onto');
expect(form.valid).toBeTruthy();
nameInput.setValue('my.onto');
expect(form.valid).toBeTruthy();
nameInput.setValue('2ndOnto');
expect(form.valid).toBeFalsy();
nameInput.setValue('-notAllowed');
expect(form.valid).toBeFalsy();
nameInput.setValue('_notAllowed');
expect(form.valid).toBeFalsy();
nameInput.setValue('no$or€');
expect(form.valid).toBeFalsy();
nameInput.setValue('no spaces');
expect(form.valid).toBeFalsy();
nameInput.setValue('ältereOnto');
expect(form.valid).toBeFalsy();
nameInput.setValue('bestOnto');
expect(form.valid).toBeTruthy();
});
it('should test form validity without label', () => {
const form = ontologyFormComponent.ontologyForm;
expect(form.valid).toBeFalsy();
const nameInput = form.controls.name;
const labelInput = form.controls.label;
nameInput.setValue('biblio');
const compiled = ontologyFormFixture.debugElement;
expect(ontologyFormComponent.ontologyLabel).toEqual('Biblio');
expect(form.valid).toBeTruthy();
labelInput.setValue('Biblio Ontology');
expect(ontologyFormComponent.ontologyForm.controls.label.value).toEqual('Biblio Ontology');
expect(form.valid).toBeTruthy();
});
});