forked from aws-amplify/amplify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: ddb walkthrough refactor and override tests (aws-amplify#8364)
* chore: overrides ddb and walthrough refactor tests * chore: address pr comments Co-authored-by: Ghosh <[email protected]>
- Loading branch information
Showing
14 changed files
with
941 additions
and
193 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/amplify-category-storage/src/__tests__/commands/add.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,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, | ||
}); | ||
}); | ||
}); |
92 changes: 92 additions & 0 deletions
92
packages/amplify-category-storage/src/__tests__/commands/override.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,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.'); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/amplify-category-storage/src/__tests__/commands/remove.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,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); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/amplify-category-storage/src/__tests__/commands/update.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,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); | ||
}); | ||
}); |
Oops, something went wrong.