Skip to content

Commit d62d96b

Browse files
committed
test(common): add builder tests
add tests for parse file pipe builder and refactor associated classes
1 parent b178eb5 commit d62d96b

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

packages/common/pipes/file/parse-file-pipe.builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ParseFileOptions } from './parse-file-options.interface';
1111
import { ParseFilePipe } from './parse-file.pipe';
1212

1313
export class ParseFilePipeBuilder {
14-
private validators: FileValidator[];
14+
private validators: FileValidator[] = [];
1515

1616
addMaxSizeValidator(options: MaxFileSizeValidatorOptions) {
1717
this.validators.push(new MaxFileSizeValidator(options));

packages/common/pipes/file/parse-file.pipe.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ export class ParseFilePipe implements PipeTransform<any> {
4242
}
4343
return file;
4444
}
45+
46+
getValidators() {
47+
return this.validators;
48+
}
4549
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)