104 lines
2.8 KiB
TypeScript
104 lines
2.8 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 ExamAnswer from './exam-answer.vue';
|
|
|
|
type ExamAnswerComponentType = InstanceType<typeof ExamAnswer>;
|
|
|
|
const bModalStub = {
|
|
render: () => {},
|
|
methods: {
|
|
hide: () => {},
|
|
show: () => {},
|
|
},
|
|
};
|
|
|
|
describe('Component Tests', () => {
|
|
let alertService: AlertService;
|
|
|
|
describe('ExamAnswer Management Component', () => {
|
|
let examAnswerServiceStub: any;
|
|
let mountOptions: MountingOptions<ExamAnswerComponentType>['global'];
|
|
|
|
beforeEach(() => {
|
|
examAnswerServiceStub = {
|
|
retrieve: vi.fn(),
|
|
delete: vi.fn(),
|
|
};
|
|
examAnswerServiceStub.retrieve.mockResolvedValue({ headers: {} });
|
|
|
|
alertService = new AlertService({
|
|
i18n: { t: vi.fn() } as any,
|
|
toast: {
|
|
create: vi.fn(),
|
|
} as any,
|
|
});
|
|
|
|
mountOptions = {
|
|
stubs: {
|
|
bModal: bModalStub as any,
|
|
'font-awesome-icon': true,
|
|
'b-badge': true,
|
|
'b-button': true,
|
|
'router-link': true,
|
|
},
|
|
directives: {
|
|
'b-modal': {},
|
|
},
|
|
provide: {
|
|
alertService,
|
|
examAnswerService: () => examAnswerServiceStub,
|
|
},
|
|
};
|
|
});
|
|
|
|
describe('Mount', () => {
|
|
it('Should call load all on init', async () => {
|
|
// GIVEN
|
|
examAnswerServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [{ id: 123 }] });
|
|
|
|
// WHEN
|
|
const wrapper = shallowMount(ExamAnswer, { global: mountOptions });
|
|
const comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
|
|
// THEN
|
|
expect(examAnswerServiceStub.retrieve).toHaveBeenCalledOnce();
|
|
expect(comp.examAnswers[0]).toEqual(expect.objectContaining({ id: 123 }));
|
|
});
|
|
});
|
|
describe('Handles', () => {
|
|
let comp: ExamAnswerComponentType;
|
|
|
|
beforeEach(async () => {
|
|
const wrapper = shallowMount(ExamAnswer, { global: mountOptions });
|
|
comp = wrapper.vm;
|
|
await comp.$nextTick();
|
|
examAnswerServiceStub.retrieve.mockReset();
|
|
examAnswerServiceStub.retrieve.mockResolvedValue({ headers: {}, data: [] });
|
|
});
|
|
|
|
it('Should call delete service on confirmDelete', async () => {
|
|
// GIVEN
|
|
examAnswerServiceStub.delete.mockResolvedValue({});
|
|
|
|
// WHEN
|
|
comp.prepareRemove({ id: 123 });
|
|
|
|
comp.removeExamAnswer();
|
|
await comp.$nextTick(); // clear components
|
|
|
|
// THEN
|
|
expect(examAnswerServiceStub.delete).toHaveBeenCalled();
|
|
|
|
// THEN
|
|
await comp.$nextTick(); // handle component clear watch
|
|
expect(examAnswerServiceStub.retrieve).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|
|
});
|