-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathuploader.component.spec.ts
More file actions
220 lines (177 loc) · 8.37 KB
/
Copy pathuploader.component.spec.ts
File metadata and controls
220 lines (177 loc) · 8.37 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
217
218
219
220
import { HttpXsrfTokenExtractor } from '@angular/common/http';
import {
ChangeDetectorRef,
Component,
CUSTOM_ELEMENTS_SCHEMA,
} from '@angular/core';
import {
ComponentFixture,
inject,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import { CookieService } from '@dspace/core/cookies/cookie.service';
import { DragService } from '@dspace/core/drag.service';
import { CookieServiceMock } from '@dspace/core/testing/cookie.service.mock';
import { HttpXsrfTokenExtractorMock } from '@dspace/core/testing/http-xsrf-token-extractor.mock';
import { createTestComponent } from '@dspace/core/testing/utils.test';
import { TranslateModule } from '@ngx-translate/core';
import { FileUploadModule } from 'ng2-file-upload';
import { LiveRegionService } from '../../live-region/live-region.service';
import { getLiveRegionServiceStub } from '../../live-region/live-region.service.stub';
import { UploaderComponent } from './uploader.component';
import { UploaderOptions } from './uploader-options.model';
describe('UploaderComponent', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
let html;
/**
* Bring an injected UploaderComponent instance to the state in which its ng2-file-upload
* callbacks are installed, so that `app.uploader.onCompleteItem(...)` runs the component's code.
*/
const driveUploader = (app: UploaderComponent): void => {
app.uploadFilesOptions = Object.assign(new UploaderOptions(), {
url: 'http://test',
authToken: null,
disableMultipart: false,
itemAlias: null,
});
app.ngOnInit();
app.ngAfterViewInit();
};
// waitForAsync beforeEach
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
FileUploadModule,
TranslateModule.forRoot(),
UploaderComponent,
TestComponent,
],
providers: [
ChangeDetectorRef,
UploaderComponent,
DragService,
{ provide: HttpXsrfTokenExtractor, useValue: new HttpXsrfTokenExtractorMock('mock-token') },
{ provide: CookieService, useValue: new CookieServiceMock() },
{ provide: LiveRegionService, useValue: getLiveRegionServiceStub() },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
});
}));
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-uploader [onBeforeUpload]="onBeforeUpload"
[uploadFilesOptions]="uploadFilesOptions"
(onCompleteItem)="onCompleteItem($event)"></ds-uploader>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create Uploader Component', inject([UploaderComponent], (app: UploaderComponent) => {
expect(app).toBeDefined();
}));
it('should emit both the legacy completion output and the new completion event on a completed upload', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItem, 'emit');
spyOn(app.onCompleteItemWithFile, 'emit');
const parsed = { foo: 'bar' };
app.uploader.onCompleteItem({ file: { name: 'test.pdf' } } as any, JSON.stringify(parsed), 200, {});
expect(app.onCompleteItem.emit).toHaveBeenCalledWith(parsed);
expect(app.onCompleteItem.emit).toHaveBeenCalledTimes(1);
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: parsed, fileName: 'test.pdf' });
}));
it('should emit a distinct file name for each of two sequential completed uploads', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({ file: { name: 'first.pdf' } } as any, JSON.stringify({ n: 1 }), 200, {});
app.uploader.onCompleteItem({ file: { name: 'second.pdf' } } as any, JSON.stringify({ n: 2 }), 200, {});
const calls = (app.onCompleteItemWithFile.emit as jasmine.Spy).calls;
expect(calls.count()).toBe(2);
expect(calls.argsFor(0)[0]).toEqual({ response: { n: 1 }, fileName: 'first.pdf' });
expect(calls.argsFor(1)[0]).toEqual({ response: { n: 2 }, fileName: 'second.pdf' });
}));
it('should omit fileName from the completion event when the item is undefined', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem(undefined, JSON.stringify({ foo: 'bar' }), 200, {});
const arg = (app.onCompleteItemWithFile.emit as jasmine.Spy).calls.mostRecent().args[0];
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: { foo: 'bar' } });
expect(Object.keys(arg)).toEqual(['response']);
expect('fileName' in arg).toBeFalse();
}));
it('should omit fileName from the completion event when the item has no file', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({} as any, JSON.stringify({ foo: 'bar' }), 200, {});
const arg = (app.onCompleteItemWithFile.emit as jasmine.Spy).calls.mostRecent().args[0];
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: { foo: 'bar' } });
expect(Object.keys(arg)).toEqual(['response']);
expect('fileName' in arg).toBeFalse();
}));
it('should omit fileName from the completion event when the file has no name', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({ file: {} } as any, JSON.stringify({ foo: 'bar' }), 200, {});
const arg = (app.onCompleteItemWithFile.emit as jasmine.Spy).calls.mostRecent().args[0];
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: { foo: 'bar' } });
expect(Object.keys(arg)).toEqual(['response']);
expect('fileName' in arg).toBeFalse();
}));
it('should omit fileName from the completion event when the file name is an empty string', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({ file: { name: '' } } as any, JSON.stringify({ foo: 'bar' }), 200, {});
const arg = (app.onCompleteItemWithFile.emit as jasmine.Spy).calls.mostRecent().args[0];
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: { foo: 'bar' } });
expect('fileName' in arg).toBeFalse();
}));
it('should keep a whitespace-only file name on the completion event', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({ file: { name: ' ' } } as any, JSON.stringify({ foo: 'bar' }), 200, {});
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: { foo: 'bar' }, fileName: ' ' });
}));
it('should not emit either completion output when the response body is empty', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onCompleteItem, 'emit');
spyOn(app.onCompleteItemWithFile, 'emit');
app.uploader.onCompleteItem({ file: { name: 'test.pdf' } } as any, '', 204, {});
expect(app.onCompleteItem.emit).not.toHaveBeenCalled();
expect(app.onCompleteItemWithFile.emit).not.toHaveBeenCalled();
}));
it('should emit onUploadError with the item, response, status and headers of the failed upload', inject([UploaderComponent], (app: UploaderComponent) => {
driveUploader(app);
spyOn(app.onUploadError, 'emit');
app.uploader.onErrorItem({ file: { name: 'broken.zip' } } as any, 'boom', 500, {});
expect(app.onUploadError.emit).toHaveBeenCalledWith({
item: { file: { name: 'broken.zip' } },
response: 'boom',
status: 500,
headers: {},
});
}));
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: `<ds-uploader></ds-uploader>`,
imports: [
FileUploadModule,
UploaderComponent,
],
})
class TestComponent {
public uploadFilesOptions: UploaderOptions = Object.assign(new UploaderOptions(), {
url: 'http://test',
authToken: null,
disableMultipart: false,
itemAlias: null,
});
/* eslint-disable no-empty,@typescript-eslint/no-empty-function */
public onBeforeUpload = () => {
};
onCompleteItem(event) {
}
/* eslint-enable no-empty, @typescript-eslint/no-empty-function */
}