-
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.
Got IrrigationEventsService tests working
- Loading branch information
1 parent
c18f1fa
commit 0bfa06a
Showing
1 changed file
with
55 additions
and
4 deletions.
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
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) | ||
}) | ||
}) |