87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { SettingsService } from './settings.service';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
describe('SettingsService', () => {
|
|
let service: SettingsService;
|
|
let prisma: PrismaService;
|
|
|
|
const mockPrisma = {
|
|
uiText: {
|
|
findMany: jest.fn(),
|
|
upsert: jest.fn(),
|
|
},
|
|
scientificTerm: {
|
|
findMany: jest.fn(),
|
|
upsert: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
SettingsService,
|
|
{ provide: PrismaService, useValue: mockPrisma },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<SettingsService>(SettingsService);
|
|
prisma = module.get<PrismaService>(PrismaService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
it('should getUiTexts', async () => {
|
|
mockPrisma.uiText.findMany.mockResolvedValue([{ key: 'k', value: 'v' }]);
|
|
const result = await service.getUiTexts();
|
|
expect(prisma.uiText.findMany).toHaveBeenCalled();
|
|
expect(result).toHaveLength(1);
|
|
});
|
|
|
|
it('should updateUiText', async () => {
|
|
mockPrisma.uiText.upsert.mockResolvedValue({ key: 'k', value: 'v2' });
|
|
const result = await service.updateUiText('k', 'v2');
|
|
expect(prisma.uiText.upsert).toHaveBeenCalledWith({
|
|
where: { key: 'k' },
|
|
update: { value: 'v2' },
|
|
create: { key: 'k', value: 'v2' },
|
|
});
|
|
expect(result.value).toBe('v2');
|
|
});
|
|
|
|
it('should getScientificTerms', async () => {
|
|
mockPrisma.scientificTerm.findMany.mockResolvedValue([{ key: 'k' }]);
|
|
const result = await service.getScientificTerms();
|
|
expect(prisma.scientificTerm.findMany).toHaveBeenCalled();
|
|
expect(result).toHaveLength(1);
|
|
});
|
|
|
|
it('should upsertScientificTerm', async () => {
|
|
mockPrisma.scientificTerm.upsert.mockResolvedValue({ key: 'k', term: 't' });
|
|
const data = { term: 't', definition: 'd', wikiId: 'w' };
|
|
const result = await service.upsertScientificTerm('k', data);
|
|
expect(prisma.scientificTerm.upsert).toHaveBeenCalledWith({
|
|
where: { key: 'k' },
|
|
update: { term: 't', definition: 'd', wikiId: 'w' },
|
|
create: { key: 'k', term: 't', definition: 'd', wikiId: 'w' },
|
|
});
|
|
expect(result.term).toBe('t');
|
|
});
|
|
|
|
it('should deleteScientificTerm', async () => {
|
|
mockPrisma.scientificTerm.delete.mockResolvedValue({ key: 'k' });
|
|
const result = await service.deleteScientificTerm('k');
|
|
expect(prisma.scientificTerm.delete).toHaveBeenCalledWith({
|
|
where: { key: 'k' },
|
|
});
|
|
expect(result.key).toBe('k');
|
|
});
|
|
});
|