diff --git a/.vscode/launch.json b/.vscode/launch.json index 24e06db..49f2254 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,12 +7,23 @@ { "type": "node", "request": "launch", - "name": "start:dev", + "name": "start", "runtimeExecutable": "npm", "console": "integratedTerminal", "envFile": "${workspaceFolder}/.env", "runtimeArgs": ["run-script", "start:debug"], "skipFiles": ["/**"] + }, + { + "type": "node", + "request": "launch", + "name": "start:dev", + "runtimeExecutable": "npm", + "console": "integratedTerminal", + "envFile": "${workspaceFolder}/.env.dev", + "runtimeArgs": ["run-script", "start:debug"], + "skipFiles": ["/**"] } ] + } diff --git a/src/modules/event/services/interaction.spec.ts b/src/modules/event/services/interaction.spec.ts new file mode 100644 index 0000000..2ea7805 --- /dev/null +++ b/src/modules/event/services/interaction.spec.ts @@ -0,0 +1,23 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { Interaction } from './interaction'; +import { CommandService } from '../../command/command.service'; + +describe('Interaction', () => { + var service: Interaction; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [Interaction, + { + provide: CommandService, + useValue: {} + }], + }).compile(); + + service = module.get(Interaction); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/modules/someone-once-said/services/someone-once-said.service.spec.ts b/src/modules/someone-once-said/services/someone-once-said.service.spec.ts new file mode 100644 index 0000000..bd24ed9 --- /dev/null +++ b/src/modules/someone-once-said/services/someone-once-said.service.spec.ts @@ -0,0 +1,126 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { Model, Error, MongooseError } from 'mongoose'; +import { SomeoneOnceSaidService } from './someone-once-said.service'; +import { SomeoneOnceSaidEntity } from '../../../schemas/someone-once-said-entity.model'; +import { SomeoneOnceSaidDocument } from '../../../schemas/someone-once-said.schema'; +import { getModelToken } from '@nestjs/mongoose'; +describe('SomeoneOnceSaidService', () => { + let service: SomeoneOnceSaidService; + let modelMock: Model; + let mockDate: Date = new Date(0); + jest.useFakeTimers(); + jest.setSystemTime(mockDate); + const mockQuoteDto: SomeoneOnceSaidEntity = { + phrase: 'Test quote', + username: 'testUser', + secName: 'Teschter', + createdAt: mockDate, + }; + const mockQuoteDocument: SomeoneOnceSaidDocument = { + phrase: 'Test quote', + username: 'testUser', + secName: 'Teschter', + createdAt: mockDate, + } as SomeoneOnceSaidDocument; + + beforeEach(async () => { + jest.setSystemTime(0); + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SomeoneOnceSaidService, + { + provide: getModelToken('SomeoneOnceSaid'), + useValue: { + create: jest.fn((p) => mockQuoteDocument), + deleteMany: jest.fn((p) => [mockQuoteDocument]), + countDocuments: jest.fn((p) => 1), + findOne: jest.fn((p) => mockQuoteDocument), + }, + }, + ], + }).compile(); + + service = module.get(SomeoneOnceSaidService); + modelMock = module.get>( + getModelToken('SomeoneOnceSaid'), + ); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('create', () => { + it('should create a quote', async () => { + (modelMock as any).create = jest.fn((p) => mockQuoteDocument); + + const result = await service.create(mockQuoteDto); + + expect(result).toEqual(mockQuoteDocument); + expect(modelMock.create).toHaveBeenCalledWith(mockQuoteDto); + }); + + it('should return null when there is an error', async () => { + const mockQuoteDto: SomeoneOnceSaidEntity = { + phrase: 'Test quote', + username: 'testUser', + createdAt: mockDate, + }; + (modelMock as any).create = jest.fn((p) => new Error('Test error')); + + const result = await service.create(mockQuoteDto); + + expect(result).toStrictEqual(new MongooseError('Test error')); + }); + }); + + describe('deleteProductionOrderForUser', () => { + it('should delete quotes for a user', async () => { + const username = 'testUser'; + + const result = await service.deleteProductionOrderForUser(username); + + expect(result).toBeUndefined(); + expect(modelMock.deleteMany).toHaveBeenCalledWith({ username }); + }); + + it('should return an error when there is an error', async () => { + const username = 'testUser'; + (modelMock as any).deleteMany = jest + .fn() + .mockRejectedValue(new Error('test')); + + const result = await service.deleteProductionOrderForUser(username); + expect(result).toStrictEqual(new MongooseError(`Error deleting Quotes for username: ${username}`)) + + }); + }); + + describe('getRandomQuote', () => { + it('should return a random quote', async () => { + (modelMock as any).findOne = jest.fn((p) => ({ + skip: () => ({ + limit: () => mockQuoteDocument, + }), + })); + const countSpy = jest.spyOn(modelMock, 'countDocuments'); + const findOneSpy = jest.spyOn(modelMock, 'findOne'); + + const result = await service.getRandomQuote(); + + expect(result).toStrictEqual(mockQuoteDocument); + expect(countSpy).toHaveBeenCalled(); + expect(findOneSpy).toHaveBeenCalledTimes(1); + }); + + it('should return null when there is an error', async () => { + (modelMock as any).countDocuments = jest.fn( + (p) => new Error('Test error'), + ); + + const result = await service.getRandomQuote(); + + expect(result).toBeNull(); + }); + }); +});