-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Blvckleg/testing
Testing suite for mongo quote service
- Loading branch information
Showing
3 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
126 changes: 126 additions & 0 deletions
126
src/modules/someone-once-said/services/someone-once-said.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |