|
| 1 | +import process from 'process' |
| 2 | + |
| 3 | +import { afterAll, afterEach, beforeEach, describe, expect, test, vi, type MockInstance } from 'vitest' |
| 4 | + |
| 5 | +import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js' |
| 6 | +import type { MinimalAccount } from '../../../../src/utils/types.js' |
| 7 | +import { startSpinner } from '../../../../src/lib/spinner.js' |
| 8 | + |
| 9 | +vi.mock('../../../../src/lib/spinner.js', async () => { |
| 10 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion |
| 11 | + const realStartSpinner = (await vi.importActual( |
| 12 | + '../../../../src/lib/spinner.js', |
| 13 | + )) as typeof import('../../../../src/lib/spinner.js') |
| 14 | + |
| 15 | + return { |
| 16 | + ...realStartSpinner, |
| 17 | + startSpinner: vi.fn(() => ({ stop: vi.fn(), error: vi.fn(), clear: vi.fn() })), |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +const siteInfo = { |
| 22 | + account_slug: 'test-account', |
| 23 | + id: 'site_id', |
| 24 | + name: 'site-name', |
| 25 | + admin_url: 'https://app.netlify.com/projects/test-site/overview', |
| 26 | + url: 'https://test-site.netlify.app/', |
| 27 | + ssl_url: 'https://test-site.netlify.app/', |
| 28 | +} |
| 29 | + |
| 30 | +const user = { full_name: 'Test User', email: 'test@netlify.com' } |
| 31 | + |
| 32 | +const accounts: MinimalAccount[] = [ |
| 33 | + { |
| 34 | + id: 'user-id', |
| 35 | + name: user.full_name, |
| 36 | + slug: siteInfo.account_slug, |
| 37 | + default: true, |
| 38 | + team_logo_url: null, |
| 39 | + on_pro_trial: false, |
| 40 | + organization_id: null, |
| 41 | + type_name: 'placeholder', |
| 42 | + type_slug: 'placeholder', |
| 43 | + members_count: 1, |
| 44 | + }, |
| 45 | +] |
| 46 | + |
| 47 | +const routes = [ |
| 48 | + { path: 'sites/site_id', response: siteInfo }, |
| 49 | + { path: 'sites/site_id/builds', response: [] }, |
| 50 | + { path: 'accounts', response: accounts }, |
| 51 | +] |
| 52 | + |
| 53 | +const importModules = async () => { |
| 54 | + const { default: BaseCommand } = await import('../../../../src/commands/base-command.js') |
| 55 | + const { createWatchCommand } = await import('../../../../src/commands/watch/index.js') |
| 56 | + |
| 57 | + return { BaseCommand, createWatchCommand } |
| 58 | +} |
| 59 | + |
| 60 | +const OLD_ENV = process.env |
| 61 | +const OLD_ARGV = process.argv |
| 62 | + |
| 63 | +describe('watch command', () => { |
| 64 | + let stdoutSpy: MockInstance |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + vi.clearAllMocks() |
| 68 | + vi.resetModules() |
| 69 | + Object.defineProperty(process, 'env', { value: {} }) |
| 70 | + stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 71 | + }) |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + stdoutSpy.mockRestore() |
| 75 | + process.argv = OLD_ARGV |
| 76 | + vi.resetModules() |
| 77 | + }) |
| 78 | + |
| 79 | + afterAll(() => { |
| 80 | + vi.restoreAllMocks() |
| 81 | + Object.defineProperty(process, 'env', { value: OLD_ENV }) |
| 82 | + }) |
| 83 | + |
| 84 | + test('should start spinner when --silent flag is not passed', async () => { |
| 85 | + process.argv = ['node', 'netlify', 'watch'] |
| 86 | + const { BaseCommand, createWatchCommand } = await importModules() |
| 87 | + |
| 88 | + await withMockApi(routes, async ({ apiUrl }) => { |
| 89 | + Object.assign(process.env, getEnvironmentVariables({ apiUrl })) |
| 90 | + |
| 91 | + const program = new BaseCommand('netlify') |
| 92 | + createWatchCommand(program) |
| 93 | + await program.parseAsync(['', '', 'watch']) |
| 94 | + |
| 95 | + expect(startSpinner).toHaveBeenCalledOnce() |
| 96 | + expect(startSpinner).toHaveBeenCalledWith({ text: 'Waiting for active project deploys to complete' }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + test('should not start spinner when --silent flag is passed', async () => { |
| 101 | + process.argv = ['node', 'netlify', 'watch', '--silent'] |
| 102 | + const { BaseCommand, createWatchCommand } = await importModules() |
| 103 | + |
| 104 | + await withMockApi(routes, async ({ apiUrl }) => { |
| 105 | + Object.assign(process.env, getEnvironmentVariables({ apiUrl })) |
| 106 | + |
| 107 | + const program = new BaseCommand('netlify') |
| 108 | + createWatchCommand(program) |
| 109 | + await program.parseAsync(['', '', 'watch', '--silent']) |
| 110 | + |
| 111 | + expect(startSpinner).not.toHaveBeenCalled() |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + test('should not print to stdout when --silent flag is passed', async () => { |
| 116 | + process.argv = ['node', 'netlify', 'watch', '--silent'] |
| 117 | + const { BaseCommand, createWatchCommand } = await importModules() |
| 118 | + |
| 119 | + await withMockApi(routes, async ({ apiUrl }) => { |
| 120 | + Object.assign(process.env, getEnvironmentVariables({ apiUrl })) |
| 121 | + |
| 122 | + const program = new BaseCommand('netlify') |
| 123 | + createWatchCommand(program) |
| 124 | + await program.parseAsync(['', '', 'watch', '--silent']) |
| 125 | + |
| 126 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + test('should allow output to stdout when --silent flag is not passed', async () => { |
| 131 | + process.argv = ['node', 'netlify', 'watch'] |
| 132 | + const { BaseCommand, createWatchCommand } = await importModules() |
| 133 | + |
| 134 | + await withMockApi(routes, async ({ apiUrl }) => { |
| 135 | + Object.assign(process.env, getEnvironmentVariables({ apiUrl })) |
| 136 | + |
| 137 | + const program = new BaseCommand('netlify') |
| 138 | + createWatchCommand(program) |
| 139 | + await program.parseAsync(['', '', 'watch']) |
| 140 | + |
| 141 | + expect(stdoutSpy).toHaveBeenCalledTimes(3) |
| 142 | + }) |
| 143 | + }) |
| 144 | +}) |
0 commit comments