Skip to content

Commit

Permalink
Merge pull request #52 from Blvckleg/testing
Browse files Browse the repository at this point in the history
Testing suite for mongo quote service
  • Loading branch information
sanriodev authored Feb 23, 2024
2 parents 110ee45 + af3af6e commit 57b9f38
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ["<node_internals>/**"]
},
{
"type": "node",
"request": "launch",
"name": "start:dev",
"runtimeExecutable": "npm",
"console": "integratedTerminal",
"envFile": "${workspaceFolder}/.env.dev",
"runtimeArgs": ["run-script", "start:debug"],
"skipFiles": ["<node_internals>/**"]
}
]

}
23 changes: 23 additions & 0 deletions src/modules/event/services/interaction.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(Interaction);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -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<SomeoneOnceSaidDocument>;
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>(SomeoneOnceSaidService);
modelMock = module.get<Model<SomeoneOnceSaidDocument>>(
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();
});
});
});

0 comments on commit 57b9f38

Please sign in to comment.