-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathJSONResponse_test.js
More file actions
183 lines (161 loc) · 5.5 KB
/
JSONResponse_test.js
File metadata and controls
183 lines (161 loc) · 5.5 KB
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
181
182
183
import * as chai from 'chai'
import { z } from 'zod'
import { JSONResponse } from '../../lib/helper/JSONResponse.js'
import Container from '../../lib/container.js'
import * as codeceptjs from '../../lib/index.js'
const expect = chai.expect
global.codeceptjs = codeceptjs.default || codeceptjs
const data = {
posts: [
{
id: 1,
title: 'json-server',
author: 'davert',
},
{
id: 2,
},
],
user: {
name: 'davert',
},
}
let restHelper
let I
describe('JSONResponse', () => {
beforeEach(async () => {
await Container.create({
helpers: {
REST: {},
},
})
I = new JSONResponse()
I._beforeSuite()
restHelper = Container.helpers('REST')
})
describe('response codes', () => {
it('should check 200x codes', async () => {
restHelper.options.onResponse({ status: 204 })
I.seeResponseCodeIs(204)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsSuccessful()
})
it('should check 300x codes', async () => {
restHelper.options.onResponse({ status: 304 })
I.seeResponseCodeIs(304)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsRedirection()
})
it('should check 400x codes', async () => {
restHelper.options.onResponse({ status: 404 })
I.seeResponseCodeIs(404)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsClientError()
})
it('should check 500x codes', async () => {
restHelper.options.onResponse({ status: 504 })
I.seeResponseCodeIs(504)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsServerError()
})
it('should throw error on invalid code', () => {
restHelper.options.onResponse({ status: 504 })
expect(() => I.seeResponseCodeIs(200)).to.throw('Response code')
})
})
describe('response data', () => {
it('should check for json inclusion', () => {
restHelper.options.onResponse({ data })
I.seeResponseContainsJson({
posts: [{ id: 2 }],
})
I.seeResponseContainsJson({
posts: [{ id: 1, author: 'davert' }],
})
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No matching element found in array for {"id":2,"author":"boss"}')
})
it('should check for json inclusion - returned Array', () => {
const arrayData = [{ ...data }]
restHelper.options.onResponse({ data: arrayData })
I.seeResponseContainsJson({
posts: [{ id: 2 }],
})
I.seeResponseContainsJson({
posts: [{ id: 1, author: 'davert' }],
})
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No elements in array matched {"posts":[{"id":2,"author":"boss"}]}')
})
it('should check for json inclusion - returned Array of 2 items', () => {
const arrayData = [{ ...data }, { posts: { id: 3 } }]
restHelper.options.onResponse({ data: arrayData })
I.seeResponseContainsJson({
posts: { id: 3 },
})
})
it('should simply check for json inclusion', () => {
restHelper.options.onResponse({ data: { user: { name: 'jon', email: 'jon@doe.com' } } })
I.seeResponseContainsJson({ user: { name: 'jon' } })
I.dontSeeResponseContainsJson({ user: { name: 'jo' } })
I.dontSeeResponseContainsJson({ name: 'joe' })
})
it('should simply check for json inclusion - returned Array', () => {
restHelper.options.onResponse({ data: [{ user: { name: 'jon', email: 'jon@doe.com' } }] })
I.seeResponseContainsJson({ user: { name: 'jon' } })
I.dontSeeResponseContainsJson({ user: { name: 'jo' } })
I.dontSeeResponseContainsJson({ name: 'joe' })
})
it('should simply check for json equality', () => {
restHelper.options.onResponse({ data: { user: 1 } })
I.seeResponseEquals({ user: 1 })
})
it('should simply check for json equality - returned Array', () => {
restHelper.options.onResponse({ data: [{ user: 1 }] })
I.seeResponseEquals([{ user: 1 }])
})
it('should check json contains keys', () => {
restHelper.options.onResponse({ data: { user: 1, post: 2 } })
I.seeResponseContainsKeys(['user', 'post'])
})
it('should check json contains keys - returned Array', () => {
restHelper.options.onResponse({ data: [{ user: 1, post: 2 }] })
I.seeResponseContainsKeys(['user', 'post'])
})
it('should check for json by callback', () => {
restHelper.options.onResponse({ data })
const fn = ({ assert, data }) => {
assert('posts' in data)
assert('user' in data)
}
I.seeResponseValidByCallback(fn)
expect(fn.toString()).to.include("assert('posts' in data)")
})
it('should check for json by zod schema', () => {
restHelper.options.onResponse({ data })
const schema = z.object({
posts: z.array(
z.object({
id: z.number(),
author: z.string(),
title: z.string(),
}),
),
user: z.object({
name: z.string(),
}),
})
const fn = () => {
return schema
}
I.seeResponseMatchesJsonSchema(fn)
I.seeResponseMatchesJsonSchema(schema)
})
it('should throw error when zod validation fails', () => {
restHelper.options.onResponse({ data: { name: 'invalid', age: 'not_a_number' } })
const schema = z.object({
name: z.string(),
age: z.number(),
})
expect(() => I.seeResponseMatchesJsonSchema(schema)).to.throw('Schema validation failed')
})
})
})