|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { useNuxt, findPath } from '@nuxt/kit' |
| 3 | +import { setupCapacitor } from '../../src/parts/capacitor' |
| 4 | + |
| 5 | +// Mock @nuxt/kit |
| 6 | +vi.mock('@nuxt/kit', () => ({ |
| 7 | + findPath: vi.fn(), |
| 8 | + useNuxt: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +describe('useCapacitor', () => { |
| 12 | + const mockNuxt = { |
| 13 | + hook: vi.fn(), |
| 14 | + options: { |
| 15 | + ignore: [], |
| 16 | + }, |
| 17 | + } |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks() |
| 21 | + vi.mocked(useNuxt).mockReturnValue(mockNuxt as any) |
| 22 | + mockNuxt.options.ignore = [] |
| 23 | + }) |
| 24 | + |
| 25 | + describe('findCapacitorConfig', () => { |
| 26 | + it('should find capacitor.config.ts', async () => { |
| 27 | + const mockPath = '/project/capacitor.config.ts' |
| 28 | + vi.mocked(findPath).mockResolvedValue(mockPath) |
| 29 | + |
| 30 | + const { findCapacitorConfig } = setupCapacitor() |
| 31 | + const result = await findCapacitorConfig() |
| 32 | + |
| 33 | + expect(result).toBe(mockPath) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should return null when no config found', async () => { |
| 37 | + vi.mocked(findPath).mockResolvedValue(null) |
| 38 | + |
| 39 | + const { findCapacitorConfig } = setupCapacitor() |
| 40 | + const result = await findCapacitorConfig() |
| 41 | + |
| 42 | + expect(result).toBeNull() |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('parseCapacitorConfig', () => { |
| 47 | + it('should return null paths when no config path provided', async () => { |
| 48 | + const { parseCapacitorConfig } = setupCapacitor() |
| 49 | + const result = await parseCapacitorConfig(null) |
| 50 | + |
| 51 | + expect(result).toEqual({ |
| 52 | + androidPath: null, |
| 53 | + iosPath: null, |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should parse capacitor config with custom paths', async () => { |
| 58 | + const configPath = './capacitor.config.ts' |
| 59 | + const mockConfig = { |
| 60 | + android: { path: 'custom-android' }, |
| 61 | + ios: { path: 'custom-ios' }, |
| 62 | + } |
| 63 | + |
| 64 | + vi.doMock(configPath, () => ({ |
| 65 | + default: mockConfig, |
| 66 | + ...mockConfig, |
| 67 | + })) |
| 68 | + |
| 69 | + const { parseCapacitorConfig } = setupCapacitor() |
| 70 | + const result = await parseCapacitorConfig(configPath) |
| 71 | + |
| 72 | + expect(result).toEqual({ |
| 73 | + androidPath: 'custom-android', |
| 74 | + iosPath: 'custom-ios', |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should handle config without android/ios paths', async () => { |
| 79 | + const configPath = './capacitor.config.ts' |
| 80 | + const mockConfig = { |
| 81 | + android: undefined, |
| 82 | + ios: undefined, |
| 83 | + } |
| 84 | + |
| 85 | + vi.doMock(configPath, () => ({ |
| 86 | + default: mockConfig, |
| 87 | + ...mockConfig, |
| 88 | + })) |
| 89 | + |
| 90 | + const { parseCapacitorConfig } = setupCapacitor() |
| 91 | + const result = await parseCapacitorConfig(configPath) |
| 92 | + |
| 93 | + expect(result).toEqual({ |
| 94 | + androidPath: null, |
| 95 | + iosPath: null, |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + describe('excludeNativeFolders', () => { |
| 101 | + it('should register prepare:types hook and add native folders to ignore', () => { |
| 102 | + const { excludeNativeFolders } = setupCapacitor() |
| 103 | + excludeNativeFolders('android', 'ios') |
| 104 | + |
| 105 | + expect(mockNuxt.hook).toHaveBeenCalledWith('prepare:types', expect.any(Function)) |
| 106 | + expect(mockNuxt.options.ignore).toContain('android') |
| 107 | + expect(mockNuxt.options.ignore).toContain('ios') |
| 108 | + }) |
| 109 | + |
| 110 | + it('should handle null paths with defaults', () => { |
| 111 | + const { excludeNativeFolders } = setupCapacitor() |
| 112 | + excludeNativeFolders(null, null) |
| 113 | + |
| 114 | + expect(mockNuxt.hook).toHaveBeenCalledWith('prepare:types', expect.any(Function)) |
| 115 | + expect(mockNuxt.options.ignore).toContain('android') |
| 116 | + expect(mockNuxt.options.ignore).toContain('ios') |
| 117 | + }) |
| 118 | + |
| 119 | + it('should modify typescript configs in prepare:types hook', () => { |
| 120 | + const { excludeNativeFolders } = setupCapacitor() |
| 121 | + excludeNativeFolders('custom-android', 'custom-ios') |
| 122 | + |
| 123 | + // Get the hook callback that was registered |
| 124 | + const hookCallback = mockNuxt.hook.mock.calls.find(call => call[0] === 'prepare:types')?.[1] |
| 125 | + expect(hookCallback).toBeDefined() |
| 126 | + |
| 127 | + // Mock typescript context |
| 128 | + const mockCtx = { |
| 129 | + tsConfig: { exclude: [] }, |
| 130 | + nodeTsConfig: { exclude: [] }, |
| 131 | + sharedTsConfig: { exclude: [] }, |
| 132 | + } |
| 133 | + |
| 134 | + // Call the hook callback |
| 135 | + hookCallback(mockCtx) |
| 136 | + |
| 137 | + // Verify all configs were updated |
| 138 | + expect(mockCtx.tsConfig.exclude).toContain('../custom-android') |
| 139 | + expect(mockCtx.tsConfig.exclude).toContain('../custom-ios') |
| 140 | + expect(mockCtx.nodeTsConfig.exclude).toContain('../custom-android') |
| 141 | + expect(mockCtx.nodeTsConfig.exclude).toContain('../custom-ios') |
| 142 | + expect(mockCtx.sharedTsConfig.exclude).toContain('../custom-android') |
| 143 | + expect(mockCtx.sharedTsConfig.exclude).toContain('../custom-ios') |
| 144 | + }) |
| 145 | + |
| 146 | + it('should initialize exclude arrays if not present in typescript configs', () => { |
| 147 | + const { excludeNativeFolders } = setupCapacitor() |
| 148 | + excludeNativeFolders('android', 'ios') |
| 149 | + |
| 150 | + const hookCallback = mockNuxt.hook.mock.calls.find(call => call[0] === 'prepare:types')?.[1] |
| 151 | + |
| 152 | + // Mock context without exclude arrays |
| 153 | + const mockCtx = { |
| 154 | + tsConfig: {} as any, |
| 155 | + nodeTsConfig: {} as any, |
| 156 | + sharedTsConfig: {} as any, |
| 157 | + } |
| 158 | + |
| 159 | + hookCallback(mockCtx) |
| 160 | + |
| 161 | + expect(mockCtx.tsConfig.exclude).toEqual(['../android', '../ios']) |
| 162 | + expect(mockCtx.nodeTsConfig.exclude).toEqual(['../android', '../ios']) |
| 163 | + expect(mockCtx.sharedTsConfig.exclude).toEqual(['../android', '../ios']) |
| 164 | + }) |
| 165 | + |
| 166 | + it('should handle missing typescript configs gracefully', () => { |
| 167 | + const { excludeNativeFolders } = setupCapacitor() |
| 168 | + excludeNativeFolders('android', 'ios') |
| 169 | + |
| 170 | + const hookCallback = mockNuxt.hook.mock.calls.find(call => call[0] === 'prepare:types')?.[1] |
| 171 | + |
| 172 | + // Mock context with only some configs present |
| 173 | + const mockCtx = { |
| 174 | + tsConfig: { exclude: [] }, |
| 175 | + // nodeTsConfig and sharedTsConfig are undefined |
| 176 | + } |
| 177 | + |
| 178 | + expect(() => hookCallback(mockCtx)).not.toThrow() |
| 179 | + expect(mockCtx.tsConfig.exclude).toContain('../android') |
| 180 | + expect(mockCtx.tsConfig.exclude).toContain('../ios') |
| 181 | + }) |
| 182 | + }) |
| 183 | +}) |
0 commit comments