-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for EnvironmentVariableDelete
- Loading branch information
1 parent
549e007
commit 11957c3
Showing
4 changed files
with
194 additions
and
37 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
packages/eas-cli/src/commands/env/__tests__/EnvironmentVariableDelete.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Config } from '@oclif/core'; | ||
|
||
import { | ||
EnvironmentVariableEnvironment, | ||
EnvironmentVariableScope, | ||
} from '../../../graphql/generated'; | ||
import { EnvironmentVariableMutation } from '../../../graphql/mutations/EnvironmentVariableMutation'; | ||
import { EnvironmentVariablesQuery } from '../../../graphql/queries/EnvironmentVariablesQuery'; | ||
import Log from '../../../log'; | ||
import { promptAsync, toggleConfirmAsync } from '../../../prompts'; | ||
import EnvironmentVariableDelete from '../delete'; | ||
|
||
jest.mock('../../../graphql/queries/EnvironmentVariablesQuery'); | ||
jest.mock('../../../graphql/mutations/EnvironmentVariableMutation'); | ||
jest.mock('../../../prompts'); | ||
jest.mock('../../../log'); | ||
|
||
describe(EnvironmentVariableDelete, () => { | ||
const projectId = 'test-project-id'; | ||
const variableId = '1'; | ||
const graphqlClient = {}; | ||
const mockConfig = {} as unknown as Config; | ||
const mockContext = { | ||
privateProjectConfig: { projectId }, | ||
loggedIn: { graphqlClient }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('deletes a variable by name in non-interactive mode', async () => { | ||
const mockVariables = [ | ||
{ | ||
id: variableId, | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Project, | ||
environments: [EnvironmentVariableEnvironment.Production], | ||
}, | ||
]; | ||
(EnvironmentVariablesQuery.byAppIdAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
|
||
const command = new EnvironmentVariableDelete( | ||
[ | ||
'--variable-name', | ||
'TEST_VARIABLE', | ||
'--variable-environment', | ||
'production', | ||
'--non-interactive', | ||
], | ||
mockConfig | ||
); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.runAsync(); | ||
|
||
expect(EnvironmentVariableMutation.deleteAsync).toHaveBeenCalledWith(graphqlClient, variableId); | ||
expect(Log.withTick).toHaveBeenCalledWith('️Deleted variable TEST_VARIABLE".'); | ||
}); | ||
|
||
it('prompts for variable selection when name is not provided', async () => { | ||
const mockVariables = [ | ||
{ id: variableId, name: 'TEST_VARIABLE', scope: EnvironmentVariableScope.Project }, | ||
]; | ||
(EnvironmentVariablesQuery.byAppIdAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
(promptAsync as jest.Mock).mockResolvedValue({ variable: mockVariables[0] }); | ||
(toggleConfirmAsync as jest.Mock).mockResolvedValue(true); | ||
|
||
const command = new EnvironmentVariableDelete([], mockConfig); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.runAsync(); | ||
|
||
expect(promptAsync).toHaveBeenCalled(); | ||
expect(EnvironmentVariableMutation.deleteAsync).toHaveBeenCalledWith(graphqlClient, variableId); | ||
expect(Log.withTick).toHaveBeenCalledWith('️Deleted variable TEST_VARIABLE".'); | ||
}); | ||
|
||
it('throws an error when variable name is not found', async () => { | ||
const mockVariables = [ | ||
{ id: variableId, name: 'TEST_VARIABLE', scope: EnvironmentVariableScope.Project }, | ||
]; | ||
(EnvironmentVariablesQuery.byAppIdAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
|
||
const command = new EnvironmentVariableDelete( | ||
['--variable-name', 'NON_EXISTENT_VARIABLE'], | ||
mockConfig | ||
); | ||
|
||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await expect(command.runAsync()).rejects.toThrow('Variable "NON_EXISTENT_VARIABLE" not found.'); | ||
}); | ||
|
||
it('throws an error when multiple variables with the same name are found', async () => { | ||
const mockVariables = [ | ||
{ id: variableId, name: 'TEST_VARIABLE', scope: EnvironmentVariableScope.Project }, | ||
{ id: '2', name: 'TEST_VARIABLE', scope: EnvironmentVariableScope.Project }, | ||
]; | ||
(EnvironmentVariablesQuery.byAppIdAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
|
||
const command = new EnvironmentVariableDelete(['--variable-name', 'TEST_VARIABLE'], mockConfig); | ||
|
||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await expect(command.runAsync()).rejects.toThrow( | ||
'Multiple variables with name "TEST_VARIABLE" found. Please select the variable to delete interactively or run command with --variable-environment ENVIRONMENT option.' | ||
); | ||
}); | ||
|
||
it('cancels deletion when user does not confirm', async () => { | ||
const mockVariables = [ | ||
{ id: variableId, name: 'TEST_VARIABLE', scope: EnvironmentVariableScope.Project }, | ||
]; | ||
(EnvironmentVariablesQuery.byAppIdAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
(promptAsync as jest.Mock).mockResolvedValue({ variable: mockVariables[0] }); | ||
(toggleConfirmAsync as jest.Mock).mockResolvedValue(false); | ||
|
||
const command = new EnvironmentVariableDelete(['--non-interactive'], mockConfig); | ||
|
||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await expect(command.runAsync()).rejects.toThrowErrorMatchingSnapshot(); | ||
|
||
expect(EnvironmentVariableMutation.deleteAsync).not.toHaveBeenCalled(); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
...s/eas-cli/src/commands/env/__tests__/__snapshots__/EnvironmentVariableDelete.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`EnvironmentVariableDelete cancels deletion when user does not confirm 1`] = `"Environment variable needs 'name' to be specified when running in non-interactive mode. Run the command with [1m--variable-name VARIABLE_NAME[22m flag to fix the issue"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters