|
| 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 | +}); |
0 commit comments