|
| 1 | +import { RemoteConfig } from '@lomray/microservice-helpers'; |
| 2 | +import { TypeormMock } from '@lomray/microservice-helpers/mocks'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import { getRepository } from 'typeorm'; |
| 6 | +import { bucketNameMock } from '@__mocks__/common'; |
| 7 | +import FileType from '@constants/file-type'; |
| 8 | +import CONST from '@constants/index'; |
| 9 | +import File from '@entities/file'; |
| 10 | +import type { IRemoteConfig } from '@interfaces/remote-config'; |
| 11 | +import AnyFile from '@services/file/any-file'; |
| 12 | +import StorageFactory from '@services/storage/factory'; |
| 13 | + |
| 14 | +describe('services/file/any-file', () => { |
| 15 | + const sandbox = sinon.createSandbox(); |
| 16 | + const fileData = getRepository(File).create({ |
| 17 | + id: 'file_id', |
| 18 | + }); |
| 19 | + const processingConfig: IRemoteConfig = { |
| 20 | + storagePathPrefix: CONST.STORAGE_PATH_PREFIX, |
| 21 | + }; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + TypeormMock.sandbox.reset(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + sandbox.restore(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should successfully save file', async () => { |
| 32 | + TypeormMock.entityManager.save.resolves(fileData); |
| 33 | + sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } }); |
| 34 | + |
| 35 | + const storage = await StorageFactory.create(); |
| 36 | + const service = new AnyFile( |
| 37 | + FileType.file, |
| 38 | + TypeormMock.entityManager, |
| 39 | + storage, |
| 40 | + processingConfig, |
| 41 | + ); |
| 42 | + |
| 43 | + sandbox.stub(service, <any>'composeData').resolves({ |
| 44 | + fileData: { url: 'url', type: FileType.file }, |
| 45 | + fileBuffer: undefined, |
| 46 | + }); |
| 47 | + |
| 48 | + sandbox.stub(service, <any>'uploadFile'); |
| 49 | + |
| 50 | + const entity = await service.save('file', 'user_id'); |
| 51 | + |
| 52 | + const [, file] = TypeormMock.entityManager.save.firstCall.args; |
| 53 | + |
| 54 | + expect(entity).to.deep.equal(fileData); |
| 55 | + expect(file).to.deep.equal({ |
| 56 | + userId: 'user_id', |
| 57 | + url: '/', |
| 58 | + type: FileType.file, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should successfully remove file', async () => { |
| 63 | + sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } }); |
| 64 | + |
| 65 | + const storage = await StorageFactory.create(); |
| 66 | + const service = new AnyFile( |
| 67 | + FileType.file, |
| 68 | + TypeormMock.entityManager, |
| 69 | + storage, |
| 70 | + processingConfig, |
| 71 | + ); |
| 72 | + |
| 73 | + sandbox.stub(storage, 'delete'); |
| 74 | + |
| 75 | + const isRemoved = await service.remove(fileData); |
| 76 | + |
| 77 | + const [, file] = TypeormMock.entityManager.remove.firstCall.args; |
| 78 | + |
| 79 | + expect(isRemoved).to.ok; |
| 80 | + expect(file).to.deep.equal(fileData); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should successfully update file', async () => { |
| 84 | + TypeormMock.entityManager.save.resolves(fileData); |
| 85 | + sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } }); |
| 86 | + |
| 87 | + const storage = await StorageFactory.create(); |
| 88 | + const service = new AnyFile( |
| 89 | + FileType.file, |
| 90 | + TypeormMock.entityManager, |
| 91 | + storage, |
| 92 | + processingConfig, |
| 93 | + ); |
| 94 | + |
| 95 | + sandbox.stub(service, <any>'composeData').resolves({ |
| 96 | + fileData: { url: 'url', type: FileType.file }, |
| 97 | + fileBuffer: undefined, |
| 98 | + }); |
| 99 | + |
| 100 | + sandbox.stub(service, <any>'uploadFile'); |
| 101 | + |
| 102 | + sandbox.stub(storage, 'delete'); |
| 103 | + |
| 104 | + const entity = await service.update(fileData, 'file'); |
| 105 | + |
| 106 | + const [, fileEntity] = TypeormMock.entityManager.save.firstCall.args; |
| 107 | + |
| 108 | + expect(entity).to.deep.equal(fileData); |
| 109 | + expect(fileEntity).to.deep.equal({ |
| 110 | + id: 'file_id', |
| 111 | + url: 'url', |
| 112 | + type: FileType.file, |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments