|
| 1 | +import { checkForUpdate } from '../../../utils/checkForUpdate'; |
| 2 | +import { logWarning } from '../../../prompter'; |
| 3 | +import { runCommand } from '../../../utils/runCommand'; |
| 4 | + |
| 5 | +// Mock dependencies |
| 6 | +jest.mock('../../../prompter'); |
| 7 | +jest.mock('../../../utils/runCommand'); |
| 8 | +jest.mock('../../../../package.json', () => ({ |
| 9 | + version: '1.0.0', |
| 10 | + name: 'react-native-integrate', |
| 11 | +})); |
| 12 | + |
| 13 | +describe('checkForUpdate', () => { |
| 14 | + beforeEach(() => { |
| 15 | + jest.clearAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should show warning when newer version is available', async () => { |
| 19 | + // Mock runCommand to return a newer version |
| 20 | + (runCommand as jest.Mock).mockResolvedValue({ |
| 21 | + output: '1.1.0', |
| 22 | + exitCode: 0, |
| 23 | + }); |
| 24 | + |
| 25 | + await checkForUpdate(); |
| 26 | + |
| 27 | + expect(runCommand).toHaveBeenCalledWith( |
| 28 | + 'npm view react-native-integrate version', |
| 29 | + { silent: true } |
| 30 | + ); |
| 31 | + expect(logWarning).toHaveBeenCalledWith( |
| 32 | + expect.stringContaining('new version') |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should not show warning when current version is latest', async () => { |
| 37 | + // Mock runCommand to return same version |
| 38 | + (runCommand as jest.Mock).mockResolvedValue({ |
| 39 | + output: '1.0.0', |
| 40 | + exitCode: 0, |
| 41 | + }); |
| 42 | + |
| 43 | + await checkForUpdate(); |
| 44 | + |
| 45 | + expect(runCommand).toHaveBeenCalledWith( |
| 46 | + 'npm view react-native-integrate version', |
| 47 | + { silent: true } |
| 48 | + ); |
| 49 | + expect(logWarning).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not show warning when npm command fails', async () => { |
| 53 | + // Mock runCommand to throw error |
| 54 | + (runCommand as jest.Mock).mockRejectedValue(new Error('npm error')); |
| 55 | + |
| 56 | + await checkForUpdate(); |
| 57 | + |
| 58 | + expect(runCommand).toHaveBeenCalledWith( |
| 59 | + 'npm view react-native-integrate version', |
| 60 | + { silent: true } |
| 61 | + ); |
| 62 | + expect(logWarning).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not check for updates when version is not defined in package.json', async () => { |
| 66 | + // Mock package.json without version |
| 67 | + jest.resetModules(); |
| 68 | + jest.mock('../../../../package.json', () => ({ |
| 69 | + name: 'react-native-integrate', |
| 70 | + })); |
| 71 | + |
| 72 | + await checkForUpdate(); |
| 73 | + |
| 74 | + expect(runCommand).not.toHaveBeenCalled(); |
| 75 | + expect(logWarning).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments