|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { useRegistryScript } from '../../src/runtime/utils' |
| 3 | + |
| 4 | +// Mock dependencies |
| 5 | +vi.mock('nuxt/app', () => ({ |
| 6 | + useRuntimeConfig: () => ({ public: { scripts: {} } }), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('../../src/runtime/composables/useScript', () => ({ |
| 10 | + useScript: vi.fn((input, options) => ({ input, options })), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('#nuxt-scripts-validator', () => ({ |
| 14 | + parse: vi.fn(), |
| 15 | +})) |
| 16 | + |
| 17 | +describe('useRegistryScript query param merging', () => { |
| 18 | + it('should merge query params when user provides custom src', () => { |
| 19 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 20 | + scriptInput: { |
| 21 | + src: 'https://example.com/script.js?id=123&auth=abc&existing=default', |
| 22 | + }, |
| 23 | + })) |
| 24 | + |
| 25 | + const userOptions = { |
| 26 | + scriptInput: { |
| 27 | + src: 'https://custom-domain.com/script.js?auth=override&new=param', |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 32 | + |
| 33 | + // The options function should be called with the user options and context |
| 34 | + expect(mockOptionsFunction).toHaveBeenCalledWith( |
| 35 | + userOptions, |
| 36 | + { scriptInput: userOptions.scriptInput }, |
| 37 | + ) |
| 38 | + |
| 39 | + // Check the result contains merged query params (user params come first due to object spread) |
| 40 | + expect(result.input.src).toBe('https://custom-domain.com/script.js?auth=override&new=param&id=123&existing=default') |
| 41 | + }) |
| 42 | + |
| 43 | + it('should preserve user query params over default ones', () => { |
| 44 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 45 | + scriptInput: { |
| 46 | + src: 'https://example.com/script.js?param1=default¶m2=default', |
| 47 | + }, |
| 48 | + })) |
| 49 | + |
| 50 | + const userOptions = { |
| 51 | + scriptInput: { |
| 52 | + src: 'https://custom-domain.com/script.js?param1=override', |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 57 | + |
| 58 | + expect(result.input.src).toBe('https://custom-domain.com/script.js?param1=override¶m2=default') |
| 59 | + }) |
| 60 | + |
| 61 | + it('should handle cases where user src has no query params', () => { |
| 62 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 63 | + scriptInput: { |
| 64 | + src: 'https://example.com/script.js?id=123&auth=abc', |
| 65 | + }, |
| 66 | + })) |
| 67 | + |
| 68 | + const userOptions = { |
| 69 | + scriptInput: { |
| 70 | + src: 'https://custom-domain.com/script.js', |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 75 | + |
| 76 | + expect(result.input.src).toBe('https://custom-domain.com/script.js?id=123&auth=abc') |
| 77 | + }) |
| 78 | + |
| 79 | + it('should handle cases where default src has no query params', () => { |
| 80 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 81 | + scriptInput: { |
| 82 | + src: 'https://example.com/script.js', |
| 83 | + }, |
| 84 | + })) |
| 85 | + |
| 86 | + const userOptions = { |
| 87 | + scriptInput: { |
| 88 | + src: 'https://custom-domain.com/script.js?custom=param', |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 93 | + |
| 94 | + expect(result.input.src).toBe('https://custom-domain.com/script.js?custom=param') |
| 95 | + }) |
| 96 | + |
| 97 | + it('should not modify src when no user src is provided', () => { |
| 98 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 99 | + scriptInput: { |
| 100 | + src: 'https://example.com/script.js?id=123&auth=abc', |
| 101 | + }, |
| 102 | + })) |
| 103 | + |
| 104 | + const userOptions = {} |
| 105 | + |
| 106 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 107 | + |
| 108 | + expect(result.input.src).toBe('https://example.com/script.js?id=123&auth=abc') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should handle complex URLs with paths and fragments', () => { |
| 112 | + const mockOptionsFunction = vi.fn((_opts, _ctx) => ({ |
| 113 | + scriptInput: { |
| 114 | + src: 'https://example.com/path/to/script.js?id=123&version=1', |
| 115 | + }, |
| 116 | + })) |
| 117 | + |
| 118 | + const userOptions = { |
| 119 | + scriptInput: { |
| 120 | + src: 'https://custom-domain.com/custom/path/script.js?version=2&custom=true', |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + const result = useRegistryScript('test', mockOptionsFunction, userOptions) |
| 125 | + |
| 126 | + expect(result.input.src).toBe('https://custom-domain.com/custom/path/script.js?version=2&custom=true&id=123') |
| 127 | + }) |
| 128 | +}) |
0 commit comments