|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { useNuxt, findPath } from '@nuxt/kit' |
| 3 | +import { useCapacitor } 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 | + options: { |
| 14 | + typescript: { |
| 15 | + tsConfig: { |
| 16 | + exclude: [], |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks() |
| 24 | + vi.mocked(useNuxt).mockReturnValue(mockNuxt as any) |
| 25 | + mockNuxt.options.typescript.tsConfig.exclude = [] |
| 26 | + }) |
| 27 | + |
| 28 | + describe('findCapacitorConfig', () => { |
| 29 | + it('should find capacitor.config.ts', async () => { |
| 30 | + const mockPath = '/project/capacitor.config.ts' |
| 31 | + vi.mocked(findPath).mockResolvedValue(mockPath) |
| 32 | + |
| 33 | + const { findCapacitorConfig } = useCapacitor() |
| 34 | + const result = await findCapacitorConfig() |
| 35 | + |
| 36 | + expect(result).toBe(mockPath) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return null when no config found', async () => { |
| 40 | + vi.mocked(findPath).mockResolvedValue(null) |
| 41 | + |
| 42 | + const { findCapacitorConfig } = useCapacitor() |
| 43 | + const result = await findCapacitorConfig() |
| 44 | + |
| 45 | + expect(result).toBeNull() |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('parseCapacitorConfig', () => { |
| 50 | + it('should return null paths when no config path provided', async () => { |
| 51 | + const { parseCapacitorConfig } = useCapacitor() |
| 52 | + const result = await parseCapacitorConfig(null) |
| 53 | + |
| 54 | + expect(result).toEqual({ |
| 55 | + androidPath: null, |
| 56 | + iosPath: null, |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should parse capacitor config with custom paths', async () => { |
| 61 | + const configPath = './capacitor.config.ts' |
| 62 | + const mockConfig = { |
| 63 | + android: { path: 'custom-android' }, |
| 64 | + ios: { path: 'custom-ios' }, |
| 65 | + } |
| 66 | + |
| 67 | + vi.doMock(configPath, () => ({ |
| 68 | + default: mockConfig, |
| 69 | + ...mockConfig, |
| 70 | + })) |
| 71 | + |
| 72 | + const { parseCapacitorConfig } = useCapacitor() |
| 73 | + const result = await parseCapacitorConfig(configPath) |
| 74 | + |
| 75 | + expect(result).toEqual({ |
| 76 | + androidPath: 'custom-android', |
| 77 | + iosPath: 'custom-ios', |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should handle config without android/ios paths', async () => { |
| 82 | + const configPath = './capacitor.config.ts' |
| 83 | + const mockConfig = { |
| 84 | + android: undefined, |
| 85 | + ios: undefined, |
| 86 | + } |
| 87 | + |
| 88 | + vi.doMock(configPath, () => ({ |
| 89 | + default: mockConfig, |
| 90 | + ...mockConfig, |
| 91 | + })) |
| 92 | + |
| 93 | + const { parseCapacitorConfig } = useCapacitor() |
| 94 | + const result = await parseCapacitorConfig(configPath) |
| 95 | + |
| 96 | + expect(result).toEqual({ |
| 97 | + androidPath: null, |
| 98 | + iosPath: null, |
| 99 | + }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('excludeNativeFolders', () => { |
| 104 | + it('should add native folders to typescript exclude', () => { |
| 105 | + const { excludeNativeFolders } = useCapacitor() |
| 106 | + excludeNativeFolders('android', 'ios') |
| 107 | + |
| 108 | + expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android') |
| 109 | + expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../ios') |
| 110 | + }) |
| 111 | + |
| 112 | + it('should handle null paths with defaults', () => { |
| 113 | + const { excludeNativeFolders } = useCapacitor() |
| 114 | + excludeNativeFolders(null, null) |
| 115 | + |
| 116 | + expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android') |
| 117 | + expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../ios') |
| 118 | + }) |
| 119 | + |
| 120 | + it('should initialize tsConfig if not present', () => { |
| 121 | + // @ts-expect-error should not be undefined |
| 122 | + mockNuxt.options.typescript.tsConfig = undefined |
| 123 | + |
| 124 | + const { excludeNativeFolders } = useCapacitor() |
| 125 | + excludeNativeFolders('android', 'ios') |
| 126 | + |
| 127 | + expect(mockNuxt.options.typescript.tsConfig).toBeDefined() |
| 128 | + expect(mockNuxt.options.typescript.tsConfig.exclude).toContain('../android') |
| 129 | + }) |
| 130 | + }) |
| 131 | +}) |
0 commit comments