import { beforeEach, describe, expect, it, vi } from 'vitest'; import axios from 'axios'; import dayjs from 'dayjs'; import { DATE_TIME_FORMAT } from '@/shared/composables/date-format'; import { ExamAttempt } from '@/shared/model/exam-attempt.model'; import ExamAttemptService from './exam-attempt.service'; const error = { response: { status: null, data: { type: null, }, }, }; const axiosStub = { get: vi.spyOn(axios, 'get'), post: vi.spyOn(axios, 'post'), put: vi.spyOn(axios, 'put'), patch: vi.spyOn(axios, 'patch'), delete: vi.spyOn(axios, 'delete'), }; describe('Service Tests', () => { describe('ExamAttempt Service', () => { let service: ExamAttemptService; let elemDefault; let currentDate: Date; beforeEach(() => { service = new ExamAttemptService(); currentDate = new Date(); elemDefault = new ExamAttempt(123, currentDate, currentDate, 'IN_PROGRESS', 0); }); describe('Service methods', () => { it('should find an element', async () => { const returnedFromService = { startedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), submittedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), ...elemDefault, }; axiosStub.get.mockResolvedValue({ data: returnedFromService }); return service.find(123).then(res => { expect(res).toMatchObject(elemDefault); }); }); it('should not find an element', async () => { axiosStub.get.mockRejectedValue(error); return service .find(123) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should create a ExamAttempt', async () => { const returnedFromService = { id: 123, startedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), submittedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), ...elemDefault, }; const expected = { startedAt: currentDate, submittedAt: currentDate, ...returnedFromService }; axiosStub.post.mockResolvedValue({ data: returnedFromService }); return service.create({}).then(res => { expect(res).toMatchObject(expected); }); }); it('should not create a ExamAttempt', async () => { axiosStub.post.mockRejectedValue(error); return service .create({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should update a ExamAttempt', async () => { const returnedFromService = { startedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), submittedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), status: 'BBBBBB', score: 1, ...elemDefault, }; const expected = { startedAt: currentDate, submittedAt: currentDate, ...returnedFromService }; axiosStub.put.mockResolvedValue({ data: returnedFromService }); return service.update(expected).then(res => { expect(res).toMatchObject(expected); }); }); it('should not update a ExamAttempt', async () => { axiosStub.put.mockRejectedValue(error); return service .update({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should partial update a ExamAttempt', async () => { const patchObject = { status: 'BBBBBB', score: 1, ...new ExamAttempt() }; const returnedFromService = Object.assign(patchObject, elemDefault); const expected = { startedAt: currentDate, submittedAt: currentDate, ...returnedFromService }; axiosStub.patch.mockResolvedValue({ data: returnedFromService }); return service.partialUpdate(patchObject).then(res => { expect(res).toMatchObject(expected); }); }); it('should not partial update a ExamAttempt', async () => { axiosStub.patch.mockRejectedValue(error); return service .partialUpdate({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should return a list of ExamAttempt', async () => { const returnedFromService = { startedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), submittedAt: dayjs(currentDate).format(DATE_TIME_FORMAT), status: 'BBBBBB', score: 1, ...elemDefault, }; const expected = { startedAt: currentDate, submittedAt: currentDate, ...returnedFromService }; axiosStub.get.mockResolvedValue([returnedFromService]); return service.retrieve({ sort: {}, page: 0, size: 10 }).then(res => { expect(res).toContainEqual(expected); }); }); it('should not return a list of ExamAttempt', async () => { axiosStub.get.mockRejectedValue(error); return service .retrieve() .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should delete a ExamAttempt', async () => { axiosStub.delete.mockResolvedValue({ ok: true }); return service.delete(123).then(res => { expect(res.ok).toBeTruthy(); }); }); it('should not delete a ExamAttempt', async () => { axiosStub.delete.mockRejectedValue(error); return service .delete(123) .then() .catch(err => { expect(err).toMatchObject(error); }); }); }); }); });