168 lines
4.7 KiB
TypeScript
168 lines
4.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { type MountingOptions, shallowMount } from '@vue/test-utils';
|
|
|
|
import AlertService from '@/shared/alert/alert.service';
|
|
|
|
import Question from './question.vue';
|
|
|
|
type QuestionComponentType = InstanceType<typeof Question>;
|
|
|
|
const bModalStub = {
|
|
render: () => {},
|
|
methods: {
|
|
hide: () => {},
|
|
show: () => {},
|
|
},
|
|
};
|
|
|
|
describe('Component Tests', () => {
|
|
let alertService: AlertService;
|
|
|
|
describe('Question Management Component', () => {
|
|
let questionServiceStub: any;
|
|
let mountOptions: MountingOptions<QuestionComponentType>['global'];
|
|
|
|
beforeEach(() => {
|
|
questionServiceStub = {
|
|
retrieve: vi.fn(),
|
|
delete: vi.fn(),
|
|
};
|
|
questionServiceStub.retrieve.mockResolvedValue({ headers: {} });
|
|
|
|
alertService = new AlertService({
|
|
i18n: { t: vi.fn() } as any,
|
|
toast: {
|
|
create: vi.fn(),
|
|
} as any,
|
|
});
|
|
|
|
mountOptions = {
|
|
stubs: {
|
|
jhiItemCount: true,
|
|
bPagination: true,
|
|
bModal: bModalStub as any,
|
|
'font-awesome-icon': true,
|
|
'b-badge': true,
|
|
'jhi-sort-indicator': true,
|
|
'b-button': true,
|
|
'router-link': true,
|
|
},
|
|
directives: {
|
|
'b-modal': {},
|
|
},
|
|
provide: {
|
|
alertService,
|
|
questionService: () => questionServiceStub,
|
|
},
|
|
};
|
|
});
|
|
|
|
describe('Mount', () => {
|
|
it('Should call load all on init', async () => {
|
|
// GIVEN
|
|
questionServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [{ id: 123 }] });
|
|
|
|
// WHEN
|
|
const wrapper = shallowMount(Question, { global: mountOptions });
|
|
const comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(questionServiceStub.retrieve).toHaveBeenCalledOnce();
|
|
expect(comp.questions[0]).toEqual(expect.objectContaining({ id: 123 }));
|
|
});
|
|
|
|
it('should calculate the sort attribute for an id', async () => {
|
|
// WHEN
|
|
const wrapper = shallowMount(Question, { global: mountOptions });
|
|
const comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(questionServiceStub.retrieve.mock.lastCall?.[0]).toMatchObject({
|
|
sort: ['id,asc'],
|
|
});
|
|
});
|
|
});
|
|
describe('Handles', () => {
|
|
let comp: QuestionComponentType;
|
|
|
|
beforeEach(async () => {
|
|
const wrapper = shallowMount(Question, { global: mountOptions });
|
|
comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
questionServiceStub.retrieve.mockReset();
|
|
questionServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [] });
|
|
});
|
|
|
|
it('should load a page', async () => {
|
|
// GIVEN
|
|
questionServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [{ id: 123 }] });
|
|
|
|
// WHEN
|
|
comp.page = 2;
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(questionServiceStub.retrieve).toHaveBeenCalled();
|
|
expect(comp.questions[0]).toEqual(expect.objectContaining({ id: 123 }));
|
|
});
|
|
|
|
it('should not load a page if the page is the same as the previous page', () => {
|
|
// WHEN
|
|
comp.page = 1;
|
|
|
|
// THEN
|
|
expect(questionServiceStub.retrieve).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should re-initialize the page', async () => {
|
|
// GIVEN
|
|
comp.page = 2;
|
|
await comp.$nextTick();
|
|
questionServiceStub.retrieve.mockReset();
|
|
questionServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [{ id: 123 }] });
|
|
|
|
// WHEN
|
|
comp.clear();
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(comp.page).toEqual(1);
|
|
expect(questionServiceStub.retrieve).toHaveBeenCalledTimes(1);
|
|
expect(comp.questions[0]).toEqual(expect.objectContaining({ id: 123 }));
|
|
});
|
|
|
|
it('should calculate the sort attribute for a non-id attribute', async () => {
|
|
// WHEN
|
|
comp.propOrder = 'name';
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(questionServiceStub.retrieve.mock.lastCall?.[0]).toMatchObject({
|
|
sort: ['name,asc', 'id'],
|
|
});
|
|
});
|
|
|
|
it('Should call delete service on confirmDelete', async () => {
|
|
// GIVEN
|
|
questionServiceStub.delete.mockResolvedValue({});
|
|
|
|
// WHEN
|
|
comp.prepareRemove({ id: 123 });
|
|
|
|
comp.removeQuestion();
|
|
await comp.$nextTick(); // clear components
|
|
|
|
// THEN
|
|
expect(questionServiceStub.delete).toHaveBeenCalled();
|
|
|
|
// THEN
|
|
await comp.$nextTick(); // handle component clear watch
|
|
expect(questionServiceStub.retrieve).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
});
|