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