|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +// Mock all heavy dependencies — we only care about the namespace logic |
| 4 | +const mockGlobalApi = { __mock: true } |
| 5 | + |
| 6 | +vi.mock('@dimina/common', () => ({ |
| 7 | + modDefine: vi.fn(), |
| 8 | + modRequire: vi.fn(), |
| 9 | +})) |
| 10 | +vi.mock('../src/api', () => ({ default: mockGlobalApi })) |
| 11 | +vi.mock('../src/instance/component/component-module', () => ({ ComponentModule: { type: 'component' } })) |
| 12 | +vi.mock('../src/instance/page/page-module', () => ({ PageModule: { type: 'page' } })) |
| 13 | +vi.mock('../src/core/loader', () => ({ default: { createAppModule: vi.fn(), createModule: vi.fn() } })) |
| 14 | +vi.mock('../src/core/router', () => ({ default: { stack: vi.fn(() => []) } })) |
| 15 | +vi.mock('../src/core/runtime', () => ({ default: { app: null } })) |
| 16 | + |
| 17 | +describe('env.js API namespace registration', () => { |
| 18 | + beforeEach(() => { |
| 19 | + // Clean up namespace-related globals before each test |
| 20 | + delete globalThis.__diminaApiNamespaces |
| 21 | + delete globalThis.qd |
| 22 | + delete globalThis.myapp |
| 23 | + delete globalThis.dd |
| 24 | + delete globalThis.wx |
| 25 | + // Simulate Worker's self (not available in Node) |
| 26 | + globalThis.self = { name: '' } |
| 27 | + }) |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + vi.resetModules() |
| 31 | + }) |
| 32 | + |
| 33 | + it('should set dd and wx to globalApi', async () => { |
| 34 | + await import('../src/core/env.js') |
| 35 | + |
| 36 | + expect(globalThis.dd).toBe(mockGlobalApi) |
| 37 | + expect(globalThis.wx).toBe(mockGlobalApi) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should register namespaces from __diminaApiNamespaces', async () => { |
| 41 | + globalThis.__diminaApiNamespaces = ['qd', 'myapp'] |
| 42 | + |
| 43 | + await import('../src/core/env.js') |
| 44 | + |
| 45 | + expect(globalThis.qd).toBe(mockGlobalApi) |
| 46 | + expect(globalThis.myapp).toBe(mockGlobalApi) |
| 47 | + // dd and wx should still work |
| 48 | + expect(globalThis.dd).toBe(mockGlobalApi) |
| 49 | + expect(globalThis.wx).toBe(mockGlobalApi) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should not create extra globals when __diminaApiNamespaces is empty', async () => { |
| 53 | + globalThis.__diminaApiNamespaces = [] |
| 54 | + |
| 55 | + await import('../src/core/env.js') |
| 56 | + |
| 57 | + expect(globalThis.qd).toBeUndefined() |
| 58 | + }) |
| 59 | + |
| 60 | + it('should fall back to self.name when __diminaApiNamespaces is not set', async () => { |
| 61 | + globalThis.name = JSON.stringify({ apiNamespaces: ['qd'] }) |
| 62 | + |
| 63 | + await import('../src/core/env.js') |
| 64 | + |
| 65 | + expect(globalThis.qd).toBe(mockGlobalApi) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should ignore invalid JSON in globalThis.name', async () => { |
| 69 | + globalThis.name = 'not-json' |
| 70 | + |
| 71 | + await import('../src/core/env.js') |
| 72 | + |
| 73 | + expect(globalThis.qd).toBeUndefined() |
| 74 | + // Core globals should still work |
| 75 | + expect(globalThis.wx).toBe(mockGlobalApi) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should prefer __diminaApiNamespaces over self.name', async () => { |
| 79 | + globalThis.__diminaApiNamespaces = ['qd'] |
| 80 | + globalThis.name = JSON.stringify({ apiNamespaces: ['myapp'] }) |
| 81 | + |
| 82 | + await import('../src/core/env.js') |
| 83 | + |
| 84 | + expect(globalThis.qd).toBe(mockGlobalApi) |
| 85 | + expect(globalThis.myapp).toBeUndefined() |
| 86 | + }) |
| 87 | +}) |
0 commit comments