Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: project templates service #516

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template name is a required string, no need || X

Copy link
Member Author

@DimitriChauvel DimitriChauvel Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When HAS_BACKEND is false the data have actually no name field

</q-card-section>
</q-card>
</template>
Expand Down
4 changes: 2 additions & 2 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 Down
24 changes: 24 additions & 0 deletions src/services/TemplatesService.js
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>}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rework on documentation.
Missing default description, description param are no explain of what is the param
Return have a break line and incomplete description.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROJECT | COMPONENT | MODEL -> , can be 'PROJECT', 'COMPONENT' or 'MODEL'.

* 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');
}
53 changes: 53 additions & 0 deletions tests/unit/services/TemplatesService.spec.js
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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is no like other test file, please check it

Copy link
Member Author

@DimitriChauvel DimitriChauvel Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont understand. I just did like in User Service tests file

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;
});
});
});