-
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 84d31d7
Showing
4 changed files
with
80 additions
and
3 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,24 @@ | ||
import { api } from 'boot/axios'; | ||
import { getTemplatesByType } from 'src/composables/TemplateManager'; | ||
|
||
/** | ||
* Filter and return only API templates related to a provided type. | ||
* @param {string} type - Type of template you want to get. PROJECT | COMPONENT | MODEL | ||
* @returns {Promise<object>} | ||
* otherwise an error. | ||
*/ | ||
export async function getAPITemplatesByType(type) { | ||
return api.get(`/libraries/templates?type=${type}`).then((data) => data.content); | ||
} | ||
|
||
/** | ||
* Get "project" templates. | ||
* @returns {Array} return array of project templates. | ||
*/ | ||
export async function getProjectTemplates() { | ||
if (!process.env.HAS_BACKEND) { | ||
return getTemplatesByType('project'); | ||
} | ||
|
||
return getAPITemplatesByType('PROJECT'); | ||
} |
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,53 @@ | ||
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.length).toEqual(1); | ||
expect(res[0].name).toEqual('Project Template'); | ||
expect(res[0].type).toEqual('PROJECT'); | ||
}); | ||
}); | ||
|
||
describe('Test function: getProjectTemplates', () => { | ||
it('should return a template list without calling api', async () => { | ||
delete process.env.HAS_BACKEND; | ||
const resultGetTemplates = ['test']; | ||
getTemplatesByType.mockImplementation(() => Promise.resolve(resultGetTemplates)); | ||
|
||
const res = await TemplatesService.getProjectTemplates(); | ||
|
||
expect(res).toEqual(resultGetTemplates); | ||
}); | ||
|
||
it('should return a template list calling api', async () => { | ||
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.length).toEqual(1); | ||
expect(res[0].name).toEqual('Project Template'); | ||
expect(res[0].type).toEqual('PROJECT'); | ||
delete process.env.HAS_BACKEND; | ||
}); | ||
}); | ||
}); |