/
search-list-value.component.spec.ts
166 lines (121 loc) · 5.57 KB
/
search-list-value.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
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { Component, EventEmitter, Inject, Input, OnInit, Output, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { MatButtonHarness } from '@angular/material/button/testing';
import { MatDialogModule } from '@angular/material/dialog';
import { MatInputModule } from '@angular/material/input';
import { MatMenu, MatMenuModule } from '@angular/material/menu';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { By } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ListNodeV2, ListsEndpointV2, MockList, MockOntology, ResourcePropertyDefinition } from '@dasch-swiss/dsp-js';
import { of } from 'rxjs';
import { DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { IRI } from '../operator';
import { SearchListValueComponent } from './search-list-value.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-search-list-value #listVal [formGroup]="form" [property]="property"></app-search-list-value>`
})
class TestHostComponent implements OnInit {
@ViewChild('listVal', { static: false }) listValue: SearchListValueComponent;
form;
property: ResourcePropertyDefinition;
constructor(@Inject(FormBuilder) private _fb: FormBuilder) {
}
ngOnInit() {
this.form = this._fb.group({});
const anythingOnto = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2');
this.property = anythingOnto.properties['http://0.0.0.0:3333/ontology/0001/anything/v2#hasListItem'] as ResourcePropertyDefinition;
}
}
/**
* test component to simulate date value component.
*/
@Component({
selector: 'app-search-display-list',
template: '<mat-menu #childMenu="matMenu" [overlapTrigger]="false"></mat-menu>'
})
class TestSearchDisplayListComponent implements OnInit {
@Input() children: ListNodeV2[];
@Output() selectedNode: EventEmitter<ListNodeV2> = new EventEmitter<ListNodeV2>();
@ViewChild('childMenu', { static: true }) public childMenu: MatMenu;
ngOnInit() {
}
}
describe('SearchListValueComponent', () => {
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
let loader: HarnessLoader;
beforeEach(waitForAsync(() => {
const listSpyObj = {
v2: {
list: jasmine.createSpyObj('list', ['getList'])
}
};
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
MatDialogModule,
MatInputModule,
MatMenuModule,
MatSnackBarModule,
ReactiveFormsModule
],
declarations: [
SearchListValueComponent,
TestHostComponent,
TestSearchDisplayListComponent
],
providers: [
{
provide: DspApiConnectionToken,
useValue: listSpyObj
}
]
})
.compileComponents();
}));
beforeEach(() => {
const listSpy = TestBed.inject(DspApiConnectionToken);
(listSpy.v2.list as jasmine.SpyObj<ListsEndpointV2>).getList.and.callFake(
(rootListNodeIri) => of(MockList.mockList('http://rdfh.ch/lists/0001/treeList'))
);
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
loader = TestbedHarnessEnvironment.loader(testHostFixture);
testHostFixture.detectChanges();
});
it('should create', () => {
const dspSpy = TestBed.inject(DspApiConnectionToken);
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.listValue).toBeTruthy();
expect(testHostComponent.listValue.listRootNode).toBeDefined();
expect(testHostComponent.listValue.listRootNode.isRootNode).toBe(true);
expect(dspSpy.v2.list.getList).toHaveBeenCalledTimes(1);
expect(dspSpy.v2.list.getList).toHaveBeenCalledWith('http://rdfh.ch/lists/0001/treeList');
});
it('should emit the selected node', async () => {
const button = await loader.getHarness(MatButtonHarness);
expect(await button.getText()).toEqual('Select list value');
await button.click();
const hostCompDe = testHostFixture.debugElement;
const searchDisplayListComponent = hostCompDe.query(By.directive(TestSearchDisplayListComponent));
expect(searchDisplayListComponent).not.toBeNull();
const listNode = new ListNodeV2();
listNode.id = 'http://rdfh.ch/lists/0001/treeList03';
(searchDisplayListComponent.componentInstance as TestSearchDisplayListComponent).selectedNode.emit(listNode);
expect(testHostComponent.listValue.form.controls['listValue'].value).toEqual('http://rdfh.ch/lists/0001/treeList03');
});
it('should get the selected list node', () => {
testHostComponent.listValue.form.controls['listValue'].setValue('http://rdfh.ch/lists/0001/treeList/01');
const expectedListNode = new IRI('http://rdfh.ch/lists/0001/treeList/01');
const listNode = testHostComponent.listValue.getValue();
expect(listNode).toEqual(expectedListNode);
});
});