forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathscripts-select.component.spec.ts
More file actions
194 lines (164 loc) · 5.31 KB
/
scripts-select.component.spec.ts
File metadata and controls
194 lines (164 loc) · 5.31 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
import { NO_ERRORS_SCHEMA } from '@angular/core';
import {
ComponentFixture,
fakeAsync,
TestBed,
tick,
waitForAsync,
} from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import {
TranslateLoader,
TranslateModule,
} from '@ngx-translate/core';
import { buildPaginatedList } from '../../../core/data/paginated-list.model';
import { ScriptDataService } from '../../../core/data/processes/script-data.service';
import { TranslateLoaderMock } from '../../../shared/mocks/translate-loader.mock';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { ActivatedRouteStub } from '../../../shared/testing/active-router.stub';
import { RouterStub } from '../../../shared/testing/router.stub';
import { Script } from '../../scripts/script.model';
import { ScriptsSelectComponent } from './scripts-select.component';
describe('ScriptsSelectComponent', () => {
let component: ScriptsSelectComponent;
let fixture: ComponentFixture<ScriptsSelectComponent>;
let scriptService;
let script1;
let script2;
function init() {
script1 = new Script();
script2 = new Script();
scriptService = jasmine.createSpyObj('scriptService',
{
findAll: createSuccessfulRemoteDataObject$(buildPaginatedList(undefined, [script1, script2])),
},
);
}
beforeEach(waitForAsync(() => {
init();
TestBed.configureTestingModule({
imports: [
FormsModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock,
},
}),
ScriptsSelectComponent,
],
providers: [
{ provide: ScriptDataService, useValue: scriptService },
{ provide: Router, useClass: RouterStub },
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
],
schemas: [NO_ERRORS_SCHEMA],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ScriptsSelectComponent);
component = fixture.componentInstance;
(component as any)._selectedScript = new Script();
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should not show a validation error if the input field was left untouched but left empty', () => {
const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeFalsy();
});
it('should show a validation error if the input field was touched but left empty', fakeAsync(() => {
(component as any)._selectedScript.id = '';
fixture.detectChanges();
tick();
const select = fixture.debugElement.query(By.css('#process-script'));
select.triggerEventHandler('blur', null);
fixture.detectChanges();
const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeTruthy();
}));
it('should not show a validation error if the input field was touched but not left empty', fakeAsync(() => {
(component as any)._selectedScript.id = 'testValue';
fixture.detectChanges();
tick();
const select = fixture.debugElement.query(By.css('#process-script'));
select.triggerEventHandler('blur', null);
fixture.detectChanges();
const validationError = fixture.debugElement.query(By.css('.validation-error'));
expect(validationError).toBeFalsy();
}));
it('should load more scripts when scrolled to the bottom', fakeAsync(() => {
spyOn(component, 'loadScripts');
const event = {
target: {
scrollTop: 100,
clientHeight: 200,
scrollHeight: 300,
},
};
component.onScroll(event);
tick();
expect(component.loadScripts).toHaveBeenCalled();
}));
it('should load more scripts when scrolled almost to the bottom', fakeAsync(() => {
spyOn(component, 'loadScripts');
const event = {
target: {
scrollTop: 99,
clientHeight: 200,
scrollHeight: 300,
},
};
component.onScroll(event);
tick();
expect(component.loadScripts).toHaveBeenCalled();
}));
it('should not load more scripts if already loading', fakeAsync(() => {
spyOn(component, 'loadScripts');
component.isLoading$.next(true);
const event = {
target: {
scrollTop: 100,
clientHeight: 200,
scrollHeight: 300,
},
};
component.onScroll(event);
tick();
expect(component.loadScripts).not.toHaveBeenCalled();
}));
it('should not load more scripts if it is the last page', fakeAsync(() => {
spyOn(component, 'loadScripts');
(component as any)._isLastPage = true;
const event = {
target: {
scrollTop: 100,
clientHeight: 200,
scrollHeight: 300,
},
};
component.onScroll(event);
tick();
expect(component.loadScripts).not.toHaveBeenCalled();
}));
it('should not load more scripts if not scrolled to the bottom', fakeAsync(() => {
spyOn(component, 'loadScripts');
const event = {
target: {
scrollTop: 50,
clientHeight: 200,
scrollHeight: 300,
},
};
component.onScroll(event);
tick();
expect(component.loadScripts).not.toHaveBeenCalled();
}));
});