/
body.formatters.builder.test.ts
131 lines (113 loc) · 2.98 KB
/
body.formatters.builder.test.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
import { JinEitherFrame } from '#frames/JinEitherFrame';
import { lightFormat } from 'date-fns';
import { expect, it } from 'vitest';
class Test001PostFrame extends JinEitherFrame {
@JinEitherFrame.P()
public readonly passing: string;
@JinEitherFrame.B({
formatters: [{ string: (value) => `${value}+111` }, { string: (value) => `${value}+222` }],
})
public readonly username: string[];
@JinEitherFrame.body()
public readonly password: string;
constructor(args: { passing: string; username: string[]; password: string }) {
super({
$$host: 'http://some.api.google.com/jinframe/:passing',
$$method: 'POST',
});
this.passing = args.passing;
this.username = args.username;
this.password = args.password;
}
}
it('T001-primitive-type-multiple-formatters', async () => {
const frame = new Test001PostFrame({
passing: 'hello',
username: ['ironman', 'thor'],
password: 'advengers',
});
const req = frame.request();
const excpetation = {
timeout: 120000,
headers: { 'Content-Type': 'application/json' },
method: 'POST',
data: { username: ['ironman+111+222', 'thor+111+222'], password: 'advengers' },
transformRequest: undefined,
url: 'http://some.api.google.com/jinframe/hello',
validateStatus: undefined,
};
// console.log(req);
expect(req).toEqual(excpetation);
});
class Test002PostFrame extends JinEitherFrame {
@JinEitherFrame.param()
public readonly passing: string;
@JinEitherFrame.body({
formatters: [
{
findFrom: 'name',
string: (value) => `${value}+111`,
},
{
findFrom: 'bio.birth',
dateTime: (value) => lightFormat(value, `yyyy-MM-dd'T'HH:mm:ss`),
},
],
})
public readonly hero: {
name: string;
age: number;
bio: {
birth: Date;
};
};
@JinEitherFrame.body()
public readonly password: string;
constructor(args: {
passing: string;
hero: {
name: string;
age: number;
bio: {
birth: Date;
};
};
password: string;
}) {
super({
$$host: 'http://some.api.google.com/jinframe/:passing',
$$method: 'POST',
});
this.passing = args.passing;
this.hero = args.hero;
this.password = args.password;
}
}
it('T002-zero-depth-post-frame', async () => {
const frame = new Test002PostFrame({
passing: 'hello',
hero: { name: 'ironman', age: 33, bio: { birth: new Date(1978, 2, 3, 11, 22, 33) } },
password: 'advengers',
});
const req = frame.request();
const excpetation = {
timeout: 120000,
headers: { 'Content-Type': 'application/json' },
method: 'POST',
data: {
hero: {
age: 33,
bio: {
birth: '1978-03-03T11:22:33',
},
name: 'ironman+111',
},
password: 'advengers',
},
transformRequest: undefined,
url: 'http://some.api.google.com/jinframe/hello',
validateStatus: undefined,
};
// console.log(req);
expect(req).toEqual(excpetation);
});