Skip to content

Commit 05b719a

Browse files
committed
fix(jsontypes): Allow null in JSONValue-type
1 parent b75d95a commit 05b719a

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
export type Opaque<K, T> = T & { __TYPE__: K };
22

3-
export type JSONValue = string | number | boolean | JSONObject | JSONArray;
3+
export type JSONValue =
4+
| null
5+
| string
6+
| number
7+
| boolean
8+
| JSONObject
9+
| JSONArray;
410
export type JSONObject = { [member: string]: JSONValue };
511
export interface JSONArray extends Array<JSONValue> {}
612

@@ -41,7 +47,9 @@ export type MockHandler =
4147
| ((args: HandlerArgument) => JSONValue)
4248
| JSONValue;
4349

44-
export type MockHandlerFunction = (args: HandlerArgument) => Promise<ResponseData>;
50+
export type MockHandlerFunction = (
51+
args: HandlerArgument
52+
) => Promise<ResponseData>;
4553
export type RequestUrl = Opaque<'RequestUrl', string>;
4654
export type MatcherUrl = Opaque<'MatcherUrl', string>;
4755

test/yet-another-fetch-mock.test.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'isomorphic-fetch';
22
import MatcherUtils from './../src/matcher-utils';
33
import ResponseUtils from './../src/response-utils';
4-
import FetchMock from '../src/yet-another-fetch-mock';
4+
import FetchMock, { JSONValue } from '../src/yet-another-fetch-mock';
55
import { HandlerArgument, RequestUrl, ResponseData } from '../src/types';
66
import { findBody, findPathParams } from '../src/internal-utils';
77
import MiddlewareUtils from '../src/middleware-utils';
@@ -97,9 +97,15 @@ describe('FetchMock', () => {
9797
mock.post('/post', { key: 'post' });
9898
mock.delete('/delete', { key: 'delete' });
9999
mock.put('/put', { key: 'put' });
100-
mock.mock(MatcherUtils.combine(MatcherUtils.method('HEAD'), MatcherUtils.url('/head')), {
101-
key: 'head'
102-
});
100+
mock.mock(
101+
MatcherUtils.combine(
102+
MatcherUtils.method('HEAD'),
103+
MatcherUtils.url('/head')
104+
),
105+
{
106+
key: 'head'
107+
}
108+
);
103109

104110
const postReq = fetchToJson('/post', { method: 'POST' }).then(json =>
105111
expect(json.key).toBe('post')
@@ -149,7 +155,9 @@ describe('FetchMock', () => {
149155
it('should support fallback to realFetch', done => {
150156
mock.get('/testurl', { key: 'testurl' });
151157

152-
const mocked = fetchToJson('/testurl').then(json => expect(json.key).toBe('testurl'));
158+
const mocked = fetchToJson('/testurl').then(json =>
159+
expect(json.key).toBe('testurl')
160+
);
153161
const fallback = fetchToJson('https://xkcd.com/info.0.json').then(json =>
154162
expect(json.num).toBeDefined()
155163
);
@@ -159,7 +167,10 @@ describe('FetchMock', () => {
159167

160168
it('should support delayed responses', done => {
161169
mock.get('/test', ResponseUtils.delayed(200, { key: 'delayed' }));
162-
mock.get('/test2', ResponseUtils.delayed(200, ResponseUtils.json({ key: 'delayed2' })));
170+
mock.get(
171+
'/test2',
172+
ResponseUtils.delayed(200, ResponseUtils.json({ key: 'delayed2' }))
173+
);
163174
const startTime = new Date().getTime();
164175

165176
Promise.all([fetchToJson('/test'), fetchToJson('/test2')]).then(json => {
@@ -198,7 +209,10 @@ describe('FetchMock', () => {
198209
it('should be able to combine response utils', done => {
199210
mock.get(
200211
'/combine',
201-
ResponseUtils.combine(ResponseUtils.json({ key: 'value' }), ResponseUtils.statusCode(201))
212+
ResponseUtils.combine(
213+
ResponseUtils.json({ key: 'value' }),
214+
ResponseUtils.statusCode(201)
215+
)
202216
);
203217

204218
mock.get(
@@ -242,7 +256,9 @@ describe('FetchMock', () => {
242256
});
243257

244258
it('should jsonValue as response in MockHandler', done => {
245-
const myResponse = ({ queryParams }: HandlerArgument) => ({ key: 'BIG-CASE' });
259+
const myResponse = ({ queryParams }: HandlerArgument) => ({
260+
key: 'BIG-CASE'
261+
});
246262

247263
mock.post('/lowercase', myResponse);
248264

@@ -260,7 +276,9 @@ describe('middleware-utils', () => {
260276
it('should combine middlewares', done => {
261277
MathMock.fixRandom(0.2);
262278
const delay = jest.fn(MiddlewareUtils.delayMiddleware(100));
263-
const failure = jest.fn(MiddlewareUtils.failurerateMiddleware(0.3, { status: 1337 }));
279+
const failure = jest.fn(
280+
MiddlewareUtils.failurerateMiddleware(0.3, { status: 1337 })
281+
);
264282
const startTime = new Date().getTime();
265283

266284
const combined = MiddlewareUtils.combine(delay, failure);
@@ -294,7 +312,10 @@ describe('middleware-utils', () => {
294312
it('should have a random failure rate', done => {
295313
MathMock.fixRandom(0.2);
296314
const delay = MiddlewareUtils.failurerateMiddleware(0.3);
297-
const result = delay({} as HandlerArgument, 'normal-response' as ResponseData);
315+
const result = delay(
316+
{} as HandlerArgument,
317+
'normal-response' as ResponseData
318+
);
298319

299320
(result as Promise<ResponseData>).then(res => {
300321
expect(res.status).toBe(500);
@@ -305,7 +326,10 @@ describe('middleware-utils', () => {
305326
it('should have a random failure rate2', done => {
306327
MathMock.fixRandom(0.4);
307328
const delay = MiddlewareUtils.failurerateMiddleware(0.3);
308-
const result = delay({} as HandlerArgument, 'normal-response' as ResponseData);
329+
const result = delay(
330+
{} as HandlerArgument,
331+
'normal-response' as ResponseData
332+
);
309333

310334
(result as Promise<String>).then(res => {
311335
expect(res).toBe('normal-response');
@@ -316,11 +340,19 @@ describe('middleware-utils', () => {
316340
it('should support custom error response', done => {
317341
MathMock.fixRandom(0.2);
318342
const delay = MiddlewareUtils.failurerateMiddleware(0.3, { status: 1337 });
319-
const result = delay({} as HandlerArgument, 'normal-response' as ResponseData);
343+
const result = delay(
344+
{} as HandlerArgument,
345+
'normal-response' as ResponseData
346+
);
320347

321348
(result as Promise<ResponseData>).then(res => {
322349
expect(res.status).toBe(1337);
323350
done();
324351
});
325352
});
353+
354+
it('should support null values in JSON', done => {
355+
const value: JSONValue = { data: null };
356+
done();
357+
});
326358
});

0 commit comments

Comments
 (0)