-
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.
Refactor env:unlink, add tests (#2591)
<!-- If this PR requires a changelog entry, add it by commenting the PR with the command `/changelog-entry [breaking-change|new-feature|bug-fix|chore] [message]`. --> <!-- You can skip the changelog check by labeling the PR with "no changelog". --> # Why [ENG-13525: Update EAS-CLI for new EnvVar features](https://linear.app/expo/issue/ENG-13525/update-eas-cli-for-new-envvar-features) Update command to use multiple environments, polish the interface. # How * use `variable-name` and `variable-environment` to select the variable * Use `multiselect` and multiple `environment` parameters to allow unlinking multiple envs All commands that were using name parameter to identify a single variable will use variable-name and variable-environment instead. This change will enable user to distinguish between name and environment that is identifying variable that is acted upon, and name and environment that is a parameter for command. For example, when the user wants to update variable TEST that has environment production and change its name and change its environment, they can use: eas env:update --variable-name TEST --variable-environment production --name TEST-update --environment production --environment preview # Test Plan Added tests
- Loading branch information
1 parent
af16cba
commit 3f83276
Showing
2 changed files
with
323 additions
and
41 deletions.
There are no files selected for viewing
232 changes: 232 additions & 0 deletions
232
packages/eas-cli/src/commands/env/__tests__/EnvironmentVariableUnlink.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,232 @@ | ||
import { Config } from '@oclif/core'; | ||
import chalk from 'chalk'; | ||
|
||
import { getMockAppFragment } from '../../../__tests__/commands/utils'; | ||
import { | ||
EnvironmentVariableEnvironment, | ||
EnvironmentVariableScope, | ||
} from '../../../graphql/generated'; | ||
import { EnvironmentVariableMutation } from '../../../graphql/mutations/EnvironmentVariableMutation'; | ||
import { AppQuery } from '../../../graphql/queries/AppQuery'; | ||
import { EnvironmentVariablesQuery } from '../../../graphql/queries/EnvironmentVariablesQuery'; | ||
import Log from '../../../log'; | ||
import { promptAsync, selectAsync, toggleConfirmAsync } from '../../../prompts'; | ||
import EnvironmentVariableUnlink from '../unlink'; | ||
|
||
jest.mock('../../../graphql/queries/EnvironmentVariablesQuery'); | ||
jest.mock('../../../graphql/mutations/EnvironmentVariableMutation'); | ||
jest.mock('../../../prompts'); | ||
jest.mock('../../../graphql/queries/AppQuery'); | ||
jest.mock('../../../log'); | ||
|
||
describe(EnvironmentVariableUnlink, () => { | ||
const projectId = 'test-project-id'; | ||
const variableId = '1'; | ||
const graphqlClient = {}; | ||
const mockConfig = {} as unknown as Config; | ||
const mockContext = { | ||
privateProjectConfig: { projectId }, | ||
loggedIn: { graphqlClient }, | ||
}; | ||
|
||
const successMessage = (env: EnvironmentVariableEnvironment): string => | ||
`Unlinked variable ${chalk.bold('TEST_VARIABLE')} from project ${chalk.bold( | ||
'@testuser/testpp' | ||
)} in ${env.toLocaleLowerCase()}.`; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
jest.mocked(AppQuery.byIdAsync).mockImplementation(async () => getMockAppFragment()); | ||
}); | ||
|
||
it('unlinks a shared variable from the current project in non-interactive mode', async () => { | ||
const mockVariables = [ | ||
{ | ||
id: variableId, | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Shared, | ||
environments: [EnvironmentVariableEnvironment.Development], | ||
linkedEnvironments: [EnvironmentVariableEnvironment.Development], | ||
}, | ||
]; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
( | ||
EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync as jest.Mock | ||
).mockResolvedValue(mockVariables[0]); | ||
|
||
const command = new EnvironmentVariableUnlink( | ||
['--variable-name', 'TEST_VARIABLE', '--non-interactive'], | ||
mockConfig | ||
); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.run(); | ||
|
||
expect(EnvironmentVariablesQuery.sharedAsync).toHaveBeenCalledWith(graphqlClient, { | ||
appId: projectId, | ||
filterNames: ['TEST_VARIABLE'], | ||
}); | ||
expect(EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync).toHaveBeenCalledWith( | ||
graphqlClient, | ||
variableId, | ||
projectId | ||
); | ||
expect(Log.withTick).toHaveBeenCalledWith( | ||
successMessage(EnvironmentVariableEnvironment.Development) | ||
); | ||
}); | ||
|
||
it('unlinks a shared variable from the current project in a specified environment', async () => { | ||
const mockVariables = [ | ||
{ | ||
id: variableId, | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Shared, | ||
environments: [EnvironmentVariableEnvironment.Development], | ||
linkedEnvironments: [EnvironmentVariableEnvironment.Production], | ||
}, | ||
]; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
( | ||
EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync as jest.Mock | ||
).mockResolvedValue(mockVariables[0]); | ||
|
||
const command = new EnvironmentVariableUnlink( | ||
['--variable-name', 'TEST_VARIABLE', '--environment', 'production', '--non-interactive'], | ||
mockConfig | ||
); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.run(); | ||
|
||
expect(Log.withTick).toHaveBeenCalledWith( | ||
successMessage(EnvironmentVariableEnvironment.Production) | ||
); | ||
expect(EnvironmentVariablesQuery.sharedAsync).toHaveBeenCalledWith(graphqlClient, { | ||
appId: projectId, | ||
filterNames: ['TEST_VARIABLE'], | ||
}); | ||
expect(EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync).toHaveBeenCalledWith( | ||
graphqlClient, | ||
variableId, | ||
projectId, | ||
EnvironmentVariableEnvironment.Production | ||
); | ||
}); | ||
|
||
it('prompts for variable selection when the name is ambigous', async () => { | ||
const mockVariables = [ | ||
{ | ||
id: variableId, | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Shared, | ||
environments: [EnvironmentVariableEnvironment.Preview], | ||
linkedEnvironments: [EnvironmentVariableEnvironment.Preview], | ||
}, | ||
{ | ||
id: 'other-id', | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Shared, | ||
environments: [EnvironmentVariableEnvironment.Development], | ||
linkedEnvironments: [EnvironmentVariableEnvironment.Development], | ||
}, | ||
]; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
( | ||
EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync as jest.Mock | ||
).mockResolvedValue(mockVariables[0]); | ||
(selectAsync as jest.Mock).mockResolvedValue(mockVariables[0]); | ||
(promptAsync as jest.Mock).mockResolvedValue({ | ||
environments: [], | ||
}); | ||
(toggleConfirmAsync as jest.Mock).mockResolvedValue(true); | ||
|
||
const command = new EnvironmentVariableUnlink([], mockConfig); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.runAsync(); | ||
|
||
expect(selectAsync).toHaveBeenCalled(); | ||
expect(EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync).toHaveBeenCalledWith( | ||
graphqlClient, | ||
variableId, | ||
projectId, | ||
EnvironmentVariableEnvironment.Preview | ||
); | ||
expect(Log.withTick).toHaveBeenCalledWith( | ||
successMessage(EnvironmentVariableEnvironment.Preview) | ||
); | ||
}); | ||
|
||
it('throws an error when variable name is not found', async () => { | ||
const mockVariables: never[] = []; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
|
||
const command = new EnvironmentVariableUnlink( | ||
['--variable-name', 'NON_EXISTENT_VARIABLE'], | ||
mockConfig | ||
); | ||
|
||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await expect(command.runAsync()).rejects.toThrow( | ||
"Shared variable NON_EXISTENT_VARIABLE doesn't exist" | ||
); | ||
}); | ||
|
||
it('uses environments from prompt to both link and unlink environments', async () => { | ||
const mockVariables = [ | ||
{ | ||
id: variableId, | ||
name: 'TEST_VARIABLE', | ||
scope: EnvironmentVariableScope.Shared, | ||
environments: [EnvironmentVariableEnvironment.Preview], | ||
linkedEnvironments: [EnvironmentVariableEnvironment.Preview], | ||
}, | ||
]; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
(EnvironmentVariableMutation.linkSharedEnvironmentVariableAsync as jest.Mock).mockResolvedValue( | ||
mockVariables[0] | ||
); | ||
(selectAsync as jest.Mock).mockResolvedValue(mockVariables[0]); | ||
(promptAsync as jest.Mock).mockResolvedValue({ | ||
environments: [EnvironmentVariableEnvironment.Production], | ||
}); | ||
(toggleConfirmAsync as jest.Mock).mockResolvedValue(true); | ||
|
||
const command = new EnvironmentVariableUnlink([], mockConfig); | ||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await command.runAsync(); | ||
|
||
expect(promptAsync).toHaveBeenCalled(); | ||
expect(EnvironmentVariableMutation.linkSharedEnvironmentVariableAsync).toHaveBeenCalledWith( | ||
graphqlClient, | ||
variableId, | ||
projectId, | ||
EnvironmentVariableEnvironment.Production | ||
); | ||
expect(EnvironmentVariableMutation.unlinkSharedEnvironmentVariableAsync).toHaveBeenCalledWith( | ||
graphqlClient, | ||
variableId, | ||
projectId, | ||
EnvironmentVariableEnvironment.Preview | ||
); | ||
}); | ||
|
||
it('throws an error when variable name is not found', async () => { | ||
const mockVariables: never[] = []; | ||
(EnvironmentVariablesQuery.sharedAsync as jest.Mock).mockResolvedValue(mockVariables); | ||
|
||
const command = new EnvironmentVariableUnlink( | ||
['--variable-name', 'NON_EXISTENT_VARIABLE'], | ||
mockConfig | ||
); | ||
|
||
// @ts-expect-error | ||
jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); | ||
await expect(command.runAsync()).rejects.toThrow( | ||
"Shared variable NON_EXISTENT_VARIABLE doesn't exist" | ||
); | ||
}); | ||
}); |
Oops, something went wrong.