forked from UoEMainLibrary/dspace-datashare-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.component.spec.ts
More file actions
132 lines (109 loc) · 4.04 KB
/
Copy pathuploader.component.spec.ts
File metadata and controls
132 lines (109 loc) · 4.04 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
// Load the implementations that should be tested
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 { TranslateModule } from '@ngx-translate/core';
import { FileUploadModule } from 'ng2-file-upload';
import { DragService } from '../../../core/drag.service';
import { CookieService } from '../../../core/services/cookie.service';
import { CookieServiceMock } from '../../mocks/cookie.service.mock';
import { HttpXsrfTokenExtractorMock } from '../../mocks/http-xsrf-token-extractor.mock';
import { createTestComponent } from '../../testing/utils.test';
import { UploaderComponent } from './uploader.component';
import { UploaderOptions } from './uploader-options.model';
describe('Chips component', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
let html;
// 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() },
],
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 onCompleteItem and onCompleteItemWithFile on a completed upload', inject([UploaderComponent], (app: UploaderComponent) => {
app.uploadFilesOptions = Object.assign(new UploaderOptions(), {
url: 'http://test',
authToken: null,
disableMultipart: false,
itemAlias: null,
});
app.ngOnInit();
app.ngAfterViewInit();
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.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: parsed, fileName: 'test.pdf' });
}));
it('should emit onCompleteItemWithFile without a fileName when the item has no file name', inject([UploaderComponent], (app: UploaderComponent) => {
app.uploadFilesOptions = Object.assign(new UploaderOptions(), {
url: 'http://test',
authToken: null,
disableMultipart: false,
itemAlias: null,
});
app.ngOnInit();
app.ngAfterViewInit();
spyOn(app.onCompleteItemWithFile, 'emit');
const parsed = { foo: 'bar' };
app.uploader.onCompleteItem(undefined, JSON.stringify(parsed), 200, {});
expect(app.onCompleteItemWithFile.emit).toHaveBeenCalledWith({ response: parsed });
}));
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``,
standalone: true,
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 */
}