-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
e0a79af
commit 3c6cf4e
Showing
4 changed files
with
93 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { api } from 'boot/axios'; | ||
import { getTemplatesByType } from 'src/composables/TemplateManager'; | ||
|
||
/** | ||
* @param {string} type - project | component | model | ||
* @returns {Promise<object>} | ||
* otherwise an error. | ||
*/ | ||
export async function getAPITemplatesByType(type) { | ||
return api.get(`/libraries/templates?type=${type.toUpperCase().trim()}`); | ||
} | ||
|
||
/** | ||
* Get templates have "project" type. | ||
* @returns {Array} return array of project templates. | ||
*/ | ||
export async function getProjectTemplates() { | ||
if (!process.env.HAS_BACKEND) { | ||
const response = await getTemplatesByType('project'); | ||
return response; | ||
} | ||
|
||
const response = await getAPITemplatesByType('project'); | ||
|
||
return response.content; | ||
} |
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,63 @@ | ||
import { api } from 'src/boot/axios'; | ||
import * as TemplatesService from 'src/services/TemplatesService'; | ||
import { getTemplatesByType } from 'src/composables/TemplateManager'; | ||
|
||
jest.mock('src/boot/axios', () => ({ | ||
api: { | ||
get: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock('src/composables/TemplateManager', () => ({ | ||
getTemplatesByType: jest.fn(), | ||
})); | ||
|
||
describe('Templates Service', () => { | ||
describe('Test function: getApiTemplatesByType', () => { | ||
it('should return a template list', async () => { | ||
const resultGetTemplates = { content: [{ name: 'Project Template', type: 'PROJECT' }] }; | ||
api.get.mockImplementation(() => Promise.resolve(resultGetTemplates)); | ||
|
||
const res = await TemplatesService.getAPITemplatesByType('project'); | ||
|
||
expect(res.content.length).toEqual(1); | ||
expect(res.content[0].name).toEqual('Project Template'); | ||
expect(res.content[0].type).toEqual('PROJECT'); | ||
}); | ||
}); | ||
|
||
describe('Test function: getProjectTemplates', () => { | ||
const OLD_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...OLD_ENV }; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = OLD_ENV; | ||
}); | ||
|
||
it('should return a template list not "using" api', async () => { | ||
jest.resetModules(); | ||
process.env.HAS_BACKEND = false; | ||
const resultGetTemplates = ['test']; | ||
getTemplatesByType.mockImplementation(() => Promise.resolve(resultGetTemplates)); | ||
|
||
const res = await TemplatesService.getProjectTemplates(); | ||
|
||
expect(res).toEqual(resultGetTemplates); | ||
}); | ||
|
||
it('should return a template list "using" api', async () => { | ||
jest.resetModules(); | ||
process.env.HAS_BACKEND = true; | ||
const resultGetTemplates = { content: [{ name: 'Project Template', type: 'PROJECT' }] }; | ||
api.get.mockImplementation(() => Promise.resolve(resultGetTemplates)); | ||
|
||
const res = await TemplatesService.getProjectTemplates(); | ||
|
||
expect(res).toEqual(resultGetTemplates.content); | ||
}); | ||
}); | ||
}); |