|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { defineComponents } from '../defineComponents'; |
| 3 | +import { loadFileUploaderFrom, UC_WINDOW_KEY } from '../loadFileUploaderFrom'; |
| 4 | + |
| 5 | +vi.mock('../defineComponents', () => ({ |
| 6 | + defineComponents: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + vi.clearAllMocks(); |
| 11 | + |
| 12 | + delete (window as any)[UC_WINDOW_KEY]; |
| 13 | +}); |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + const scripts = document.querySelectorAll('script[src]'); |
| 17 | + for (const script of scripts) { |
| 18 | + script.remove(); |
| 19 | + } |
| 20 | + |
| 21 | + delete (window as any)[UC_WINDOW_KEY]; |
| 22 | +}); |
| 23 | + |
| 24 | +describe('loadFileUploaderFrom', async () => { |
| 25 | + const { default: testUrl } = await import('~/web/file-uploader.iife.min.js?url'); |
| 26 | + |
| 27 | + describe('UC_WINDOW_KEY', () => { |
| 28 | + it('should export UC_WINDOW_KEY as "UC"', () => { |
| 29 | + expect(UC_WINDOW_KEY).toBe('UC'); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('when window.UC already exists', () => { |
| 34 | + it('should resolve with existing window.UC immediately', async () => { |
| 35 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 36 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 37 | + |
| 38 | + const result = await loadFileUploaderFrom(testUrl); |
| 39 | + |
| 40 | + expect(result).toBe(mockBlocks); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not create a new script element', async () => { |
| 44 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 45 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 46 | + const initialScriptCount = document.querySelectorAll('script').length; |
| 47 | + |
| 48 | + await loadFileUploaderFrom(testUrl); |
| 49 | + |
| 50 | + expect(document.querySelectorAll('script').length).toBe(initialScriptCount); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not call defineComponents even if register is true', async () => { |
| 54 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 55 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 56 | + |
| 57 | + await loadFileUploaderFrom(testUrl, true); |
| 58 | + |
| 59 | + expect(defineComponents).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('when loading script', () => { |
| 64 | + it('should append script to document head', async () => { |
| 65 | + const loadPromise = loadFileUploaderFrom(testUrl); |
| 66 | + |
| 67 | + const script = document.head.querySelector(`script[src="${testUrl}"]`); |
| 68 | + expect(script).not.toBeNull(); |
| 69 | + |
| 70 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 71 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 72 | + (script as HTMLScriptElement).onload?.(new Event('load')); |
| 73 | + |
| 74 | + await loadPromise; |
| 75 | + }); |
| 76 | + |
| 77 | + it('should resolve with window.UC on successful load', async () => { |
| 78 | + const loadPromise = loadFileUploaderFrom(testUrl); |
| 79 | + const mockBlocks = { TestBlock: { reg: vi.fn() }, AnotherBlock: { reg: vi.fn() } }; |
| 80 | + |
| 81 | + const script = document.querySelector(`script[src="${testUrl}"]`) as HTMLScriptElement; |
| 82 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 83 | + script.onload?.(new Event('load')); |
| 84 | + |
| 85 | + const result = await loadPromise; |
| 86 | + expect(result).toBe(mockBlocks); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should call defineComponents when register is true', async () => { |
| 90 | + const loadPromise = loadFileUploaderFrom(testUrl, true); |
| 91 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 92 | + |
| 93 | + const script = document.querySelector(`script[src="${testUrl}"]`) as HTMLScriptElement; |
| 94 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 95 | + script.onload?.(new Event('load')); |
| 96 | + |
| 97 | + await loadPromise; |
| 98 | + expect(defineComponents).toHaveBeenCalledWith(mockBlocks); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should not call defineComponents when register is false', async () => { |
| 102 | + const loadPromise = loadFileUploaderFrom(testUrl, false); |
| 103 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 104 | + |
| 105 | + const script = document.querySelector(`script[src="${testUrl}"]`) as HTMLScriptElement; |
| 106 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 107 | + script.onload?.(new Event('load')); |
| 108 | + |
| 109 | + await loadPromise; |
| 110 | + expect(defineComponents).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should not call defineComponents by default', async () => { |
| 114 | + const loadPromise = loadFileUploaderFrom(testUrl); |
| 115 | + const mockBlocks = { TestBlock: { reg: vi.fn() } }; |
| 116 | + |
| 117 | + const script = document.querySelector(`script[src="${testUrl}"]`) as HTMLScriptElement; |
| 118 | + (window as any)[UC_WINDOW_KEY] = mockBlocks; |
| 119 | + script.onload?.(new Event('load')); |
| 120 | + |
| 121 | + await loadPromise; |
| 122 | + expect(defineComponents).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should reject on script error', async () => { |
| 126 | + const loadPromise = loadFileUploaderFrom(testUrl); |
| 127 | + |
| 128 | + const script = document.querySelector(`script[src="${testUrl}"]`) as HTMLScriptElement; |
| 129 | + script.onerror?.(new Event('error')); |
| 130 | + |
| 131 | + await expect(loadPromise).rejects.toBeUndefined(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('non-browser environment', () => { |
| 136 | + it('should resolve with null when document is not an object', async () => { |
| 137 | + const originalDocument = global.document; |
| 138 | + |
| 139 | + // @ts-expect-error - intentionally setting to non-object for test |
| 140 | + global.document = undefined; |
| 141 | + |
| 142 | + const result = await loadFileUploaderFrom(testUrl); |
| 143 | + |
| 144 | + expect(result).toBeNull(); |
| 145 | + |
| 146 | + global.document = originalDocument; |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments