Skip to content
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

chore: ddb walkthrough refactor and override tests #8364

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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