|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { |
| 3 | + FileTypeValidator, |
| 4 | + MaxFileSizeValidator, |
| 5 | + ParseFilePipeBuilder, |
| 6 | +} from '../../../pipes'; |
| 7 | + |
| 8 | +describe('ParseFilePipeBuilder', () => { |
| 9 | + let parseFilePipeBuilder: ParseFilePipeBuilder; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + parseFilePipeBuilder = new ParseFilePipeBuilder(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('build', () => { |
| 16 | + describe('when no validator was passed', () => { |
| 17 | + it('should return a ParseFilePipe with no validators', () => { |
| 18 | + const parseFilePipe = parseFilePipeBuilder.build(); |
| 19 | + expect(parseFilePipe.getValidators()).to.be.empty; |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('when addMaxSizeValidator was chained', () => { |
| 24 | + it('should return a ParseFilePipe with MaxSizeValidator and given options', () => { |
| 25 | + const options = { |
| 26 | + maxSize: 1000, |
| 27 | + }; |
| 28 | + const parseFilePipe = parseFilePipeBuilder |
| 29 | + .addMaxSizeValidator(options) |
| 30 | + .build(); |
| 31 | + |
| 32 | + expect(parseFilePipe.getValidators()).to.deep.include( |
| 33 | + new MaxFileSizeValidator(options), |
| 34 | + ); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('when addFileTypeValidator was chained', () => { |
| 39 | + it('should return a ParseFilePipe with FileTypeValidator and given options', () => { |
| 40 | + const options = { |
| 41 | + fileType: 'image/jpeg', |
| 42 | + }; |
| 43 | + const parseFilePipe = parseFilePipeBuilder |
| 44 | + .addFileTypeValidator(options) |
| 45 | + .build(); |
| 46 | + |
| 47 | + expect(parseFilePipe.getValidators()).to.deep.include( |
| 48 | + new FileTypeValidator(options), |
| 49 | + ); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('when it is called twice with different validators', () => { |
| 54 | + it('should not reuse validators', () => { |
| 55 | + const maxSizeValidatorOptions = { |
| 56 | + maxSize: 1000, |
| 57 | + }; |
| 58 | + |
| 59 | + const pipeWithMaxSizeValidator = parseFilePipeBuilder |
| 60 | + .addMaxSizeValidator(maxSizeValidatorOptions) |
| 61 | + .build(); |
| 62 | + |
| 63 | + const fileTypeValidatorOptions = { |
| 64 | + fileType: 'image/jpeg', |
| 65 | + }; |
| 66 | + |
| 67 | + const pipeWithFileTypeValidator = parseFilePipeBuilder |
| 68 | + .addFileTypeValidator(fileTypeValidatorOptions) |
| 69 | + .build(); |
| 70 | + |
| 71 | + expect(pipeWithFileTypeValidator.getValidators()).not.to.deep.equal( |
| 72 | + pipeWithMaxSizeValidator.getValidators(), |
| 73 | + ); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments