-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotation-input.component.spec.ts
More file actions
216 lines (188 loc) · 7.93 KB
/
annotation-input.component.spec.ts
File metadata and controls
216 lines (188 loc) · 7.93 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { FormArray, FormGroup } from "@angular/forms";
import { AnnotationInputComponent } from "./annotation-input.component";
import { provideHttpClientTesting } from "@angular/common/http/testing";
import { provideHttpClient } from "@angular/common/http";
import { ActivatedRoute, Router } from "@angular/router";
import { of } from "rxjs";
import { Dataset, KnowledgeBaseRelationship, Problem, EntailmentLabel } from "../../types";
describe("AnnotationInputComponent", () => {
let component: AnnotationInputComponent;
let fixture: ComponentFixture<AnnotationInputComponent>;
let mockRouter: jasmine.SpyObj<Router>;
let mockActivatedRoute: any;
beforeEach(async () => {
const routerSpy = jasmine.createSpyObj('Router', ['navigate']);
mockActivatedRoute = {
params: of({ problemId: "1" }),
snapshot: {
paramMap: {
get: jasmine.createSpy('get').and.returnValue("17")
}
}
};
await TestBed.configureTestingModule({
imports: [AnnotationInputComponent],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
{ provide: Router, useValue: routerSpy }
],
}).compileComponents();
mockRouter = TestBed.inject(Router) as jasmine.SpyObj<Router>;
fixture = TestBed.createComponent(AnnotationInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it("should create", () => {
expect(component).toBeTruthy();
});
describe('buildForm', () => {
it('should build form with correct structure and values from problem data', () => {
const mockProblem: Problem = {
id: 123,
base: null,
hidden: false,
premises: ["First premise", "Second premise"],
hypothesis: "Test hypothesis",
entailmentLabel: EntailmentLabel.ENTAILMENT,
kbAnnotations: [
{
id: 456,
entity1: "cat",
entity2: "animal",
relationship: KnowledgeBaseRelationship.SUBSET,
createdAt: "",
createdBy: "",
removedAt: null,
removedBy: null,
notes: "",
session: null,
removable: true
},
{
id: 789,
entity1: "dog",
entity2: "pet",
relationship: KnowledgeBaseRelationship.EQUAL,
createdAt: "",
createdBy: "",
removedAt: null,
removedBy: null,
notes: "",
session: null,
removable: true
}
],
labelAnnotations: [],
dataset: Dataset.USER,
extraData: null
};
// Access private method.
const form = component['buildForm'](mockProblem);
// Test form structure
expect(form).toBeTruthy();
expect(form.get('id')?.value).toBe(123);
expect(form.get('hypothesis')?.value).toBe('Test hypothesis');
// Test premises array
const premisesArray = form.controls.premises;
expect(premisesArray.length).toBe(2);
expect(premisesArray.controls[0].value).toBe('First premise');
expect(premisesArray.controls[1].value).toBe('Second premise');
// Test knowledge base items
const kbItemsArray = form.controls.kbItems;
expect(kbItemsArray.length).toBe(2);
const firstKbForm = kbItemsArray.controls[0];
expect(firstKbForm.value.id).toBe(456);
expect(firstKbForm.value.entity1).toBe('cat');
expect(firstKbForm.value.entity2).toBe('animal');
expect(firstKbForm.value.relationship).toBe(KnowledgeBaseRelationship.SUBSET);
const secondKbForm = kbItemsArray.controls[1];
expect(secondKbForm.value.id).toBe(789);
expect(secondKbForm.value.entity1).toBe('dog');
expect(secondKbForm.value.entity2).toBe('pet');
expect(secondKbForm.value.relationship).toBe(KnowledgeBaseRelationship.EQUAL);
});
it('should handle empty premises and kbItems arrays', () => {
const mockProblem: Problem = {
id: 123,
base: null,
hidden: false,
premises: [],
hypothesis: "Empty test hypothesis",
entailmentLabel: EntailmentLabel.NEUTRAL,
kbAnnotations: [],
labelAnnotations: [],
dataset: Dataset.USER,
extraData: null
};
const form = component['buildForm'](mockProblem);
expect(form.get('id')?.value).toBe(123);
expect(form.get('hypothesis')?.value).toBe('Empty test hypothesis');
const premisesArray = form.get('premises') as FormArray;
expect(premisesArray.length).toBe(0);
});
it('should create form controls with required validators', () => {
const mockProblem: Problem = {
id: 1,
base: null,
hidden: false,
premises: ["Test premise"],
hypothesis: "Test hypothesis",
entailmentLabel: EntailmentLabel.CONTRADICTION,
dataset: Dataset.USER,
extraData: null,
kbAnnotations: [],
labelAnnotations: [],
};
const form = component['buildForm'](mockProblem);
const hypothesisControl = form.get('hypothesis');
expect(hypothesisControl?.hasError('required')).toBeFalsy();
hypothesisControl?.setValue('');
expect(hypothesisControl?.hasError('required')).toBeTruthy();
});
});
describe('navigateToNewProblem', () => {
it('should navigate when problem ID is different from current route', () => {
const mockProblem: Problem = {
id: 12,
base: null,
hidden: false,
premises: [],
hypothesis: "",
entailmentLabel: EntailmentLabel.UNKNOWN,
dataset: Dataset.USER,
extraData: null,
kbAnnotations: [],
labelAnnotations: [],
};
component['navigateToNewProblem'](mockProblem);
expect(mockRouter.navigate).toHaveBeenCalledWith(
['/annotate', 12],
{ queryParamsHandling: 'preserve' }
);
});
it('should not navigate when problem ID matches current route', () => {
const mockProblem: Problem = {
id: 17,
base: null,
hidden: false,
premises: [],
hypothesis: "",
entailmentLabel: EntailmentLabel.UNKNOWN,
dataset: Dataset.USER,
extraData: null,
kbAnnotations: [],
labelAnnotations: [],
};
component['navigateToNewProblem'](mockProblem);
expect(mockRouter.navigate).not.toHaveBeenCalled();
});
it('should not navigate when problem is null', () => {
// Call the private method with null
component['navigateToNewProblem'](null);
expect(mockRouter.navigate).not.toHaveBeenCalled();
});
});
});