import { beforeEach, describe, expect, it, vi } from 'vitest'; import axios from 'axios'; import { Choice } from '@/shared/model/choice.model'; import ChoiceService from './choice.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('Choice Service', () => { let service: ChoiceService; let elemDefault; beforeEach(() => { service = new ChoiceService(); elemDefault = new Choice(123, 'A', 'AAAAAAA', 0, false); }); describe('Service methods', () => { it('should find an element', async () => { const returnedFromService = { ...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 Choice', async () => { const returnedFromService = { id: 123, ...elemDefault }; const expected = { ...returnedFromService }; axiosStub.post.mockResolvedValue({ data: returnedFromService }); return service.create({}).then(res => { expect(res).toMatchObject(expected); }); }); it('should not create a Choice', async () => { axiosStub.post.mockRejectedValue(error); return service .create({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should update a Choice', async () => { const returnedFromService = { optionKey: 'BBBBBB', content: 'BBBBBB', displayOrder: 1, correct: true, ...elemDefault }; const expected = { ...returnedFromService }; axiosStub.put.mockResolvedValue({ data: returnedFromService }); return service.update(expected).then(res => { expect(res).toMatchObject(expected); }); }); it('should not update a Choice', async () => { axiosStub.put.mockRejectedValue(error); return service .update({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should partial update a Choice', async () => { const patchObject = { content: 'BBBBBB', ...new Choice() }; const returnedFromService = Object.assign(patchObject, elemDefault); const expected = { ...returnedFromService }; axiosStub.patch.mockResolvedValue({ data: returnedFromService }); return service.partialUpdate(patchObject).then(res => { expect(res).toMatchObject(expected); }); }); it('should not partial update a Choice', async () => { axiosStub.patch.mockRejectedValue(error); return service .partialUpdate({}) .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should return a list of Choice', async () => { const returnedFromService = { optionKey: 'BBBBBB', content: 'BBBBBB', displayOrder: 1, correct: true, ...elemDefault }; const expected = { ...returnedFromService }; axiosStub.get.mockResolvedValue([returnedFromService]); return service.retrieve().then(res => { expect(res).toContainEqual(expected); }); }); it('should not return a list of Choice', async () => { axiosStub.get.mockRejectedValue(error); return service .retrieve() .then() .catch(err => { expect(err).toMatchObject(error); }); }); it('should delete a Choice', async () => { axiosStub.delete.mockResolvedValue({ ok: true }); return service.delete(123).then(res => { expect(res.ok).toBeTruthy(); }); }); it('should not delete a Choice', async () => { axiosStub.delete.mockRejectedValue(error); return service .delete(123) .then() .catch(err => { expect(err).toMatchObject(error); }); }); }); }); });