Skip to content

Commit

Permalink
chore: ddb walkthrough refactor and override tests (aws-amplify#8364)
Browse files Browse the repository at this point in the history
* chore: overrides ddb and walthrough refactor tests

* chore: address pr comments

Co-authored-by: Ghosh <kaustavg@3c22fb229ff6.ant.amazon.com>
  • Loading branch information
2 people authored and akshbhu committed Oct 27, 2021
1 parent 724c59a commit c12d0d6
Show file tree
Hide file tree
Showing 14 changed files with 941 additions and 193 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { $TSContext, $TSObject } from 'amplify-cli-core';
import { run } from '../../commands/storage/add';
import * as providerController from '../../provider-utils/awscloudformation/index';

jest.mock('../../provider-utils/awscloudformation/index');
jest.mock('amplify-cli-core');

const providerController_mock = providerController as jest.Mocked<typeof providerController>;
providerController_mock.addResource.mockImplementation = jest.fn().mockImplementation(async () => {
return 'mockResourceName';
});

describe('add ddb command tests', () => {
const provider = 'awscloudformation';
let mockContext: $TSContext;

beforeEach(() => {
jest.clearAllMocks();
mockContext = {
amplify: {},
} as unknown as $TSContext;
});

it('add resource workflow is invoked for DDB', async () => {
const service = 'DynamoDB';
mockContext.amplify.serviceSelectionPrompt = jest.fn().mockImplementation(async () => {
return { service: service, providerName: provider };
});

await run(mockContext);

expect(providerController_mock.addResource).toHaveBeenCalledWith(mockContext, 'storage', service, {
service: service,
providerPlugin: provider,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { $TSContext, $TSObject, stateManager, generateOverrideSkeleton, pathManager } from 'amplify-cli-core';
import { run } from '../../commands/storage/override';
import { printer, prompter } from 'amplify-prompts';
import path from 'path';
import { DynamoDBInputState } from '../../provider-utils/awscloudformation/service-walkthroughs/dynamoDB-input-state';

jest.mock('amplify-cli-core');
jest.mock('amplify-prompts');
jest.mock('path');
jest.mock('../../provider-utils/awscloudformation/service-walkthroughs/dynamoDB-input-state');
jest.mock('../../provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform');

const generateOverrideSkeleton_mock = generateOverrideSkeleton as jest.MockedFunction<typeof generateOverrideSkeleton>;
generateOverrideSkeleton_mock.mockImplementation = jest.fn().mockImplementation(async () => {
return 'mockResourceName';
});

describe('override ddb command tests', () => {
let mockContext: $TSContext;
let mockAmplifyMeta: $TSObject = {};

beforeEach(() => {
jest.clearAllMocks();
mockContext = {
amplify: {},
} as unknown as $TSContext;
});

it('override ddb when two ddb storage resources present', async () => {
mockAmplifyMeta = {
storage: {
dynamo73399689: {
service: 'DynamoDB',
providerPlugin: 'awscloudformation',
},
dynamoefb50875: {
service: 'DynamoDB',
providerPlugin: 'awscloudformation',
},
},
};

const destDir = 'mockDir';
const srcDir = 'mockSrcDir';

stateManager.getMeta = jest.fn().mockReturnValue(mockAmplifyMeta);
pathManager.getResourceDirectoryPath = jest.fn().mockReturnValue(destDir);
path.join = jest.fn().mockReturnValue(srcDir);

prompter.pick = jest.fn().mockReturnValue('dynamo73399689');
jest.spyOn(DynamoDBInputState.prototype, 'cliInputFileExists').mockImplementation(() => true);

await run(mockContext);

expect(prompter.pick).toBeCalledTimes(1);
expect(generateOverrideSkeleton).toHaveBeenCalledWith(mockContext, srcDir, destDir);
});

it('override ddb when one ddb storage resource present', async () => {
mockAmplifyMeta = {
storage: {
dynamo73399689: {
service: 'DynamoDB',
providerPlugin: 'awscloudformation',
},
},
};

const destDir = 'mockDir';
const srcDir = 'mockSrcDir';

stateManager.getMeta = jest.fn().mockReturnValue(mockAmplifyMeta);
pathManager.getResourceDirectoryPath = jest.fn().mockReturnValue(destDir);
path.join = jest.fn().mockReturnValue(srcDir);

jest.spyOn(DynamoDBInputState.prototype, 'cliInputFileExists').mockImplementation(() => true);

await run(mockContext);

// Prompter should not be called when only one ddb/storage resource present
expect(prompter.pick).toBeCalledTimes(0);
expect(generateOverrideSkeleton).toHaveBeenCalledWith(mockContext, srcDir, destDir);
});

it('override ddb when no ddb storage resource present', async () => {
mockAmplifyMeta = {};
stateManager.getMeta = jest.fn().mockReturnValue(mockAmplifyMeta);

await run(mockContext);
expect(printer.error).toHaveBeenCalledWith('No resources to override. You need to add a resource.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { $TSContext, $TSObject } from 'amplify-cli-core';
import { run } from '../../commands/storage/remove';
import * as providerController from '../../provider-utils/awscloudformation/index';

jest.mock('../../provider-utils/awscloudformation/index');
jest.mock('amplify-cli-core');

const providerController_mock = providerController as jest.Mocked<typeof providerController>;
providerController_mock.updateResource.mockImplementation = jest.fn().mockImplementation(async () => {
return 'mockResourceName';
});

describe('remove ddb command tests', () => {
const provider = 'awscloudformation';
let mockContext: $TSContext;

beforeEach(() => {
jest.clearAllMocks();
mockContext = {
amplify: {},
parameters: {},
} as unknown as $TSContext;
});

it('update resource workflow is invoked for DDB with no params', async () => {
mockContext.amplify.removeResource = jest.fn().mockImplementation(async () => {
return;
});

await run(mockContext);

expect(mockContext.amplify.removeResource).toHaveBeenCalledWith(mockContext, 'storage', undefined);
});

it('update resource workflow is invoked for DDB with params as resourceName', async () => {
const mockResourceName = 'mockResourceName';
mockContext.parameters.first = mockResourceName;
mockContext.amplify.removeResource = jest.fn().mockImplementation(async () => {
return;
});

await run(mockContext);

expect(mockContext.amplify.removeResource).toHaveBeenCalledWith(mockContext, 'storage', mockResourceName);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { $TSContext, $TSObject } from 'amplify-cli-core';
import { run } from '../../commands/storage/update';
import * as providerController from '../../provider-utils/awscloudformation/index';

jest.mock('../../provider-utils/awscloudformation/index');
jest.mock('amplify-cli-core');

const providerController_mock = providerController as jest.Mocked<typeof providerController>;
providerController_mock.updateResource.mockImplementation = jest.fn().mockImplementation(async () => {
return 'mockResourceName';
});

describe('update ddb command tests', () => {
const provider = 'awscloudformation';
let mockContext: $TSContext;

beforeEach(() => {
jest.clearAllMocks();
mockContext = {
amplify: {},
} as unknown as $TSContext;
});

it('update resource workflow is invoked for DDB', async () => {
const service = 'DynamoDB';
mockContext.amplify.serviceSelectionPrompt = jest.fn().mockImplementation(async () => {
return { service: service, providerName: provider };
});

await run(mockContext);

expect(providerController_mock.updateResource).toHaveBeenCalledWith(mockContext, 'storage', service);
});
});
Loading

0 comments on commit c12d0d6

Please sign in to comment.