|
| 1 | +import { |
| 2 | + HttpClientTestingModule, |
| 3 | + HttpTestingController |
| 4 | +} from '@angular/common/http/testing'; |
1 | 5 | import { TestBed } from '@angular/core/testing';
|
| 6 | +import { environment } from 'environments/environment'; |
| 7 | + |
| 8 | +import { getOneFileMock } from '@mocks/file.mock'; |
2 | 9 |
|
3 | 10 | import { FileService } from './file.service';
|
4 | 11 |
|
| 12 | +const base_url = environment.base_url; |
| 13 | + |
5 | 14 | describe('FileService', () => {
|
6 |
| - let service: FileService; |
| 15 | + let fileService: FileService; |
| 16 | + let httpController: HttpTestingController; |
7 | 17 |
|
8 | 18 | beforeEach(() => {
|
9 |
| - TestBed.configureTestingModule({}); |
10 |
| - service = TestBed.inject(FileService); |
| 19 | + TestBed.configureTestingModule({ |
| 20 | + imports: [ |
| 21 | + HttpClientTestingModule, |
| 22 | + ] |
| 23 | + }); |
| 24 | + fileService = TestBed.inject(FileService); |
| 25 | + httpController = TestBed.inject(HttpTestingController); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + httpController.verify(); |
11 | 30 | });
|
12 | 31 |
|
13 | 32 | it('should be created', () => {
|
14 |
| - expect(service).toBeTruthy(); |
| 33 | + expect(fileService).toBeTruthy(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('create files', () => { |
| 37 | + it('should return true if the files were created', (doneFn) => { |
| 38 | + const files = [new File([''], 'test.txt')]; |
| 39 | + const folderID = '1234'; |
| 40 | + fileService.createFiles(files, folderID).subscribe((resp) => { |
| 41 | + expect(resp).toBeTrue(); |
| 42 | + doneFn(); |
| 43 | + }); |
| 44 | + |
| 45 | + const url = `${base_url}/file/upload/${folderID}`; |
| 46 | + const req = httpController.expectOne(url); |
| 47 | + |
| 48 | + expect(req.request.method).toBe('POST'); |
| 49 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 50 | + expect(req.request.body).toBeInstanceOf(FormData); |
| 51 | + req.flush({ ok: true, files: [] }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return false if the files were not created', (doneFn) => { |
| 55 | + const files = [new File([''], 'test.txt')]; |
| 56 | + const folderID = '1234'; |
| 57 | + fileService.createFiles(files, folderID).subscribe((resp) => { |
| 58 | + expect(resp).toBeFalse(); |
| 59 | + doneFn(); |
| 60 | + }); |
| 61 | + |
| 62 | + const url = `${base_url}/file/upload/${folderID}`; |
| 63 | + const req = httpController.expectOne(url); |
| 64 | + |
| 65 | + expect(req.request.method).toBe('POST'); |
| 66 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 67 | + expect(req.request.body).toBeInstanceOf(FormData); |
| 68 | + req.flush({ ok: false, files: [] }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should emit the files created', (doneFn) => { |
| 72 | + const files = [new File([''], 'test.txt')]; |
| 73 | + const folderID = '1234'; |
| 74 | + fileService.filesCreated.subscribe((resp) => { |
| 75 | + expect(resp).toEqual([]); |
| 76 | + doneFn(); |
| 77 | + }); |
| 78 | + fileService.createFiles(files, folderID).subscribe(); |
| 79 | + |
| 80 | + const url = `${base_url}/file/upload/${folderID}`; |
| 81 | + const req = httpController.expectOne(url); |
| 82 | + |
| 83 | + expect(req.request.method).toBe('POST'); |
| 84 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 85 | + expect(req.request.body).toBeInstanceOf(FormData); |
| 86 | + req.flush({ ok: true, files: [] }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should emit the total size of the files created', (doneFn) => { |
| 90 | + const files = [new File([''], 'test.txt')]; |
| 91 | + const folderID = '1234'; |
| 92 | + fileService.updateTotalSize.subscribe((resp) => { |
| 93 | + expect(resp).toEqual(0); |
| 94 | + doneFn(); |
| 95 | + }); |
| 96 | + fileService.createFiles(files, folderID).subscribe(); |
| 97 | + |
| 98 | + const url = `${base_url}/file/upload/${folderID}`; |
| 99 | + const req = httpController.expectOne(url); |
| 100 | + |
| 101 | + expect(req.request.method).toBe('POST'); |
| 102 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 103 | + expect(req.request.body).toBeInstanceOf(FormData); |
| 104 | + req.flush({ ok: true, files: [] }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('delete files', () => { |
| 109 | + it('should return true if the files were deleted', (doneFn) => { |
| 110 | + const fileID = '1234'; |
| 111 | + fileService.deleteFile(fileID).subscribe((resp) => { |
| 112 | + expect(resp).toBeTrue(); |
| 113 | + doneFn(); |
| 114 | + }); |
| 115 | + |
| 116 | + const url = `${base_url}/file/${fileID}`; |
| 117 | + const req = httpController.expectOne(url); |
| 118 | + |
| 119 | + expect(req.request.method).toBe('DELETE'); |
| 120 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 121 | + req.flush({ ok: true, file: {} }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return false if the files were not deleted', (doneFn) => { |
| 125 | + const fileID = '1234'; |
| 126 | + fileService.deleteFile(fileID).subscribe((resp) => { |
| 127 | + expect(resp).toBeFalse(); |
| 128 | + doneFn(); |
| 129 | + }); |
| 130 | + |
| 131 | + const url = `${base_url}/file/${fileID}`; |
| 132 | + const req = httpController.expectOne(url); |
| 133 | + |
| 134 | + expect(req.request.method).toBe('DELETE'); |
| 135 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 136 | + req.flush({ ok: false, file: {} }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('download files', () => { |
| 141 | + it('should return the file', (doneFn) => { |
| 142 | + const fileID = '1234'; |
| 143 | + fileService.downloadFile(fileID).subscribe((resp) => { |
| 144 | + expect(resp).toBeInstanceOf(Blob); |
| 145 | + doneFn(); |
| 146 | + }); |
| 147 | + |
| 148 | + const url = `${base_url}/file/download/${fileID}`; |
| 149 | + const req = httpController.expectOne(url); |
| 150 | + |
| 151 | + expect(req.request.method).toBe('GET'); |
| 152 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 153 | + req.flush(new Blob()); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('search files', () => { |
| 158 | + it("should return the files found with name 'test'", (doneFn) => { |
| 159 | + const search = 'test'; |
| 160 | + const filesExpected = [ |
| 161 | + { ...getOneFileMock(), name: 'test1' }, |
| 162 | + { ...getOneFileMock(), name: 'test22' }, |
| 163 | + { ...getOneFileMock(), name: 'test332' }, |
| 164 | + ]; |
| 165 | + fileService.searchFiles(search).subscribe(({ files }) => { |
| 166 | + const includes = files.every((file) => file.name.includes(search)); |
| 167 | + |
| 168 | + expect(includes).toBeTrue(); |
| 169 | + doneFn(); |
| 170 | + }); |
| 171 | + |
| 172 | + const url = `${base_url}/file/search?q=${search}`; |
| 173 | + const req = httpController.expectOne(url); |
| 174 | + |
| 175 | + expect(req.request.method).toBe('GET'); |
| 176 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 177 | + req.flush({ ok: true, files: filesExpected }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should return an empty array if no files were found', (doneFn) => { |
| 181 | + const search = 'test'; |
| 182 | + fileService.searchFiles(search).subscribe(({ files }) => { |
| 183 | + expect(files).toEqual([]); |
| 184 | + doneFn(); |
| 185 | + }); |
| 186 | + |
| 187 | + const url = `${base_url}/file/search?q=${search}`; |
| 188 | + const req = httpController.expectOne(url); |
| 189 | + |
| 190 | + expect(req.request.method).toBe('GET'); |
| 191 | + expect(req.request.headers.has('x-token')).toBeTrue(); |
| 192 | + req.flush({ ok: true, files: [] }); |
| 193 | + }); |
15 | 194 | });
|
16 | 195 | });
|
0 commit comments