Skip to content

Commit 1ce2c84

Browse files
committed
add utils tests
1 parent f6a00e6 commit 1ce2c84

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/utils.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { isDataWithResponseInit, isRedirect, isResponse, lazy } from './utils.js';
2+
3+
describe('utils', () => {
4+
describe('lazy', () => {
5+
it('should call the function only once', () => {
6+
const fn = jest.fn(() => 'test');
7+
const lazyFn = lazy(fn);
8+
9+
expect(fn).not.toHaveBeenCalled();
10+
11+
expect(lazyFn()).toBe('test');
12+
expect(fn).toHaveBeenCalledTimes(1);
13+
14+
expect(lazyFn()).toBe('test');
15+
expect(fn).toHaveBeenCalledTimes(1);
16+
});
17+
});
18+
19+
describe('isRedirect', () => {
20+
it('should return true for a redirect response', () => {
21+
const res = new Response(null, { status: 302 });
22+
23+
expect(isRedirect(res)).toBe(true);
24+
});
25+
26+
it('should return false for a non-redirect response', () => {
27+
const res = new Response(null, { status: 200 });
28+
29+
expect(isRedirect(res)).toBe(false);
30+
});
31+
});
32+
33+
describe('isResponse', () => {
34+
it('should return true for a Response object', () => {
35+
const res = new Response();
36+
37+
expect(isResponse(res)).toBe(true);
38+
});
39+
40+
it('should return false for a non-Response object', () => {
41+
expect(isResponse({})).toBe(false);
42+
});
43+
});
44+
45+
describe('isDataWithResponseInit', () => {
46+
it('should return true for a DataWithResponseInit object', () => {
47+
const data = {
48+
type: 'DataWithResponseInit',
49+
data: {},
50+
init: {},
51+
};
52+
53+
expect(isDataWithResponseInit(data)).toBe(true);
54+
});
55+
56+
it('should return false for a non-DataWithResponseInit object', () => {
57+
expect(isDataWithResponseInit({})).toBe(false);
58+
});
59+
});
60+
});

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function isResponse(response: unknown): response is Response {
3838
}
3939

4040
/**
41-
* Returns true if the data is a DataWithResponseInit object.
41+
* Checks if the data is a DataWithResponseInit object.
42+
* @param data - The data to check.
43+
* @returns True if the data is a DataWithResponseInit object.
4244
*/
4345
export function isDataWithResponseInit(data: unknown): data is DataWithResponseInit<unknown> {
4446
return (

0 commit comments

Comments
 (0)