Skip to content

Commit

Permalink
Got tests working for IrrigationEventsController
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpetro committed Feb 11, 2024
1 parent 3b7f45b commit 799d066
Showing 1 changed file with 183 additions and 2 deletions.
185 changes: 183 additions & 2 deletions src/irrigation-events/irrigation-events.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,202 @@ import { MakerApiService } from './maker-api.service'
import { ViewmodelTransformService } from './viewmodel-transform.service'
import { IrrigationEventsService } from './irrigation-events.service'
import { ConfigModule } from '@nestjs/config'
import { DeviceState } from './enums/device-state.interface'
import { MakerApiEventDto } from './dto/maker-api-event.dto'
import { IrrigationEvent } from './interfaces/irrigation-event.interface'
import { parseISO } from 'date-fns'
import { DeviceStates } from './interfaces/device-states.interface'

describe('IrrigationEventsController', () => {
let controller: IrrigationEventsController
const mockInsertIrrigationEvent = jest.fn()
const mockGetIrrigationEvents = jest.fn()
const mockGetEventsBeforeStart = jest.fn()
const mockGetEventsAfterEnd = jest.fn()
const mockGetAllDeviceDetails = jest.fn()

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot({ envFilePath: '.env.local' })],
controllers: [IrrigationEventsController],
providers: [IrrigationEventsService, MakerApiService, ViewmodelTransformService],
}).compile()
providers: [ViewmodelTransformService],
})
.useMocker((token) => {
if (token === IrrigationEventsService) {
return {
insertIrrigationEvent: mockInsertIrrigationEvent,
getIrrigationEvents: mockGetIrrigationEvents,
getEventsBeforeStart: mockGetEventsBeforeStart,
getEventsAfterEnd: mockGetEventsAfterEnd,
}
}
if (token === MakerApiService) {
return {
getAllDeviceDetails: mockGetAllDeviceDetails,
}
}
})
.compile()

controller = module.get<IrrigationEventsController>(IrrigationEventsController)
})

afterEach(() => {
jest.clearAllMocks()
})

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

it('should call insertIrrigationEvent', async () => {
const makerApiEventDto = {
name: 'switch',
displayName: 'Device 1',
deviceId: 1,
value: DeviceState.ON,
} as MakerApiEventDto
await controller.create(makerApiEventDto)
expect(mockInsertIrrigationEvent).toHaveBeenCalledWith(makerApiEventDto)
})

it('should return viewmodels', async () => {
mockGetIrrigationEvents.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T10:00:00.000Z'),
deviceName: 'Device 1',
deviceId: 1,
state: DeviceState.ON,
},
{
timestamp: parseISO('2024-01-01T11:00:00.000Z'),
deviceName: 'Device 1',
deviceId: 1,
state: DeviceState.OFF,
},
{
timestamp: parseISO('2024-01-01T11:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.ON,
},
{
timestamp: parseISO('2024-01-01T12:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.OFF,
},
] as IrrigationEvent[])
const startTimestamp = '2024-01-01T00:00:00.000Z'
const endTimestamp = '2024-01-02T00:00:00.000Z'
const viewmodels = await controller.get({ startTimestamp, endTimestamp })
expect(mockGetIrrigationEvents).toHaveBeenCalledWith(startTimestamp, endTimestamp)
expect(viewmodels).toEqual([
{
startDate: '2024-01-01T10:00:00.000Z',
endDate: '2024-01-01T11:00:00.000Z',
title: 'Device 1',
deviceId: 1,
},
{
startDate: '2024-01-01T11:00:00.000Z',
endDate: '2024-01-01T12:00:00.000Z',
title: 'Device 42',
deviceId: 42,
},
])
})

it('should get events before the start of the time range', async () => {
mockGetIrrigationEvents.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T12:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.OFF,
},
] as IrrigationEvent[])
mockGetEventsBeforeStart.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T11:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.ON,
},
])
const startTimestamp = '2024-01-01T11:30:00.000Z'
const endTimestamp = '2024-01-02T00:00:00.000Z'
const viewmodels = await controller.get({ startTimestamp, endTimestamp })
expect(mockGetIrrigationEvents).toHaveBeenCalledWith(startTimestamp, endTimestamp)
expect(mockGetEventsBeforeStart).toHaveBeenCalledWith(startTimestamp, 42)
expect(viewmodels).toEqual([
{
startDate: '2024-01-01T11:00:00.000Z',
endDate: '2024-01-01T12:00:00.000Z',
title: 'Device 42',
deviceId: 42,
},
])
})

it('should get events after the end of the time range', async () => {
mockGetIrrigationEvents.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T11:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.ON,
},
] as IrrigationEvent[])
mockGetEventsAfterEnd.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T12:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.OFF,
},
])
const startTimestamp = '2024-01-01T00:00:00.000Z'
const endTimestamp = '2024-01-02T11:30:00.000Z'
const viewmodels = await controller.get({ startTimestamp, endTimestamp })
expect(mockGetIrrigationEvents).toHaveBeenCalledWith(startTimestamp, endTimestamp)
expect(mockGetEventsAfterEnd).toHaveBeenCalledWith(endTimestamp, 42)
expect(viewmodels).toEqual([
{
startDate: '2024-01-01T11:00:00.000Z',
endDate: '2024-01-01T12:00:00.000Z',
title: 'Device 42',
deviceId: 42,
},
])
})

it('should add current device states', async () => {
mockGetIrrigationEvents.mockResolvedValue([
{
timestamp: parseISO('2024-01-01T11:00:00.000Z'),
deviceName: 'Device 42',
deviceId: 42,
state: DeviceState.ON,
},
] as IrrigationEvent[])
mockGetEventsAfterEnd.mockResolvedValue([])
mockGetAllDeviceDetails.mockResolvedValue({ 42: DeviceState.ON } as DeviceStates)
const dateSpy = jest.spyOn(Date, 'now').mockImplementation(() => new Date('2024-01-01T11:30:00.000Z').getTime())
const startTimestamp = '2024-01-01T00:00:00.000Z'
const endTimestamp = '2024-01-02T00:00:00.000Z'
const viewmodels = await controller.get({ startTimestamp, endTimestamp })
expect(mockGetIrrigationEvents).toHaveBeenCalledWith(startTimestamp, endTimestamp)
expect(mockGetAllDeviceDetails).toHaveBeenCalled()
expect(viewmodels).toEqual([
{
startDate: '2024-01-01T11:00:00.000Z',
endDate: '2024-01-01T11:30:00.000Z',
title: 'Device 42',
deviceId: 42,
currentlyOn: true,
},
])
dateSpy.mockRestore()
})
})

0 comments on commit 799d066

Please sign in to comment.