|
| 1 | +import { resolveFetch, resolveResponse, recursiveToCamel, isPlainObject } from '../src/lib/helpers' |
| 2 | + |
| 3 | +describe('Helpers', () => { |
| 4 | + describe('resolveFetch', () => { |
| 5 | + test('returns wrapper function with custom fetch', () => { |
| 6 | + const customFetch = jest.fn() |
| 7 | + const result = resolveFetch(customFetch) |
| 8 | + expect(typeof result).toBe('function') |
| 9 | + }) |
| 10 | + |
| 11 | + test('returns wrapper function with global fetch', () => { |
| 12 | + const originalFetch = global.fetch |
| 13 | + const mockFetch = jest.fn() |
| 14 | + global.fetch = mockFetch |
| 15 | + |
| 16 | + const result = resolveFetch() |
| 17 | + expect(typeof result).toBe('function') |
| 18 | + |
| 19 | + global.fetch = originalFetch |
| 20 | + }) |
| 21 | + |
| 22 | + test('returns dynamic import when fetch is undefined', async () => { |
| 23 | + const originalFetch = global.fetch |
| 24 | + // @ts-ignore |
| 25 | + global.fetch = undefined |
| 26 | + |
| 27 | + const result = resolveFetch() |
| 28 | + expect(typeof result).toBe('function') |
| 29 | + |
| 30 | + global.fetch = originalFetch |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + describe('resolveResponse', () => { |
| 35 | + test('returns Response constructor when available', async () => { |
| 36 | + // In Node.js, Response might not be globally available |
| 37 | + const originalResponse = global.Response |
| 38 | + // @ts-ignore |
| 39 | + global.Response = class MockResponse {} |
| 40 | + |
| 41 | + const result = await resolveResponse() |
| 42 | + expect(typeof result).toBe('function') |
| 43 | + |
| 44 | + global.Response = originalResponse |
| 45 | + }) |
| 46 | + |
| 47 | + test('returns dynamic import when Response is undefined', async () => { |
| 48 | + const originalResponse = global.Response |
| 49 | + // @ts-ignore |
| 50 | + global.Response = undefined |
| 51 | + |
| 52 | + const result = await resolveResponse() |
| 53 | + expect(typeof result).toBe('function') |
| 54 | + |
| 55 | + global.Response = originalResponse |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('recursiveToCamel', () => { |
| 60 | + test('converts snake_case to camelCase', () => { |
| 61 | + const input = { snake_case: 'value', another_key: 'test' } |
| 62 | + const result = recursiveToCamel(input) |
| 63 | + expect(result).toEqual({ snakeCase: 'value', anotherKey: 'test' }) |
| 64 | + }) |
| 65 | + |
| 66 | + test('converts kebab-case to camelCase', () => { |
| 67 | + const input = { 'kebab-case': 'value' } |
| 68 | + const result = recursiveToCamel(input) |
| 69 | + expect(result).toEqual({ kebabCase: 'value' }) |
| 70 | + }) |
| 71 | + |
| 72 | + test('handles nested objects', () => { |
| 73 | + const input = { outer_key: { inner_key: 'value' } } |
| 74 | + const result = recursiveToCamel(input) |
| 75 | + expect(result).toEqual({ outerKey: { innerKey: 'value' } }) |
| 76 | + }) |
| 77 | + |
| 78 | + test('handles arrays', () => { |
| 79 | + const input = [{ snake_case: 'value1' }, { another_key: 'value2' }] |
| 80 | + const result = recursiveToCamel(input) |
| 81 | + expect(result).toEqual([{ snakeCase: 'value1' }, { anotherKey: 'value2' }]) |
| 82 | + }) |
| 83 | + |
| 84 | + test('returns functions unchanged', () => { |
| 85 | + const func = () => {} |
| 86 | + const result = recursiveToCamel(func) |
| 87 | + expect(result).toBe(func) |
| 88 | + }) |
| 89 | + |
| 90 | + test('returns non-objects unchanged', () => { |
| 91 | + expect(recursiveToCamel('string' as any)).toBe('string') |
| 92 | + expect(recursiveToCamel(123 as any)).toBe(123) |
| 93 | + expect(recursiveToCamel(null as any)).toBe(null) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('isPlainObject', () => { |
| 98 | + test('returns true for plain objects', () => { |
| 99 | + expect(isPlainObject({})).toBe(true) |
| 100 | + expect(isPlainObject(new Object())).toBe(true) |
| 101 | + expect(isPlainObject(Object.create(null))).toBe(true) |
| 102 | + expect(isPlainObject({ key: 'value' })).toBe(true) |
| 103 | + }) |
| 104 | + |
| 105 | + test('returns false for non-objects', () => { |
| 106 | + expect(isPlainObject('string' as any)).toBe(false) |
| 107 | + expect(isPlainObject(123 as any)).toBe(false) |
| 108 | + expect(isPlainObject(null as any)).toBe(false) |
| 109 | + expect(isPlainObject(undefined as any)).toBe(false) |
| 110 | + }) |
| 111 | + |
| 112 | + test('returns false for arrays', () => { |
| 113 | + expect(isPlainObject([])).toBe(false) |
| 114 | + expect(isPlainObject([1, 2, 3])).toBe(false) |
| 115 | + }) |
| 116 | + |
| 117 | + test('returns false for functions', () => { |
| 118 | + expect(isPlainObject(() => {})).toBe(false) |
| 119 | + expect(isPlainObject(function () {})).toBe(false) |
| 120 | + }) |
| 121 | + |
| 122 | + test('returns false for class instances', () => { |
| 123 | + class TestClass {} |
| 124 | + expect(isPlainObject(new TestClass())).toBe(false) |
| 125 | + expect(isPlainObject(new Date())).toBe(false) |
| 126 | + expect(isPlainObject(new Error())).toBe(false) |
| 127 | + }) |
| 128 | + |
| 129 | + test('returns false for objects with toStringTag', () => { |
| 130 | + const obj = {} |
| 131 | + Object.defineProperty(obj, Symbol.toStringTag, { value: 'CustomObject' }) |
| 132 | + expect(isPlainObject(obj)).toBe(false) |
| 133 | + }) |
| 134 | + |
| 135 | + test('returns false for iterables', () => { |
| 136 | + const obj = {} |
| 137 | + Object.defineProperty(obj, Symbol.iterator, { value: function* () {} }) |
| 138 | + expect(isPlainObject(obj)).toBe(false) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments