Skip to content
Snippets Groups Projects
Unverified Commit ec34b44e authored by Marius Friess's avatar Marius Friess Committed by GitHub
Browse files

Add unit tests for NoteModule (#47)

* add note entity test

* add tests for note service

* remove unused imports

* refactor mock note repository

* fix
parent e557343a
No related branches found
No related tags found
No related merge requests found
import { Note } from '../note.entity';
export class MockNoteRepository {
public async findOneOrFail({ id }: { id: number }): Promise<Note> {
if (id === 1) {
return Promise.resolve({
id: 1,
name: 'Test Note',
content: 'Test Note Content',
createdAt: new Date(),
updatedAt: new Date(),
} as Note);
}
throw new Error('Note not found');
}
}
import { Note } from './note.entity';
import { Room } from '../room/room.entity';
import { Category } from '../category/category.entity';
describe('Note Entity', () => {
let room: Room;
beforeEach(() => {
room = new Room('Test Room', new Category('Test Category', undefined));
});
it('should be able to create an instance', () => {
const note = new Note('Test Note', room);
expect(note).toBeDefined();
expect(note.name).toEqual('Test Note');
expect(note.room).toEqual(room);
});
it('should set default dates on creation', () => {
const note = new Note('Test Note', room);
const currentDate = new Date();
expect(note.createdAt.getTime()).toBeLessThanOrEqual(currentDate.getTime());
expect(note.updatedAt.getTime()).toBeLessThanOrEqual(currentDate.getTime());
});
});
import { NoteService } from './note.service';
import { Test, TestingModule } from '@nestjs/testing';
import { EntityManager } from '@mikro-orm/core';
import { getRepositoryToken } from '@mikro-orm/nestjs';
import { Note } from './note.entity';
import { Room } from '../room/room.entity';
import { Category } from '../category/category.entity';
import { MockNoteRepository } from './mock/note.repository.mock';
/**
* Test suite for the 'NoteService' class.
*/
describe('NoteService', () => {
let service: NoteService;
let entityManager: EntityManager;
/**
* Initialize the testing module and get an instance of `NoteService`.
*/
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
NoteService,
{
provide: EntityManager,
useValue: {
persistAndFlush: jest.fn(),
removeAndFlush: jest.fn(),
},
},
{
provide: getRepositoryToken(Note),
useValue: new MockNoteRepository(),
},
],
}).compile();
service = module.get<NoteService>(NoteService);
entityManager = module.get<EntityManager>(EntityManager);
});
/**
* Test case to ensure that the `NoteService` instance is defined.
*/
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('addNote', () => {
let room: Room;
beforeEach(() => {
room = new Room('Test Room', new Category('Test Category', undefined));
});
it('should create a new note and add it to the given room', async () => {
const noteName = 'New Test Note';
const createdNote = await service.addNote(room, noteName);
expect(createdNote.name).toBe(noteName);
expect(createdNote.room).toBe(room);
expect(entityManager.persistAndFlush).toHaveBeenCalledWith(createdNote);
});
});
describe('updateNote', () => {
it('should update note content correctly', async () => {
const updatedContent = 'Updated Test Note Content';
const updatedNote = await service.updateNote(1, updatedContent);
expect(updatedNote.content).toBe(updatedContent);
expect(entityManager.persistAndFlush).toHaveBeenCalledWith(updatedNote);
});
it('should throw an error if note does not exist', async () => {
await expect(service.updateNote(2, 'Some content')).rejects.toThrow(
'Note not found',
);
expect(entityManager.persistAndFlush).not.toHaveBeenCalled();
});
});
describe('deleteNoteById', () => {
it('should delete a note successfully when note with given ID exists', async () => {
const result = await service.deleteNoteById(1);
expect(result).toBe(true);
expect(entityManager.removeAndFlush).toHaveBeenCalled();
});
it('should throw an error when note with given ID does not exist', async () => {
await expect(service.deleteNoteById(2)).rejects.toThrow('Note not found');
expect(entityManager.removeAndFlush).not.toHaveBeenCalled();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment