-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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,144 @@ | ||
const add = require('../../commands/codegen/add'); | ||
const codeGen = require('../../src/index'); | ||
|
||
jest.mock('../../src/index'); | ||
|
||
describe('cli - add', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('feature name', () => { | ||
expect(add.name).toEqual('add'); | ||
}); | ||
|
||
describe('run', () => { | ||
test('executes codegen add', async () => { | ||
const context = { | ||
parameters: {}, | ||
}; | ||
await add.run(context); | ||
expect(codeGen.add).toHaveBeenCalledWith(context, null, undefined); | ||
}); | ||
|
||
test('catches error in codegen add', async () => { | ||
const error = jest.fn(); | ||
const context = { | ||
parameters: {}, | ||
print: { | ||
error, | ||
}, | ||
}; | ||
|
||
const codegenError = new Error('failed to read file'); | ||
codeGen.add.mockRejectedValueOnce(codegenError); | ||
await add.run(context); | ||
expect(error).toHaveBeenCalledWith(codegenError.message); | ||
}); | ||
|
||
test('passes apiId', async () => { | ||
const apiId = 'apiid'; | ||
const context = { | ||
parameters: { | ||
options: { | ||
apiId, | ||
}, | ||
}, | ||
}; | ||
await add.run(context); | ||
expect(codeGen.add).toHaveBeenCalledWith(context, apiId, undefined); | ||
}); | ||
|
||
test('passes region', async () => { | ||
const region = 'region'; | ||
const context = { | ||
parameters: { | ||
options: { | ||
region, | ||
}, | ||
}, | ||
}; | ||
await add.run(context); | ||
expect(codeGen.add).toHaveBeenCalledWith(context, null, region); | ||
}); | ||
|
||
test('throws error on invalid arg', async () => { | ||
const badArg = 'badArg'; | ||
const info = jest.fn(); | ||
const context = { | ||
parameters: { | ||
options: { | ||
badArg, | ||
}, | ||
}, | ||
print: { | ||
info, | ||
}, | ||
}; | ||
await add.run(context); | ||
expect(info).toHaveBeenCalledWith('Invalid parameter badArg'); | ||
|
||
expect(info).toHaveBeenCalledWith( | ||
'amplify codegen add takes only apiId and region as parameters. \n$ amplify codegen add [--apiId <API_ID>] [--region <region>]', | ||
); | ||
}); | ||
|
||
test('throws error on invalid args', async () => { | ||
const badArgOne = 'badArgOne'; | ||
const badArgTwo = 'badArgTwo'; | ||
const info = jest.fn(); | ||
const context = { | ||
parameters: { | ||
options: { | ||
badArgOne, | ||
badArgTwo, | ||
}, | ||
}, | ||
print: { | ||
info, | ||
}, | ||
}; | ||
await add.run(context); | ||
expect(info).toHaveBeenCalledWith('Invalid parameters badArgOne, badArgTwo'); | ||
|
||
expect(info).toHaveBeenCalledWith( | ||
'amplify codegen add takes only apiId and region as parameters. \n$ amplify codegen add [--apiId <API_ID>] [--region <region>]', | ||
); | ||
}); | ||
|
||
test('allows undocummented frontend and framework', async () => { | ||
const frontend = 'frontend'; | ||
const framework = 'framework'; | ||
const info = jest.fn(); | ||
const context = { | ||
parameters: { | ||
options: { | ||
frontend, | ||
framework, | ||
}, | ||
}, | ||
print: { | ||
info, | ||
}, | ||
}; | ||
await add.run(context); | ||
expect(info).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('ignores yes arg', async () => { | ||
const yes = true; | ||
const info = jest.fn(); | ||
const context = { | ||
parameters: { | ||
options: { | ||
yes, | ||
}, | ||
}, | ||
print: { | ||
info, | ||
}, | ||
}; | ||
await add.run(context); | ||
}); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/amplify-codegen/tests/commands/__snapshots__/add.test.js.snap
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 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`command - add without init should read frontend and framework from options 1`] = ` | ||
Object { | ||
"amplifyExtension": Object { | ||
"apiId": "MOCK_API_ID", | ||
"codeGenTarget": "TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE", | ||
"docsFilePath": "MOCK_DOCS_FILE_PATH", | ||
"framework": "vue", | ||
"frontend": "javascript", | ||
"generatedFileName": "API.TS", | ||
"region": "us-east-1", | ||
}, | ||
"excludes": "MOCK_EXCLUDE", | ||
"includes": "MOCK_INCLUDE", | ||
"projectName": "Codegen Project", | ||
"schema": "/user/foo/project/schema.graphql", | ||
} | ||
`; | ||
|
||
exports[`command - add without init should use region supplied when without init 1`] = ` | ||
Object { | ||
"amplifyExtension": Object { | ||
"apiId": "MOCK_API_ID", | ||
"codeGenTarget": "TYPE_SCRIPT_OR_FLOW_OR_ANY_OTHER_LANGUAGE", | ||
"docsFilePath": "MOCK_DOCS_FILE_PATH", | ||
"framework": "react", | ||
"frontend": "javascript", | ||
"generatedFileName": "API.TS", | ||
"region": "us-west-2", | ||
}, | ||
"excludes": "MOCK_EXCLUDE", | ||
"includes": "MOCK_INCLUDE", | ||
"projectName": "Codegen Project", | ||
"schema": "/user/foo/project/schema.graphql", | ||
} | ||
`; |
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