UNPKG

6.16 kBJavaScriptView Raw
1'use strict';
2
3const frisby = require('../src/frisby');
4const mocks = require('./fixtures/http_mocks');
5
6const testHost = 'http://api.example.com';
7
8describe('expect(\'json\')', function() {
9
10 it('should match exact JSON', function(doneFn) {
11 mocks.use(['getUser1']);
12
13 frisby.fetch(testHost + '/users/1')
14 .expect('json', {
15 id: 1,
16 email: 'joe.schmoe@example.com'
17 })
18 .done(doneFn);
19 });
20
21 it('should error with extra key', function(doneFn) {
22 mocks.use(['getUser1']);
23
24 frisby.fetch(testHost + '/users/1')
25 .expectNot('json', {
26 id: 1,
27 id2: 2,
28 email: 'joe.schmoe@example.com'
29 })
30 .done(doneFn);
31 });
32
33 it('should NOT error with missing key', function(doneFn) {
34 mocks.use(['getUser1']);
35
36 frisby.fetch(testHost + '/users/1')
37 .expect('json', {
38 email: 'joe.schmoe@example.com'
39 })
40 .done(doneFn);
41 });
42
43 it('should error with matching keys, but incorrect values', function(doneFn) {
44 mocks.use(['getUser1']);
45
46 frisby.fetch(testHost + '/users/1')
47 .expectNot('json', {
48 id: 1,
49 email: 'joe.schmoe@example.net'
50 })
51 .done(doneFn);
52 });
53
54 it('should match from data via fromJSON', function(doneFn) {
55 frisby.fromJSON({
56 foo: 'bar',
57 bar: 'baz'
58 })
59 .expect('json', {
60 foo: 'bar'
61 })
62 .done(doneFn);
63 });
64
65 it('should error with incorrect nested key value', function(doneFn) {
66 frisby.fromJSON({
67 one: {
68 two: {
69 three: 3
70 }
71 }
72 })
73 .expectNot('json', {
74 one: {
75 two: {
76 three: 4
77 }
78 }
79 })
80 .done(doneFn);
81 });
82
83 it('should match JSON content using provided path and object', function(doneFn) {
84 frisby.fromJSON({
85 one: {
86 two: {
87 three: 3
88 }
89 }
90 })
91 .expect('json', 'one.two', {
92 three: 3
93 })
94 .done(doneFn);
95 });
96
97 it('should match single value using json', function(doneFn) {
98 mocks.use(['getUser1']);
99
100 frisby.fetch(testHost + '/users/1')
101 .expect('json', 'id', 1)
102 .done(doneFn);
103 });
104
105 it('should match single value using RegExp', function(doneFn) {
106 mocks.use(['getUser1']);
107
108 frisby.fetch(testHost + '/users/1')
109 .expect('json', 'email', /joe\.\w+@\w+\.\w{3}/)
110 .done(doneFn);
111 });
112
113 it('should match single null value using json', function(doneFn) {
114 frisby.fromJSON({
115 foo: null
116 })
117 .expect('json', 'foo', null)
118 .done(doneFn);
119 });
120
121 it('should match array using json', function(doneFn) {
122 frisby.fromJSON(['a', 1, true, null])
123 .expect('json', ['a', 1, true, null])
124 .then(function() {
125 return frisby.fromJSON(['a', 1, true, null])
126 .expect('json', ['a', 1, true]);
127 })
128 .then(function() {
129 return frisby.fromJSON(['a', 1, true, null])
130 .expect('json', [1, null]);
131 })
132 .then(function() {
133 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
134 .expect('json', [{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]]);
135 })
136 .then(function() {
137 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
138 .expect('json', [{a: 0}, {b: 1}, {c: 2}]);
139 })
140 .then(function() {
141 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
142 .expect('json', [{b: 1}, [0, 1, 2]]);
143 })
144 .done(doneFn);
145 });
146
147 it('should error different array using json', function(doneFn) {
148 frisby.fromJSON(['a', 1, true, null])
149 .expectNot('json', ['a', 0, true, null])
150 .then(function() {
151 return frisby.fromJSON(['a', 1, true, null])
152 .expectNot('json', ['a', 1, true, null, false]);
153 })
154 .then(function() {
155 return frisby.fromJSON(['a', 1, true, null])
156 .expectNot('json', [0, null]);
157 })
158 .then(function() {
159 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
160 .expectNot('json', [{a: 0}, {b: 1}, {c: 1}, [0, 1, 2]]);
161 })
162 .then(function() {
163 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
164 .expectNot('json', [{a: 0}, {b: 1}, {c: 1}]);
165 })
166 .then(function() {
167 return frisby.fromJSON([{a: 0}, {b: 1}, {c: 2}, [0, 1, 2]])
168 .expectNot('json', [{b: 1}, [0, 1, 1]]);
169 })
170 .then(function() {
171 return frisby.fromJSON({a: 0})
172 .expectNot('json', [{a: 0}]);
173 })
174 .done(doneFn);
175 });
176
177});
178
179describe('expect(\'jsonStrict\')', function() {
180 it('should match exact JSON', function(doneFn) {
181 mocks.use(['getUser1']);
182
183 frisby.fetch(testHost + '/users/1')
184 .expect('jsonStrict', {
185 id: 1,
186 email: 'joe.schmoe@example.com'
187 })
188 .done(doneFn);
189 });
190
191 it('should error with extra key', function(doneFn) {
192 mocks.use(['getUser1']);
193
194 frisby.fetch(testHost + '/users/1')
195 .expectNot('jsonStrict', {
196 id: 1,
197 id2: 2,
198 email: 'joe.schmoe@example.com'
199 })
200 .done(doneFn);
201 });
202
203 it('should error with missing key', function(doneFn) {
204 mocks.use(['getUser1']);
205
206 frisby.fetch(testHost + '/users/1')
207 .expectNot('jsonStrict', {
208 email: 'joe.schmoe@example.com'
209 })
210 .done(doneFn);
211 });
212
213 it('should error with matching keys, but incorrect values', function(doneFn) {
214 mocks.use(['getUser1']);
215
216 frisby.fetch(testHost + '/users/1')
217 .expectNot('jsonStrict', {
218 id: 1,
219 email: 'joe.schmoe@example.net'
220 })
221 .done(doneFn);
222 });
223
224 it('should match from data via fromJSON', function(doneFn) {
225 frisby.fromJSON({
226 foo: 'bar'
227 })
228 .expect('jsonStrict', {
229 foo: 'bar'
230 })
231 .done(doneFn);
232 });
233
234 it('should match single value using json', function(doneFn) {
235 mocks.use(['getUser1']);
236
237 frisby.fetch(testHost + '/users/1')
238 .expect('jsonStrict', 'id', 1)
239 .done(doneFn);
240 });
241
242 it('should match single null value using json', function(doneFn) {
243 frisby.fromJSON({
244 foo: null
245 })
246 .expect('jsonStrict', 'foo', null)
247 .done(doneFn);
248 });
249
250});