|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { FileFieldsInterceptor } from './../../interceptors/file-fields.interceptor'; |
| 4 | +import { Observable, of } from 'rxjs'; |
| 5 | +import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context.host'; |
| 6 | + |
| 7 | +describe('FileFieldsInterceptor', () => { |
| 8 | + it('should return metatype with expected structure', async () => { |
| 9 | + const targetClass = FileFieldsInterceptor([{name: 'file', maxCount: 1}, {name: 'anotherFile', maxCount: 1}]); |
| 10 | + expect(targetClass.prototype.intercept).to.not.be.undefined; |
| 11 | + }); |
| 12 | + describe('intercept', () => { |
| 13 | + let stream$; |
| 14 | + beforeEach(() => { |
| 15 | + stream$ = of('test'); |
| 16 | + }); |
| 17 | + it('should call object with expected params', async () => { |
| 18 | + const fieldName1 = 'file'; |
| 19 | + const maxCount1 = 1; |
| 20 | + const fieldName2 = 'anotherFile'; |
| 21 | + const maxCount2 = 2; |
| 22 | + const argument = [ |
| 23 | + {name: fieldName1, maxCount: maxCount1}, |
| 24 | + {name: fieldName2, maxCount: maxCount2} |
| 25 | + ] |
| 26 | + const target = new (FileFieldsInterceptor(argument))(); |
| 27 | + |
| 28 | + const callback = (req, res, next) => next(); |
| 29 | + const fieldsSpy = sinon |
| 30 | + .stub((target as any).upload, 'fields') |
| 31 | + .returns(callback); |
| 32 | + |
| 33 | + await target.intercept(new ExecutionContextHost([]), stream$); |
| 34 | + |
| 35 | + expect(fieldsSpy.called).to.be.true; |
| 36 | + expect(fieldsSpy.calledWith(argument)).to.be.true; |
| 37 | + }); |
| 38 | + it('should transform exception', async () => { |
| 39 | + const fieldName1 = 'file'; |
| 40 | + const maxCount1 = 1; |
| 41 | + const fieldName2 = 'anotherFile'; |
| 42 | + const maxCount2 = 2; |
| 43 | + const argument = [ |
| 44 | + {name: fieldName1, maxCount: maxCount1}, |
| 45 | + {name: fieldName2, maxCount: maxCount2} |
| 46 | + ] |
| 47 | + const target = new (FileFieldsInterceptor(argument)); |
| 48 | + const err = {}; |
| 49 | + const callback = (req, res, next) => next(err); |
| 50 | + |
| 51 | + (target as any).fields = { |
| 52 | + array: () => callback, |
| 53 | + }; |
| 54 | + expect(target.intercept(new ExecutionContextHost([]), stream$)).to.eventually.throw(); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments