Skip to content

Commit

Permalink
Got IrrigationEventsService tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpetro committed Feb 10, 2024
1 parent c18f1fa commit 0bfa06a
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions src/irrigation-events/irrigation-events.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
import { Test, TestingModule } from '@nestjs/testing'
import { IrrigationEventsService } from '@/irrigation-events/irrigation-events.service'
import { IrrigationEventDocument } from './interfaces/irrigation-event-document.interface'
import { MakerApiEventDto } from './dto/maker-api-event.dto'
import { DeviceState } from './enums/device-state.interface'
import { ConfigModule } from '@nestjs/config'

// Mock the Nano library
const mockInsert = jest.fn().mockResolvedValue({})
const mockGet = jest.fn().mockResolvedValue({})
jest.mock('nano', () => {
return {
__esModule: true,
default: jest.fn().mockImplementation(() => {
return {
auth: jest.fn(),
db: {
use: jest.fn().mockReturnValue({
get: mockGet,
insert: mockInsert,
// Add other methods as needed
}),
},
}
}),
}
})

import { IrrigationEventsService } from '@/irrigation-events/irrigation-events.service'

const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/

describe('IrrigationEventsService', () => {
let service: IrrigationEventsService
let testingModule: TestingModule

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot({ envFilePath: '.env.local' })],
testingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot({ envFilePath: '.env.testing' })],
providers: [IrrigationEventsService],
}).compile()

service = module.get<IrrigationEventsService>(IrrigationEventsService)
testingModule.init()
testingModule.enableShutdownHooks()
service = testingModule.get<IrrigationEventsService>(IrrigationEventsService)
})

afterEach(async () => {
await testingModule.close()
})

it('should be defined', () => {
expect(service).toBeDefined()
})

it('should insert an irrigation event', async () => {
const mockIrrigationEvent: MakerApiEventDto = {
name: 'Name Field',
displayName: 'Display Name Field',
value: DeviceState.ON,
deviceId: 42,
}
const mockDocument: IrrigationEventDocument = {
_id: expect.stringMatching(iso8601Regex),
deviceName: 'Display Name Field',
state: DeviceState.ON,
deviceId: 42,
}
await service.insertIrrigationEvent(mockIrrigationEvent)
expect(mockInsert).toHaveBeenCalledWith(mockDocument)
})
})

0 comments on commit 0bfa06a

Please sign in to comment.