Skip to content

Commit a8d3181

Browse files
committed
test: tests for integer
1 parent 9ab72e8 commit a8d3181

File tree

4 files changed

+185
-2
lines changed

4 files changed

+185
-2
lines changed

test/validator/body.test.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,60 @@ describe('Body Validator', () => {
140140
expect(res.status).toBe(200)
141141
})
142142

143+
it('parse single integer', async () => {
144+
const app = new Elysia().post('/', ({ body }) => body, {
145+
body: t.Object({
146+
name: t.String(),
147+
job: t.String(),
148+
trait: t.Optional(t.String()),
149+
age: t.Integer()
150+
})
151+
})
152+
const res = await app.handle(
153+
post('/', {
154+
name: 'sucrose',
155+
job: 'alchemist',
156+
age: '16'
157+
})
158+
)
159+
160+
expect(await res.json()).toEqual({
161+
name: 'sucrose',
162+
job: 'alchemist',
163+
age: 16
164+
})
165+
166+
expect(res.status).toBe(200)
167+
})
168+
169+
it('parse multiple integers', async () => {
170+
const app = new Elysia().post('/', ({ body }) => body, {
171+
body: t.Object({
172+
name: t.String(),
173+
job: t.String(),
174+
trait: t.Optional(t.String()),
175+
age: t.Integer(),
176+
rank: t.Integer()
177+
})
178+
})
179+
const res = await app.handle(
180+
post('/', {
181+
name: 'sucrose',
182+
job: 'alchemist',
183+
age: '16',
184+
rank: '4'
185+
})
186+
)
187+
188+
expect(await res.json()).toEqual({
189+
name: 'sucrose',
190+
job: 'alchemist',
191+
age: 16,
192+
rank: 4
193+
})
194+
expect(res.status).toBe(200)
195+
})
196+
143197
it('validate empty body', async () => {
144198
const app = new Elysia().post('/', ({ body }) => body, {
145199
body: t.Union([
@@ -416,7 +470,7 @@ describe('Body Validator', () => {
416470
expect(value).toBe('number')
417471
})
418472

419-
it("coerce number to numeric", async () => {
473+
it('coerce number to numeric', async () => {
420474
const app = new Elysia().post('/', ({ body }) => typeof body, {
421475
body: t.Number()
422476
})
@@ -450,7 +504,7 @@ describe('Body Validator', () => {
450504
expect(response.status).toBe(422)
451505
})
452506

453-
it("coerce string to boolean", async () => {
507+
it('coerce string to boolean', async () => {
454508
const app = new Elysia().post('/', ({ body }) => typeof body, {
455509
body: t.Boolean()
456510
})

test/validator/header.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,49 @@ describe('Header Validator', () => {
150150
expect(res.status).toBe(200)
151151
})
152152

153+
it('parse single integer', async () => {
154+
const app = new Elysia().get('/', ({ headers }) => headers, {
155+
headers: t.Object({
156+
limit: t.Integer()
157+
})
158+
})
159+
const res = await app.handle(
160+
req('/', {
161+
headers: {
162+
limit: '16'
163+
}
164+
})
165+
)
166+
167+
expect(await res.json()).toEqual({
168+
limit: 16
169+
})
170+
expect(res.status).toBe(200)
171+
})
172+
173+
it('parse multiple integers', async () => {
174+
const app = new Elysia().get('/', ({ headers }) => headers, {
175+
headers: t.Object({
176+
limit: t.Integer(),
177+
offset: t.Integer()
178+
})
179+
})
180+
const res = await app.handle(
181+
req('/', {
182+
headers: {
183+
limit: '16',
184+
offset: '4'
185+
}
186+
})
187+
)
188+
189+
expect(await res.json()).toEqual({
190+
limit: 16,
191+
offset: 4
192+
})
193+
expect(res.status).toBe(200)
194+
})
195+
153196
it('validate partial', async () => {
154197
const app = new Elysia().get('/', ({ headers }) => headers, {
155198
headers: t.Partial(

test/validator/params.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,69 @@ describe('Params Validator', () => {
8989
expect(res.status).toBe(200)
9090
})
9191

92+
it('parse single integer', async () => {
93+
const app = new Elysia().get('/id/:id', ({ params }) => params, {
94+
params: t.Object({
95+
id: t.Integer()
96+
})
97+
})
98+
const res = await app.handle(req('/id/617'))
99+
expect(await res.json()).toEqual({
100+
id: 617
101+
})
102+
expect(res.status).toBe(200)
103+
})
104+
105+
it('parse malformed integer', async () => {
106+
const app = new Elysia().get('/id/:id', ({ params }) => params, {
107+
params: t.Object({
108+
id: t.Integer()
109+
})
110+
})
111+
const res = await app.handle(req('/id/617.1234'))
112+
expect(await res.json()).toEqual({
113+
errors: [
114+
{
115+
message: 'Expected integer',
116+
path: '',
117+
schema: {
118+
type: 'integer'
119+
},
120+
summary: 'Expected integer',
121+
type: 27,
122+
value: 617.1234
123+
}
124+
],
125+
expected: 0,
126+
found: 617.1234,
127+
message: 'Expected integer',
128+
on: 'property',
129+
property: 'root',
130+
summary: 'Expected integer',
131+
type: 'validation'
132+
})
133+
expect(res.status).toBe(422)
134+
})
135+
136+
it('parse multiple integer', async () => {
137+
const app = new Elysia().get(
138+
'/id/:id/chapter/:chapterId',
139+
({ params }) => params,
140+
{
141+
params: t.Object({
142+
id: t.Integer(),
143+
chapterId: t.Integer()
144+
})
145+
}
146+
)
147+
const res = await app.handle(req('/id/617/chapter/12'))
148+
expect(await res.json()).toEqual({
149+
id: 617,
150+
chapterId: 12
151+
})
152+
expect(res.status).toBe(200)
153+
})
154+
92155
it('create default string params', async () => {
93156
const app = new Elysia().get('/:name', ({ params }) => params, {
94157
params: t.Object({

test/validator/query.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,29 @@ describe('Query Validator', () => {
142142
expect(res.status).toBe(200)
143143
})
144144

145+
it('parse single integer', async () => {
146+
const app = new Elysia().get('/', ({ query: { limit } }) => limit, {
147+
query: t.Object({
148+
limit: t.Integer()
149+
})
150+
})
151+
const res = await app.handle(req('/?limit=16'))
152+
expect(res.status).toBe(200)
153+
expect(await res.text()).toBe('16')
154+
})
155+
156+
it('parse multiple integer', async () => {
157+
const app = new Elysia().get('/', ({ query }) => query, {
158+
query: t.Object({
159+
limit: t.Integer(),
160+
offset: t.Integer()
161+
})
162+
})
163+
const res = await app.handle(req('/?limit=16&offset=0'))
164+
expect(res.status).toBe(200)
165+
expect(await res.json()).toEqual({ limit: 16, offset: 0 })
166+
})
167+
145168
it('validate partial', async () => {
146169
const app = new Elysia().get('/', ({ query }) => query, {
147170
query: t.Partial(

0 commit comments

Comments
 (0)