Skip to content

Commit ea0037a

Browse files
committed
test: parse date pipe spec & include it in e2e test
1 parent 59e71a1 commit ea0037a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

lib/e2e.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createHandler } from './createHandler';
77
import { Body, Delete, Get, Header, HttpCode, Post, Put, Query, Req, Res, Response, SetHeader } from './decorators';
88
import { ValidationPipe } from './pipes';
99
import { ParseBooleanPipe } from './pipes/parseBoolean.pipe';
10+
import { ParseDatePipe } from './pipes/parseDate.pipe';
1011
import { ParseNumberPipe } from './pipes/parseNumber.pipe';
1112

1213
enum CreateSource {
@@ -49,9 +50,18 @@ class TestHandler {
4950
@Header('Content-Type') contentType: string,
5051
@Query('id') id: string,
5152
@Query('step', ParseNumberPipe({ nullable: false })) step: number,
52-
@Query('redirect', ParseBooleanPipe) redirect: boolean
53+
@Query('redirect', ParseBooleanPipe) redirect: boolean,
54+
@Query('startAt', ParseDatePipe) startAt: Date
5355
) {
54-
return { contentType, id, step, redirect, test: this.testField };
56+
return {
57+
contentType,
58+
id,
59+
step,
60+
redirect,
61+
test: this.testField,
62+
startAt,
63+
isStartAtDateInstance: startAt instanceof Date
64+
};
5565
}
5666

5767
@HttpCode(201)
@@ -94,7 +104,7 @@ describe('E2E', () => {
94104

95105
it('read', () =>
96106
request(server)
97-
.get('/?id=my-id&step=1&redirect=true')
107+
.get('/?id=my-id&step=1&redirect=true&startAt=2021-01-01T22:00:00')
98108
.set('Content-Type', 'application/json')
99109
.expect(200)
100110
.then(res =>
@@ -108,7 +118,8 @@ describe('E2E', () => {
108118
contentType: 'application/json',
109119
id: 'my-id',
110120
step: 1,
111-
redirect: true
121+
redirect: true,
122+
isStartAtDateInstance: true
112123
}
113124
})
114125
));

lib/pipes/parseDate.pipe.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ParseDatePipe } from './parseDate.pipe';
2+
3+
describe('ParseDatePipe', () => {
4+
it('Should parse the given date', () => {
5+
const parsed = ParseDatePipe()('2021-01-01');
6+
expect(parsed).toBeInstanceOf(Date);
7+
expect(parsed).toEqual(new Date('2021-01-01'));
8+
});
9+
10+
it('Should parse the given date and time string with spaces in between.', () => {
11+
const parsed = ParseDatePipe()('2021-01-01 20:00:00');
12+
expect(parsed).toBeInstanceOf(Date);
13+
expect(parsed).toEqual(new Date('2021-01-01T20:00:00'));
14+
});
15+
16+
it('Should parse the given date and time string with T in between.', () => {
17+
const parsed = ParseDatePipe()('2021-01-01T20:00:00');
18+
expect(parsed).toBeInstanceOf(Date);
19+
expect(parsed).toEqual(new Date('2021-01-01T20:00:00'));
20+
});
21+
22+
it('Should throw when then given value is not a string.', () => expect(() => ParseDatePipe()(1)).toThrow());
23+
24+
it('Should pass for partial date (year and month).', () =>
25+
expect(ParseDatePipe()('2021-01')).toEqual(new Date('2021-01-01')));
26+
27+
it('Should throw for non-existing date.', () => expect(() => ParseDatePipe()('2021-02-31')).toThrow());
28+
29+
it('Should throw when the given value is string `null`.', () => {
30+
expect(() => ParseDatePipe()('null')).toThrow();
31+
});
32+
33+
it('Should throw when the given value is `null`.', () => {
34+
expect(() => ParseDatePipe()(null)).toThrow();
35+
});
36+
});

0 commit comments

Comments
 (0)