|
| 1 | +import { beforeEach, describe, it, expect, vi } from 'vitest' |
| 2 | + |
| 3 | +import * as clack from '@clack/prompts' |
| 4 | + |
| 5 | +import { createUIEnvironment } from '../src/ui-environment.js' |
| 6 | + |
| 7 | +vi.mock('@clack/prompts') |
| 8 | + |
| 9 | +// @ts-expect-error |
| 10 | +vi.spyOn(process, 'exit').mockImplementation(() => {}) |
| 11 | + |
| 12 | +describe('createUIEnvironment', () => { |
| 13 | + it('should create a silent UI environment', () => { |
| 14 | + const environment = createUIEnvironment('test', true) |
| 15 | + expect(environment.appName).toBe('test') |
| 16 | + }) |
| 17 | + |
| 18 | + it('should handle intro', () => { |
| 19 | + const environment = createUIEnvironment('test', false) |
| 20 | + const spy = vi |
| 21 | + .spyOn(clack, 'intro') |
| 22 | + .mockImplementation(async () => undefined) |
| 23 | + environment.intro('test') |
| 24 | + expect(spy).toHaveBeenCalledWith('test') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should handle outro', () => { |
| 28 | + const environment = createUIEnvironment('test', false) |
| 29 | + const spy = vi |
| 30 | + .spyOn(clack, 'outro') |
| 31 | + .mockImplementation(async () => undefined) |
| 32 | + environment.outro('test') |
| 33 | + expect(spy).toHaveBeenCalledWith('test') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should handle info', () => { |
| 37 | + const environment = createUIEnvironment('test', false) |
| 38 | + const spy = vi |
| 39 | + .spyOn(clack.log, 'info') |
| 40 | + .mockImplementation(async () => undefined) |
| 41 | + environment.info('test') |
| 42 | + expect(spy).toHaveBeenCalled() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should handle error', () => { |
| 46 | + const environment = createUIEnvironment('test', false) |
| 47 | + const spy = vi |
| 48 | + .spyOn(clack.log, 'error') |
| 49 | + .mockImplementation(async () => undefined) |
| 50 | + environment.error('test') |
| 51 | + expect(spy).toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle warn', () => { |
| 55 | + const environment = createUIEnvironment('test', false) |
| 56 | + const spy = vi |
| 57 | + .spyOn(clack.log, 'warn') |
| 58 | + .mockImplementation(async () => undefined) |
| 59 | + environment.warn('test') |
| 60 | + expect(spy).toHaveBeenCalled() |
| 61 | + }) |
| 62 | + |
| 63 | + it('should handle confirm', async () => { |
| 64 | + const environment = createUIEnvironment('test', false) |
| 65 | + const spy = vi.spyOn(clack, 'confirm').mockImplementation(async () => true) |
| 66 | + const isCancelSpy = vi |
| 67 | + .spyOn(clack, 'isCancel') |
| 68 | + .mockImplementation(() => false) |
| 69 | + const result = await environment.confirm('test') |
| 70 | + expect(spy).toHaveBeenCalled() |
| 71 | + expect(isCancelSpy).toHaveBeenCalled() |
| 72 | + expect(result).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should handle confirm', async () => { |
| 76 | + const environment = createUIEnvironment('test', false) |
| 77 | + const spy = vi.spyOn(clack, 'confirm').mockImplementation(async () => true) |
| 78 | + const isCancelSpy = vi |
| 79 | + .spyOn(clack, 'isCancel') |
| 80 | + .mockImplementation(() => true) |
| 81 | + await environment.confirm('test') |
| 82 | + expect(spy).toHaveBeenCalled() |
| 83 | + expect(isCancelSpy).toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should handle spinner', async () => { |
| 87 | + const environment = createUIEnvironment('test', false) |
| 88 | + // @ts-expect-error |
| 89 | + const spy = vi.spyOn(clack, 'spinner').mockImplementation(async () => ({ |
| 90 | + start: (_msg?: string) => {}, |
| 91 | + stop: (_msg?: string) => {}, |
| 92 | + message: (_msg?: string) => {}, |
| 93 | + })) |
| 94 | + const result = await environment.spinner() |
| 95 | + expect(spy).toHaveBeenCalled() |
| 96 | + }) |
| 97 | +}) |
0 commit comments