Skip to content

Commit

Permalink
Get project templates with API
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriChauvel committed Mar 27, 2024
1 parent e0a79af commit 3c6cf4e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/components/card/TemplateCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class="q-px-none q-py-sm text-caption ellipsis-2-lines"
data-cy="title-container"
>
{{ template.type }}
{{ template.name || template.type }}
</q-card-section>
</q-card>
</template>
Expand Down
6 changes: 3 additions & 3 deletions src/pages/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import ProjectGrid from 'src/components/grid/ProjectGrid.vue';
import TemplateGrid from 'src/components/grid/TemplateGrid.vue';
import { getProjects } from 'src/composables/Project';
import { getTemplatesByType } from 'src/composables/TemplateManager';
import { getProjectTemplates } from 'src/services/TemplatesService';
import ImportProjectDialog from 'components/dialog/ImportProjectDialog.vue';
import CreateProjectTemplateDialog from 'components/dialog/CreateProjectTemplateDialog.vue';
import CreateProjectDialog from 'components/dialog/CreateProjectDialog.vue';
Expand Down Expand Up @@ -73,7 +73,7 @@ async function openCreateProjectTemplateDialog(template) {
onMounted(async () => {
setProjects();
updateProjectSubscription = ProjectEvent.UpdateProjectEvent.subscribe(setProjects);
templates.value = await getTemplatesByType('project');
templates.value = await getProjectTemplates();
});
onUnmounted(() => {
Expand All @@ -90,4 +90,4 @@ onUnmounted(() => {
width: 100%;
}
}
</style>
</style>src/services/TemplatesService
26 changes: 26 additions & 0 deletions src/services/TemplatesService.js
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;
}
63 changes: 63 additions & 0 deletions tests/unit/services/TemplatesService.spec.js
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);
});
});
});

0 comments on commit 3c6cf4e

Please sign in to comment.