-
Notifications
You must be signed in to change notification settings - Fork 452
feat: support switching to a known user #8046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
84d36e3
70ef776
ecad52d
53a2508
09c6925
f8c4ab5
7b2f737
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
|
||
| const { logMessages, mockPrompt, mockLogin } = vi.hoisted(() => ({ | ||
| logMessages: [] as string[], | ||
| mockPrompt: vi.fn(), | ||
| mockLogin: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('inquirer', () => ({ | ||
| default: { prompt: mockPrompt }, | ||
| })) | ||
|
|
||
| vi.mock('../../../../src/utils/command-helpers.js', async () => ({ | ||
| ...(await vi.importActual('../../../../src/utils/command-helpers.js')), | ||
| log: (...args: string[]) => { | ||
| logMessages.push(args.join(' ')) | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('../../../../src/commands/login/login.js', () => ({ | ||
| login: mockLogin, | ||
| })) | ||
|
|
||
| import { switchCommand } from '../../../../src/commands/switch/switch.js' | ||
|
|
||
| const users = { | ||
| 'user-1': { id: 'user-1', name: 'Alice', email: 'alice@example.com' }, | ||
| 'user-2': { id: 'user-2', name: 'Bob', email: 'bob@corp.com' }, | ||
| } | ||
|
|
||
| const createCommand = (usersData = users) => { | ||
| const mockSet = vi.fn() | ||
| const command = { | ||
| netlify: { | ||
| globalConfig: { | ||
| get: vi.fn().mockReturnValue(usersData), | ||
| set: mockSet, | ||
| }, | ||
| }, | ||
| } | ||
| return { command, mockSet } | ||
| } | ||
|
|
||
| describe('switchCommand', () => { | ||
| beforeEach(() => { | ||
| logMessages.length = 0 | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| test('--email auto-switches when a match is found', async () => { | ||
| const { command, mockSet } = createCommand() | ||
|
|
||
| await switchCommand({ email: 'alice@example.com' }, command as any) | ||
|
Check failure on line 53 in tests/unit/commands/switch/switch.test.ts
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| expect(mockSet).toHaveBeenCalledWith('userId', 'user-1') | ||
| expect(logMessages.some((m) => m.includes('Alice'))).toBe(true) | ||
| expect(mockPrompt).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('--email falls through to prompt when no match is found', async () => { | ||
| const { command } = createCommand() | ||
| mockPrompt.mockResolvedValueOnce({ accountSwitchChoice: 'Bob (bob@corp.com)' }) | ||
|
|
||
| await switchCommand({ email: 'nobody@example.com' }, command as any) | ||
|
Check failure on line 64 in tests/unit/commands/switch/switch.test.ts
|
||
|
|
||
| expect(logMessages.some((m) => m.includes('No account found matching'))).toBe(true) | ||
| expect(mockPrompt).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('--email matches partial email strings', async () => { | ||
| const { command, mockSet } = createCommand() | ||
|
|
||
| await switchCommand({ email: 'bob@corp' }, command as any) | ||
|
Check failure on line 73 in tests/unit/commands/switch/switch.test.ts
|
||
|
|
||
| expect(mockSet).toHaveBeenCalledWith('userId', 'user-2') | ||
| expect(mockPrompt).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('without --email shows interactive prompt', async () => { | ||
| const { command, mockSet } = createCommand() | ||
| mockPrompt.mockResolvedValueOnce({ accountSwitchChoice: 'Alice (alice@example.com)' }) | ||
|
|
||
| await switchCommand({}, command as any) | ||
|
Check failure on line 83 in tests/unit/commands/switch/switch.test.ts
|
||
|
|
||
| expect(mockPrompt).toHaveBeenCalled() | ||
| expect(mockSet).toHaveBeenCalledWith('userId', 'user-1') | ||
| }) | ||
|
|
||
| test('selecting login new triggers login flow', async () => { | ||
| const { command } = createCommand() | ||
| mockPrompt.mockResolvedValueOnce({ accountSwitchChoice: 'I would like to login to a new account' }) | ||
|
|
||
| await switchCommand({}, command as any) | ||
|
Check failure on line 93 in tests/unit/commands/switch/switch.test.ts
|
||
|
|
||
| expect(mockLogin).toHaveBeenCalledWith({ new: true }, command) | ||
| }) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.