Skip to content

Commit

Permalink
feat(amplify-category-function): skip unnecessary prompt for 'amplify…
Browse files Browse the repository at this point in the history
… update function'

* feat(amplify-category-function): skip unnecessary prompt for 'amplify update function'

* test: remove additional prompt from e2e
  • Loading branch information
johnpc authored Jul 21, 2021
1 parent ea3d3cb commit 12872a4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { determineServiceSelection } from '../../../../provider-utils/awscloudformation/utils/determineServiceSelection';
import { ServiceName } from '../../../../provider-utils/awscloudformation/utils/constants';

const serviceSelectionPromptMock = jest.fn();
const mockChooseServiceMessage = 'mockChooseServiceMessage';
const mockContext = {
amplify: {
getResourceStatus: async () => {
return { allResources: [] };
},
serviceSelectionPrompt: serviceSelectionPromptMock,
},
};

describe('determineServiceSelection', () => {
it('returns LambdaFunction when no resources exists', async () => {
const response = await determineServiceSelection(mockContext, mockChooseServiceMessage);
expect(response.service === ServiceName.LambdaFunction);
expect(serviceSelectionPromptMock).toBeCalledTimes(0);
});

it('returns LambdaFunction when only LambdaFunction resources exists', async () => {
mockContext.amplify.getResourceStatus = async () => {
return {
allResources: [
{
service: ServiceName.LambdaFunction,
},
],
};
};
const response = await determineServiceSelection(mockContext, mockChooseServiceMessage);
expect(response.service === ServiceName.LambdaFunction);
expect(serviceSelectionPromptMock).toBeCalledTimes(0);
});

it('returns LambdaLayer when only LambdaLayer resources exists', async () => {
mockContext.amplify.getResourceStatus = async () => {
return {
allResources: [
{
service: ServiceName.LambdaLayer,
},
],
};
};
const response = await determineServiceSelection(mockContext, mockChooseServiceMessage);
expect(response.service === ServiceName.LambdaLayer);
expect(serviceSelectionPromptMock).toBeCalledTimes(0);
});

it('returns LambdaLayer when existing LambdaFunction resources have mobileHubMigrated', async () => {
mockContext.amplify.getResourceStatus = async () => {
return {
allResources: [
{
service: ServiceName.LambdaLayer,
},
{
service: ServiceName.LambdaFunction,
mobileHubMigrated: true,
},
],
};
};
const response = await determineServiceSelection(mockContext, mockChooseServiceMessage);
expect(response.service === ServiceName.LambdaLayer);
expect(serviceSelectionPromptMock).toBeCalledTimes(0);
});

it('prompts for user input when both LambdaFunction and LambdaLayer resources exist', async () => {
mockContext.amplify.getResourceStatus = async () => {
return {
allResources: [
{
service: ServiceName.LambdaFunction,
},
{
service: ServiceName.LambdaLayer,
},
],
};
};
await determineServiceSelection(mockContext, mockChooseServiceMessage);
expect(serviceSelectionPromptMock).toBeCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { supportedServices } from '../../provider-utils/supported-services';
import { chooseServiceMessageUpdate } from '../../provider-utils/awscloudformation/utils/constants';
import { categoryName } from '../../constants';
import { determineServiceSelection } from '../../provider-utils/awscloudformation/utils/determineServiceSelection';

const subcommand = 'update';

module.exports = {
name: subcommand,
alias: ['configure'],
run: async context => {
const { amplify } = context;
const servicesMetadata = supportedServices;
return amplify
.serviceSelectionPrompt(context, categoryName, servicesMetadata, chooseServiceMessageUpdate)
return determineServiceSelection(context, chooseServiceMessageUpdate)
.then(result => {
const providerController = servicesMetadata[result.service].providerController;
if (!providerController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { ServiceName } from '../utils/constants';
export async function removeResource(resourceName?: string): Promise<$TSAny> {
const enabledCategoryResources = getEnabledResources();

if (enabledCategoryResources.length === 0) {
throw new Error('No Lambda function resource to remove. Use "amplify add function" to create a new function.');
}

if (resourceName) {
const resource = enabledCategoryResources.find(categoryResource => categoryResource.value.resourceName === resourceName);
return resource.value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ServiceName } from './constants';
import { categoryName } from '../../../constants';
import { supportedServices } from '../../supported-services';

export const determineServiceSelection = async (context, chooseServiceMessage) => {
const { allResources } = await context.amplify.getResourceStatus();
const lambdaLayerExists = allResources.filter(resource => resource.service === ServiceName.LambdaLayer).length > 0;
const lambdaFunctionExists =
allResources.filter(resource => resource.service === ServiceName.LambdaFunction && resource.mobileHubMigrated !== true).length > 0;

if ((!lambdaFunctionExists && !lambdaLayerExists) || (lambdaFunctionExists && !lambdaLayerExists)) {
return {
service: ServiceName.LambdaFunction,
};
}

if (!lambdaFunctionExists && lambdaLayerExists) {
return {
service: ServiceName.LambdaLayer,
};
}

return await context.amplify.serviceSelectionPrompt(context, categoryName, supportedServices, chooseServiceMessage);
};
6 changes: 1 addition & 5 deletions packages/amplify-e2e-core/src/categories/lambda-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ const coreFunction = (
selectTemplate(chain, settings.functionTemplate, runtime);
}
} else {
chain
.wait('Select which capability you want to update:')
.sendCarriageReturn() // lambda function
.wait('Select the Lambda function you want to update')
.sendCarriageReturn(); // assumes only one function configured in the project
chain.wait('Select the Lambda function you want to update').sendCarriageReturn(); // assumes only one function configured in the project
}

if (functionConfigCallback) {
Expand Down

0 comments on commit 12872a4

Please sign in to comment.