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