forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-file.pipe.spec.ts
180 lines (151 loc) 路 4.67 KB
/
parse-file.pipe.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { HttpStatus } from '../../../enums';
import { BadRequestException, ConflictException } from '../../../exceptions';
import { FileValidator, ParseFilePipe } from '../../../pipes';
import { expect } from 'chai';
class AlwaysValidValidator extends FileValidator {
isValid(): boolean {
return true;
}
buildErrorMessage(): string {
return '';
}
}
const customErrorMessage = 'Error!';
class AlwaysInvalidValidator extends FileValidator {
isValid(): boolean {
return false;
}
buildErrorMessage(): string {
return customErrorMessage;
}
}
describe('ParseFilePipe', () => {
let parseFilePipe: ParseFilePipe;
describe('transform', () => {
describe('when there are no validators (explicit)', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [],
});
});
it('should return the file object', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.eventually.eql(
requestFile,
);
});
});
describe('when there are no validators (by default constructor)', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe();
});
it('should return the file object', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.eventually.eql(
requestFile,
);
});
});
describe('when all the validators validate the file', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [new AlwaysValidValidator({})],
});
});
it('should return the file object', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.eventually.eql(
requestFile,
);
});
});
describe('when some validator invalidates the file', () => {
describe('and the pipe has the default error', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [new AlwaysInvalidValidator({})],
});
});
it('should throw a BadRequestException', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.be.rejectedWith(
BadRequestException,
);
});
});
describe('and the pipe has a custom error code', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [new AlwaysInvalidValidator({})],
errorHttpStatusCode: HttpStatus.CONFLICT,
});
});
it('should throw this custom Error', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.be.rejectedWith(
ConflictException,
);
});
});
});
describe('when fileIsRequired is false', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [],
fileIsRequired: false,
});
});
it('should pass validation if no file is provided', async () => {
const requestFile = undefined;
await expect(parseFilePipe.transform(requestFile)).to.eventually.eql(
requestFile,
);
});
});
describe('when fileIsRequired is true', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [],
fileIsRequired: true,
});
});
it('should throw an error if no file is provided', async () => {
const requestFile = undefined;
await expect(parseFilePipe.transform(requestFile)).to.be.rejectedWith(
BadRequestException,
);
});
it('should pass validation if a file is provided', async () => {
const requestFile = {
path: 'some-path',
};
await expect(parseFilePipe.transform(requestFile)).to.eventually.eql(
requestFile,
);
});
});
describe('when fileIsRequired is not explicitly provided', () => {
beforeEach(() => {
parseFilePipe = new ParseFilePipe({
validators: [new AlwaysInvalidValidator({})],
});
});
it('should throw an error if no file is provided', async () => {
const requestFile = undefined;
await expect(parseFilePipe.transform(requestFile)).to.be.rejectedWith(
BadRequestException,
);
});
});
});
});