Files
jhipster-question-bank/src/main/webapp/app/entities/question/question.service.spec.ts
T

162 lines
4.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import axios from 'axios';
import { Question } from '@/shared/model/question.model';
import QuestionService from './question.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('Question Service', () => {
let service: QuestionService;
let elemDefault;
beforeEach(() => {
service = new QuestionService();
elemDefault = new Question(123, 'AAAAAAA', 'AAAAAAA', 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 Question', 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 Question', async () => {
axiosStub.post.mockRejectedValue(error);
return service
.create({})
.then()
.catch(err => {
expect(err).toMatchObject(error);
});
});
it('should update a Question', async () => {
const returnedFromService = { prompt: 'BBBBBB', explanation: 'BBBBBB', active: 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 Question', async () => {
axiosStub.put.mockRejectedValue(error);
return service
.update({})
.then()
.catch(err => {
expect(err).toMatchObject(error);
});
});
it('should partial update a Question', async () => {
const patchObject = { active: true, ...new Question() };
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 Question', async () => {
axiosStub.patch.mockRejectedValue(error);
return service
.partialUpdate({})
.then()
.catch(err => {
expect(err).toMatchObject(error);
});
});
it('should return a list of Question', async () => {
const returnedFromService = { prompt: 'BBBBBB', explanation: 'BBBBBB', active: true, ...elemDefault };
const expected = { ...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 Question', async () => {
axiosStub.get.mockRejectedValue(error);
return service
.retrieve()
.then()
.catch(err => {
expect(err).toMatchObject(error);
});
});
it('should delete a Question', async () => {
axiosStub.delete.mockResolvedValue({ ok: true });
return service.delete(123).then(res => {
expect(res.ok).toBeTruthy();
});
});
it('should not delete a Question', async () => {
axiosStub.delete.mockRejectedValue(error);
return service
.delete(123)
.then()
.catch(err => {
expect(err).toMatchObject(error);
});
});
});
});
});