|
| 1 | +import type { AppIconBadgeConfig } from 'app-icon-badge/types'; |
| 2 | + |
| 3 | +const mockExecFileSync = jest.fn(); |
| 4 | +const mockExistsSync = jest.fn(() => true); |
| 5 | +const mockStatSync = jest.fn(() => ({ size: 100 })); |
| 6 | + |
| 7 | +jest.mock('child_process', () => ({ |
| 8 | + execFileSync: mockExecFileSync, |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('fs', () => ({ |
| 12 | + existsSync: mockExistsSync, |
| 13 | + statSync: mockStatSync, |
| 14 | +})); |
| 15 | + |
| 16 | +interface TestExpoConfig { |
| 17 | + _internal?: { |
| 18 | + projectRoot?: string; |
| 19 | + }; |
| 20 | + android?: { |
| 21 | + adaptiveIcon?: { |
| 22 | + foregroundImage?: string; |
| 23 | + }; |
| 24 | + }; |
| 25 | + icon?: string; |
| 26 | + ios?: { |
| 27 | + icon?: string; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +const withAppIconBadge = jest.requireActual('../with-app-icon-badge.js') as (config: TestExpoConfig, options?: AppIconBadgeConfig) => TestExpoConfig; |
| 32 | + |
| 33 | +describe('withAppIconBadge', () => { |
| 34 | + beforeEach(() => { |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('generates all configured icons before rewriting their config paths', () => { |
| 39 | + const config: TestExpoConfig = { |
| 40 | + _internal: { projectRoot: '/project' }, |
| 41 | + icon: './assets/icon.png', |
| 42 | + ios: { icon: './assets/ios-icon.png' }, |
| 43 | + android: { adaptiveIcon: { foregroundImage: './assets/adaptive-icon.png' } }, |
| 44 | + }; |
| 45 | + const options: AppIconBadgeConfig = { |
| 46 | + enabled: true, |
| 47 | + badges: [{ type: 'banner', text: 'development' }], |
| 48 | + }; |
| 49 | + |
| 50 | + const result = withAppIconBadge(config, options); |
| 51 | + |
| 52 | + expect(mockExecFileSync).toHaveBeenCalledTimes(1); |
| 53 | + const generatorArguments = mockExecFileSync.mock.calls[0]?.[1] as string[]; |
| 54 | + const payload = JSON.parse(generatorArguments[1] ?? '{}') as { |
| 55 | + jobs: { isAdaptiveIcon: boolean; outputPath: string; sourcePath: string }[]; |
| 56 | + }; |
| 57 | + expect(payload.jobs).toEqual([ |
| 58 | + { |
| 59 | + sourcePath: '/project/assets/icon.png', |
| 60 | + outputPath: '/project/.expo/app-icon-badge/icon.png', |
| 61 | + isAdaptiveIcon: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + sourcePath: '/project/assets/ios-icon.png', |
| 65 | + outputPath: '/project/.expo/app-icon-badge/ios-icon.png', |
| 66 | + isAdaptiveIcon: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + sourcePath: '/project/assets/adaptive-icon.png', |
| 70 | + outputPath: '/project/.expo/app-icon-badge/foregroundImage.png', |
| 71 | + isAdaptiveIcon: true, |
| 72 | + }, |
| 73 | + ]); |
| 74 | + expect(result.icon).toBe('.expo/app-icon-badge/icon.png'); |
| 75 | + expect(result.ios?.icon).toBe('.expo/app-icon-badge/ios-icon.png'); |
| 76 | + expect(result.android?.adaptiveIcon?.foregroundImage).toBe('.expo/app-icon-badge/foregroundImage.png'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('leaves icon paths untouched when badges are disabled', () => { |
| 80 | + const config: TestExpoConfig = { |
| 81 | + _internal: { projectRoot: '/project' }, |
| 82 | + icon: './assets/icon.png', |
| 83 | + android: { adaptiveIcon: { foregroundImage: './assets/adaptive-icon.png' } }, |
| 84 | + }; |
| 85 | + |
| 86 | + const result = withAppIconBadge(config, { enabled: false, badges: [] }); |
| 87 | + |
| 88 | + expect(mockExecFileSync).not.toHaveBeenCalled(); |
| 89 | + expect(result.icon).toBe('./assets/icon.png'); |
| 90 | + expect(result.android?.adaptiveIcon?.foregroundImage).toBe('./assets/adaptive-icon.png'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments