/
upload.component.spec.ts
199 lines (156 loc) · 6.96 KB
/
upload.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
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
import { Component, OnInit, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormBuilder, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { CreateStillImageFileValue } from '@dasch-swiss/dsp-js';
import { DspActionModule } from '@dasch-swiss/dsp-ui';
import { of } from 'rxjs';
import { SplitPipe } from 'src/app/main/pipes/split.pipe';
import { UploadFileService } from './upload-file.service';
import { UploadComponent } from './upload.component';
/**
* test host component to simulate parent component.
*/
@Component({
template: `
<app-upload #upload [representation]="representation" [parentForm]="form"></app-upload>`
})
class TestHostComponent implements OnInit {
@ViewChild('upload') uploadComp: UploadComponent;
representation = 'stillImage';
form: FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.form = this._fb.group({});
}
}
describe('UploadComponent', () => {
const mockFile = new File(['1'], 'testfile', { type: 'image/jpeg' });
const fb = new FormBuilder();
let testHostComponent: TestHostComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
beforeEach(waitForAsync(() => {
const uploadServiceSpy = jasmine.createSpyObj('UploadFileService', ['upload']);
TestBed.configureTestingModule({
declarations: [
UploadComponent,
TestHostComponent,
SplitPipe
],
imports: [
DspActionModule,
MatInputModule,
MatSnackBarModule,
ReactiveFormsModule,
MatIconModule
],
providers: [
{
provide: UploadFileService,
useValue: uploadServiceSpy
}
]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(TestHostComponent);
testHostComponent = testHostFixture.componentInstance;
testHostFixture.detectChanges();
});
it('should create', () => {
expect(testHostComponent).toBeTruthy();
expect(testHostComponent.uploadComp).toBeTruthy();
});
it('should display file type', () => {
expect(testHostComponent.uploadComp.representation).toBeDefined();
});
it('should be created without a file', () => {
expect(testHostComponent.uploadComp.file).toBeFalsy();
});
it('should delete attachment', () => {
testHostComponent.uploadComp.file = mockFile;
testHostComponent.uploadComp.fileControl.setValue(mockFile);
testHostComponent.uploadComp.thumbnailUrl = 'test';
testHostComponent.uploadComp.deleteAttachment();
expect(testHostComponent.uploadComp.file).toBeNull();
expect(testHostComponent.uploadComp.fileControl.value).toBeNull();
expect(testHostComponent.uploadComp.thumbnailUrl).toBeNull();
});
describe('form', () => {
it('should create form group and file control and add it to the parent form', waitForAsync(() => {
testHostFixture.whenStable().then(() => {
expect(testHostComponent.uploadComp.form).toBeDefined();
expect(testHostComponent.uploadComp.fileControl).toBeTruthy();
// check that the form control has been added to the parent form
expect(testHostComponent.form.contains('file')).toBe(true);
});
}));
it('should reset the form', () => {
testHostComponent.uploadComp.form = fb.group({ test: '' });
testHostComponent.uploadComp.resetForm();
expect(testHostComponent.uploadComp.form.get('test').value).toBeNull();
});
});
describe('isFileTypeSupported', () => {
it('should return true for the supported image files', () => {
const fileTypes = ['image/jpeg', 'image/jp2', 'image/tiff', 'image/tiff-fx', 'image/png'];
for (const type of fileTypes) {
expect(testHostComponent.uploadComp['_isFileTypeSupported'](type)).toBeTruthy();
}
});
it('should return false for unsupported image files', () => {
// --> TODO: add real unsupported filetypes?
const fileTypes = ['image/a', 'image/b', 'image/c', 'image/d', 'image/e'];
for (const type of fileTypes) {
expect(testHostComponent.uploadComp['_isFileTypeSupported'](type)).toBeFalsy();
}
});
});
describe('isMoreThanOneFile', () => {
it('should return false for one file array', () => {
const filesArray: File[] = [];
filesArray.push(mockFile);
expect(testHostComponent.uploadComp['_isMoreThanOneFile'](filesArray)).toBeFalsy();
});
it('should return false for more than one file', () => {
const filesArray: File[] = [];
filesArray.push(mockFile, mockFile, mockFile);
expect(testHostComponent.uploadComp['_isMoreThanOneFile'](filesArray)).toBeTruthy();
});
});
describe('addFile', () => {
it('should make a request to Sipi when a file is added', () => {
expect(testHostComponent.uploadComp.form.valid).toBe(false);
const uploadService = TestBed.inject(UploadFileService) as jasmine.SpyObj<UploadFileService>;
uploadService.upload.and.returnValue(of({
uploadedFiles: [
{
fileType: 'image',
temporaryUrl: 'http://localhost:1024/tmp/8oDdefPSkaz-EG187srxBFZ.jp2',
originalFilename: 'beaver.jpg',
internalFilename: '8oDdefPSkaz-EG187srxBFZ.jp2'
}
]
}));
// https://stackoverflow.com/questions/57080760/fake-file-drop-event-for-unit-testing
const drop = {
preventDefault: () => { },
stopPropagation: () => { },
target: { files: [mockFile] }
};
testHostComponent.uploadComp.addFile(drop);
expect(testHostComponent.uploadComp.form.valid).toBe(true);
const createFileVal = testHostComponent.uploadComp.getNewValue();
expect(createFileVal instanceof CreateStillImageFileValue).toBe(true);
expect((createFileVal as CreateStillImageFileValue).filename).toEqual('8oDdefPSkaz-EG187srxBFZ.jp2');
const expectedFormData = new FormData();
expectedFormData.append(mockFile.name, mockFile);
expect(uploadService.upload).toHaveBeenCalledTimes(1);
expect(uploadService.upload).toHaveBeenCalledWith(expectedFormData);
});
});
});