Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
import { CatalogRepository } from './catalog.repository';
import { LoggerService } from '../../../logger/logger.service';
import { ModelTypes } from 'ottoman';
import { Result } from '../../../../src/util';
import { ICatalog, ICatalogFields } from './catalog.model';
describe('CatalogRepository', () => {
let catalogRepository: CatalogRepository;
let loggerService: LoggerService;
let mockCatalogModel: ModelTypes<ICatalog, any>;
beforeEach(async () => {
// Mocking the catalogModel object to simulate its methods
mockCatalogModel = {
find: jest.fn(),
save: jest.fn(),
updateById: jest.fn(),
removeById: jest.fn(),
};
// Mocking LoggerService
loggerService = { error: jest.fn() } as unknown as LoggerService;
const module: TestingModule = await Test.createTestingModule({
providers: [
CatalogRepository,
{ provide: LoggerService, useValue: loggerService },
{ provide: 'CATALOG_MODEL', useValue: mockCatalogModel },
],
}).compile();
catalogRepository = module.get<CatalogRepository>(CatalogRepository);
});
describe('createDocument', () => {
it('should create a document successfully', async () => {
const mockCatalog: ICatalog = { id: '1', itemCode: 'item-123', itemName: 'Item 123', itemIsModifier: false };
const mockResponse = { rows: [mockCatalog] };
// Mocking the save function of the model to return success
mockCatalogModel.save.mockResolvedValue(mockResponse);
const result = await catalogRepository.createDocument(mockCatalog);
expect(result.isSuccess()).toBe(true); // Check that the result is success
expect(mockCatalogModel.save).toHaveBeenCalled(); // Ensure save was called
});
it('should log an error when document creation fails', async () => {
const mockCatalog: ICatalog = { id: '1', itemCode: 'item-123', itemName: 'Item 123', itemIsModifier: false };
// Mocking the save function of the model to reject with an error
mockCatalogModel.save.mockRejectedValue(new Error('Error creating document'));
await catalogRepository.createDocument(mockCatalog);
// Ensure that an error was logged
expect(loggerService.error).toHaveBeenCalledWith('Error while creating the catalog creation');
});
});
describe('getList', () => {
it('should retrieve a list of catalog documents successfully', async () => {
const mockCatalogs: ICatalogFields[] = [
{ id: '1', itemCode: 'item-123', itemName: 'Item 123', itemIsModifier: false },
{ id: '2', itemCode: 'item-456', itemName: 'Item 456', itemIsModifier: true },
];
const mockResponse = { rows: mockCatalogs };
// Mocking the find method of the catalogModel to return mock data
mockCatalogModel.find.mockResolvedValue(mockResponse);
const result = await catalogRepository.getList();
expect(result.isSuccess()).toBe(true); // Ensure the result is successful
expect(result.getValue()).toEqual(mockCatalogs); // Ensure the result value matches the mock data
expect(mockCatalogModel.find).toHaveBeenCalled(); // Ensure that the find method was called
});
it('should log an error when retrieving catalog documents fails', async () => {
// Mocking the find method to reject with an error
mockCatalogModel.find.mockRejectedValue(new Error('Error retrieving documents'));
await catalogRepository.getList();
// Ensure that an error was logged
expect(loggerService.error).toHaveBeenCalledWith('Error while retrieving the catalog documents');
});
});
describe('getDocumentById', () => {
it('should return a catalog document by ID successfully', async () => {
const mockCatalog: ICatalogFields = {
id: '1',
itemCode: 'item-123',
itemName: 'Item 123',
itemIsModifier: false,
};
const mockResponse = { rows: [mockCatalog] };
// Mocking the find method to return mock data
mockCatalogModel.find.mockResolvedValue(mockResponse);
const result = await catalogRepository.getDocumentById('1');
expect(result.isSuccess()).toBe(true); // Ensure success
expect(result.getValue()).toEqual(mockCatalog); // Ensure the result value is correct
expect(mockCatalogModel.find).toHaveBeenCalledWith({ id: '1' }); // Ensure the correct ID was passed to find
});
it('should log an error if the document is not found', async () => {
mockCatalogModel.find.mockResolvedValue({ rows: [] }); // No document found
await catalogRepository.getDocumentById('1');
// Ensure that an error was logged
expect(loggerService.error).toHaveBeenCalledWith('Error while fetching the catalog 1');
});
});
describe('updateDocument', () => {
it('should update a catalog document successfully', async () => {
const updateData = { itemName: 'Updated Item' };
const mockResponse = { rows: [updateData] };
// Mocking updateById and save methods of catalogModel
mockCatalogModel.updateById.mockResolvedValue(mockResponse);
mockCatalogModel.save.mockResolvedValue(mockResponse);
const result = await catalogRepository.updateDocument('1', updateData);
expect(result.isSuccess()).toBe(true); // Ensure the result is successful
expect(mockCatalogModel.updateById).toHaveBeenCalledWith('1', updateData); // Ensure the updateById method was called with correct arguments
expect(mockCatalogModel.save).toHaveBeenCalled(); // Ensure save method was called
});
it('should log an error if updating the catalog document fails', async () => {
const updateData = { itemName: 'Updated Item' };
// Mocking updateById method to reject with an error
mockCatalogModel.updateById.mockRejectedValue(new Error('Error updating document'));
await catalogRepository.updateDocument('1', updateData);
// Ensure an error is logged
expect(loggerService.error).toHaveBeenCalledWith('Error while updating the catalog 1');
});
});
describe('deleteDocument', () => {
it('should delete a catalog document successfully', async () => {
// Mocking removeById method to resolve successfully
mockCatalogModel.removeById.mockResolvedValue({});
const result = await catalogRepository.deleteDocument('1');
expect(result.isSuccess()).toBe(true); // Ensure success
expect(mockCatalogModel.removeById).toHaveBeenCalledWith('1'); // Ensure removeById was called with the correct ID
});
it('should log an error if deleting the catalog document fails', async () => {
// Mocking removeById method to reject with an error
mockCatalogModel.removeById.mockRejectedValue(new Error('Error deleting document'));
await catalogRepository.deleteDocument('1');
// Ensure an error is logged
expect(loggerService.error).toHaveBeenCalledWith('Error while deleting the catalog 1');
});
});
});
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team