| 1 |
import { vi } from 'vitest' |
| 2 |
import { |
| 3 |
isOnline, |
| 4 |
isDocumentVisible, |
| 5 |
flatten, |
| 6 |
joinUrls, |
| 7 |
} from '@internal/query/utils' |
| 8 |
|
| 9 |
afterAll(() => { |
| 10 |
vi.restoreAllMocks() |
| 11 |
}) |
| 12 |
|
| 13 |
describe('isOnline', () => { |
| 14 |
test('Assumes online=true in a node env', () => { |
| 15 |
vi.spyOn(window, 'navigator', 'get').mockImplementation( |
| 16 |
() => undefined as any, |
| 17 |
) |
| 18 |
|
| 19 |
expect(navigator).toBeUndefined() |
| 20 |
expect(isOnline()).toBe(true) |
| 21 |
}) |
| 22 |
|
| 23 |
test('Returns false if navigator isOnline=false', () => { |
| 24 |
vi.spyOn(window, 'navigator', 'get').mockImplementation( |
| 25 |
() => ({ onLine: false }) as any, |
| 26 |
) |
| 27 |
expect(isOnline()).toBe(false) |
| 28 |
}) |
| 29 |
|
| 30 |
test('Returns true if navigator isOnline=true', () => { |
| 31 |
vi.spyOn(window, 'navigator', 'get').mockImplementation( |
| 32 |
() => ({ onLine: true }) as any, |
| 33 |
) |
| 34 |
expect(isOnline()).toBe(true) |
| 35 |
}) |
| 36 |
}) |
| 37 |
|
| 38 |
describe('isDocumentVisible', () => { |
| 39 |
test('Assumes true when in a non-browser env', () => { |
| 40 |
vi.spyOn(window, 'document', 'get').mockImplementation( |
| 41 |
() => undefined as any, |
| 42 |
) |
| 43 |
expect(window.document).toBeUndefined() |
| 44 |
expect(isDocumentVisible()).toBe(true) |
| 45 |
}) |
| 46 |
|
| 47 |
test('Returns false when hidden=true', () => { |
| 48 |
vi.spyOn(window, 'document', 'get').mockImplementation( |
| 49 |
() => ({ visibilityState: 'hidden' }) as any, |
| 50 |
) |
| 51 |
expect(isDocumentVisible()).toBe(false) |
| 52 |
}) |
| 53 |
|
| 54 |
test('Returns true when visibilityState=prerender', () => { |
| 55 |
vi.spyOn(window, 'document', 'get').mockImplementation( |
| 56 |
() => ({ visibilityState: 'prerender' }) as any, |
| 57 |
) |
| 58 |
expect(document.visibilityState).toBe('prerender') |
| 59 |
expect(isDocumentVisible()).toBe(true) |
| 60 |
}) |
| 61 |
test('Returns true when visibilityState=visible', () => { |
| 62 |
vi.spyOn(window, 'document', 'get').mockImplementation( |
| 63 |
() => ({ visibilityState: 'visible' }) as any, |
| 64 |
) |
| 65 |
expect(document.visibilityState).toBe('visible') |
| 66 |
expect(isDocumentVisible()).toBe(true) |
| 67 |
}) |
| 68 |
test('Returns true when visibilityState=undefined', () => { |
| 69 |
vi.spyOn(window, 'document', 'get').mockImplementation( |
| 70 |
() => ({ visibilityState: undefined }) as any, |
| 71 |
) |
| 72 |
expect(document.visibilityState).toBeUndefined() |
| 73 |
expect(isDocumentVisible()).toBe(true) |
| 74 |
}) |
| 75 |
}) |
| 76 |
|
| 77 |
describe('joinUrls', () => { |
| 78 |
test.each([ |
| 79 |
['/api/', '/banana', '/api/banana'], |
| 80 |
['/api/', 'banana', '/api/banana'], |
| 81 |
['/api', '/banana', '/api/banana'], |
| 82 |
['/api', 'banana', '/api/banana'], |
| 83 |
['', '/banana', '/banana'], |
| 84 |
['', 'banana', 'banana'], |
| 85 |
['api', '?a=1', 'api?a=1'], |
| 86 |
['api/', '?a=1', 'api/?a=1'], |
| 87 |
['api', 'banana?a=1', 'api/banana?a=1'], |
| 88 |
['api/', 'banana?a=1', 'api/banana?a=1'], |
| 89 |
['https://example.com/api', 'banana', 'https://example.com/api/banana'], |
| 90 |
['https://example.com/api', '/banana', 'https://example.com/api/banana'], |
| 91 |
['https://example.com/api/', 'banana', 'https://example.com/api/banana'], |
| 92 |
['https://example.com/api/', '/banana', 'https://example.com/api/banana'], |
| 93 |
['https://example.com/api/', 'https://example.org', 'https://example.org'], |
| 94 |
['https://example.com/api/', '//example.org', '//example.org'], |
| 95 |
])('%s and %s join to %s', (base, url, expected) => { |
| 96 |
expect(joinUrls(base, url)).toBe(expected) |
| 97 |
}) |
| 98 |
}) |
| 99 |
|
| 100 |
describe('flatten', () => { |
| 101 |
test('flattens an array to a depth of 1', () => { |
| 102 |
expect(flatten([1, 2, [3, 4]])).toEqual([1, 2, 3, 4]) |
| 103 |
}) |
| 104 |
test('does not flatten to a depth of 2', () => { |
| 105 |
const flattenResult = flatten([1, 2, [3, 4, [5, 6]]]) |
| 106 |
expect(flattenResult).not.toEqual([1, 2, 3, 4, 5, 6]) |
| 107 |
expect(flattenResult).toEqual([1, 2, 3, 4, [5, 6]]) |
| 108 |
}) |
| 109 |
}) |