-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
parse-file-pipe.builder.spec.ts
105 lines (88 loc) 路 3.01 KB
/
parse-file-pipe.builder.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
import { expect } from 'chai';
import {
FileTypeValidator,
FileTypeValidatorOptions,
FileValidator,
MaxFileSizeValidator,
ParseFilePipeBuilder,
} from '../../../pipes';
describe('ParseFilePipeBuilder', () => {
let parseFilePipeBuilder: ParseFilePipeBuilder;
beforeEach(() => {
parseFilePipeBuilder = new ParseFilePipeBuilder();
});
describe('build', () => {
describe('when no validator was passed', () => {
it('should return a ParseFilePipe with no validators', () => {
const parseFilePipe = parseFilePipeBuilder.build();
expect(parseFilePipe.getValidators()).to.be.empty;
});
});
describe('when addMaxSizeValidator was chained', () => {
it('should return a ParseFilePipe with MaxSizeValidator and given options', () => {
const options = {
maxSize: 1000,
};
const parseFilePipe = parseFilePipeBuilder
.addMaxSizeValidator(options)
.build();
expect(parseFilePipe.getValidators()).to.deep.include(
new MaxFileSizeValidator(options),
);
});
});
describe('when addFileTypeValidator was chained', () => {
it('should return a ParseFilePipe with FileTypeValidator and given options', () => {
const options = {
fileType: 'image/jpeg',
};
const parseFilePipe = parseFilePipeBuilder
.addFileTypeValidator(options)
.build();
expect(parseFilePipe.getValidators()).to.deep.include(
new FileTypeValidator(options),
);
});
});
describe('when custom validator was chained', () => {
it('should return a ParseFilePipe with TestFileValidator and given options', () => {
class TestFileValidator extends FileValidator<{ name: string }> {
buildErrorMessage(file: any): string {
return 'TestFileValidator failed';
}
isValid(file: any): boolean | Promise<boolean> {
return true;
}
}
const options = {
name: 'test',
};
const parseFilePipe = parseFilePipeBuilder
.addValidator(new TestFileValidator(options))
.build();
expect(parseFilePipe.getValidators()).to.deep.include(
new TestFileValidator(options),
);
});
});
describe('when it is called twice with different validators', () => {
it('should not reuse validators', () => {
const maxSizeValidatorOptions = {
maxSize: 1000,
};
const pipeWithMaxSizeValidator = parseFilePipeBuilder
.addMaxSizeValidator(maxSizeValidatorOptions)
.build();
const fileTypeValidatorOptions = {
fileType: 'image/jpeg',
};
const pipeWithFileTypeValidator = parseFilePipeBuilder
.addFileTypeValidator(fileTypeValidatorOptions)
.build();
expect(pipeWithFileTypeValidator.getValidators()).not.to.deep.equal(
pipeWithMaxSizeValidator.getValidators(),
);
});
});
});
});