|
| 1 | +import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 2 | +import { createPlatform, setPlatform } from './platform'; |
| 3 | +import { getSymbolHash } from '../qrl/qrl-class'; |
| 4 | + |
| 5 | +describe('core platform', () => { |
| 6 | + beforeEach(() => { |
| 7 | + // Initialize a fresh Map for each test to avoid pollution |
| 8 | + (globalThis as any).__qwik_reg_symbols = new Map<string, any>(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + // Clean up global state |
| 13 | + delete (globalThis as any).__qwik_reg_symbols; |
| 14 | + }); |
| 15 | + |
| 16 | + describe('importSymbol - server mode', () => { |
| 17 | + test('returns registered symbol without importing', async () => { |
| 18 | + const platform = createPlatform(); |
| 19 | + |
| 20 | + // Register a mock symbol |
| 21 | + const symbolName = 'myComponent_abc123'; |
| 22 | + const hash = getSymbolHash(symbolName); |
| 23 | + const mockFunction = () => 'mock component'; |
| 24 | + (globalThis as any).__qwik_reg_symbols.set(hash, mockFunction); |
| 25 | + |
| 26 | + // importSymbol should return the registered symbol synchronously |
| 27 | + const result = await platform.importSymbol(null as any, '', symbolName); |
| 28 | + |
| 29 | + expect(result).toBe(mockFunction); |
| 30 | + }); |
| 31 | + |
| 32 | + test('throws error for unregistered symbol without importing', async () => { |
| 33 | + const platform = createPlatform(); |
| 34 | + |
| 35 | + const symbolName = 'unregisteredSymbol_xyz789'; |
| 36 | + |
| 37 | + // importSymbol should throw qError without attempting any dynamic import |
| 38 | + expect(() => platform.importSymbol(null as any, '', symbolName)).toThrow(); |
| 39 | + |
| 40 | + let didThrow = false; |
| 41 | + // Verify it throws with the correct error structure |
| 42 | + try { |
| 43 | + platform.importSymbol(null as any, '', symbolName); |
| 44 | + expect.unreachable('Should have thrown an error'); |
| 45 | + } catch (e: any) { |
| 46 | + didThrow = true; |
| 47 | + // The error should be a QError with code for dynamic import failed |
| 48 | + expect(e.message).toMatch(/Code\(\d+\)/); |
| 49 | + expect(e.message).toContain('Dynamic import not found'); |
| 50 | + } |
| 51 | + expect(didThrow).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + test('does not call dynamic import on server', async () => { |
| 55 | + const platform = createPlatform(); |
| 56 | + |
| 57 | + const symbolName = 'testSymbol_test123'; |
| 58 | + |
| 59 | + // Verify that the function throws synchronously without any async import |
| 60 | + const startTime = Date.now(); |
| 61 | + try { |
| 62 | + platform.importSymbol(null as any, '', symbolName); |
| 63 | + } catch (e) { |
| 64 | + // Expected to throw |
| 65 | + } |
| 66 | + const endTime = Date.now(); |
| 67 | + |
| 68 | + // Should complete nearly instantly (no network/file I/O) |
| 69 | + // If it took more than 100ms, something is probably doing async work |
| 70 | + expect(endTime - startTime).toBeLessThan(100); |
| 71 | + }); |
| 72 | + |
| 73 | + test('works with symbols containing multiple underscores in server mode', async () => { |
| 74 | + const platform = createPlatform(); |
| 75 | + |
| 76 | + const symbolName = 'my_component_with_underscores_abc123'; |
| 77 | + const hash = getSymbolHash(symbolName); |
| 78 | + const mockFunction = () => 'mock'; |
| 79 | + (globalThis as any).__qwik_reg_symbols.set(hash, mockFunction); |
| 80 | + |
| 81 | + const result = await platform.importSymbol(null as any, '', symbolName); |
| 82 | + |
| 83 | + expect(result).toBe(mockFunction); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('setPlatform and getPlatform', () => { |
| 88 | + test('setPlatform updates the platform', () => { |
| 89 | + const customPlatform = createPlatform(); |
| 90 | + const customImportSymbol = vi.fn(); |
| 91 | + customPlatform.importSymbol = customImportSymbol; |
| 92 | + |
| 93 | + setPlatform(customPlatform); |
| 94 | + |
| 95 | + // The platform should now use the custom implementation |
| 96 | + expect(customPlatform.importSymbol).toBe(customImportSymbol); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('platform utilities', () => { |
| 101 | + test('nextTick returns a promise and executes callback', async () => { |
| 102 | + const platform = createPlatform(); |
| 103 | + const mockFn = vi.fn(() => 'result'); |
| 104 | + |
| 105 | + const promise = platform.nextTick(mockFn); |
| 106 | + |
| 107 | + expect(promise).toBeInstanceOf(Promise); |
| 108 | + const result = await promise; |
| 109 | + expect(mockFn).toHaveBeenCalled(); |
| 110 | + expect(result).toBe('result'); |
| 111 | + }); |
| 112 | + |
| 113 | + test('chunkForSymbol returns symbol and chunk', () => { |
| 114 | + const platform = createPlatform(); |
| 115 | + |
| 116 | + const result1 = platform.chunkForSymbol('mySymbol', 'chunk.js'); |
| 117 | + expect(result1).toEqual(['mySymbol', 'chunk.js']); |
| 118 | + |
| 119 | + const result2 = platform.chunkForSymbol('mySymbol', null); |
| 120 | + expect(result2).toEqual(['mySymbol', '_']); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('getSymbolHash', () => { |
| 125 | + test('extracts hash after last underscore', () => { |
| 126 | + expect(getSymbolHash('mySymbol_abc123')).toBe('abc123'); |
| 127 | + expect(getSymbolHash('component_with_multiple_underscores_xyz789')).toBe('xyz789'); |
| 128 | + }); |
| 129 | + |
| 130 | + test('returns full name if no underscore present', () => { |
| 131 | + expect(getSymbolHash('noUnderscore')).toBe('noUnderscore'); |
| 132 | + expect(getSymbolHash('simpleSymbol')).toBe('simpleSymbol'); |
| 133 | + }); |
| 134 | + |
| 135 | + test('handles edge cases', () => { |
| 136 | + expect(getSymbolHash('_leadingUnderscore')).toBe('leadingUnderscore'); |
| 137 | + expect(getSymbolHash('trailingUnderscore_')).toBe(''); |
| 138 | + expect(getSymbolHash('_')).toBe(''); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments