-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.service.spec.ts
125 lines (103 loc) · 3.82 KB
/
gpio.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Test, TestingModule } from '@nestjs/testing';
import { GpioService } from './gpio.service';
import { ConfigModule } from '../../config/config.module';
import { EntitiesModule } from '../../entities/entities.module';
import { EntitiesService } from '../../entities/entities.service';
import { BinarySensor } from '../../entities/binary-sensor';
import { BinarySensorConfig } from '../home-assistant/binary-sensor-config';
import { Gpio } from 'onoff';
import { mocked } from 'ts-jest/utils';
import { ClusterService } from '../../cluster/cluster.service';
import { GpioSwitch } from './gpio.switch';
jest.mock('onoff');
describe('GpioService', () => {
let service: GpioService;
const entitiesService = {
add: jest.fn()
};
const loggerService = {
log: jest.fn(),
error: jest.fn(),
warn: jest.fn()
};
const mockGpio = mocked(Gpio);
beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule, EntitiesModule],
providers: [GpioService]
})
.overrideProvider(EntitiesService)
.useValue(entitiesService)
.overrideProvider(ClusterService)
.useValue({})
.compile();
module.useLogger(loggerService);
service = module.get<GpioService>(GpioService);
});
it('should register entities on bootstrap', () => {
service.onApplicationBootstrap();
expect(entitiesService.add).toHaveBeenCalledTimes(3);
expect(entitiesService.add).toHaveBeenCalledWith(
new BinarySensor('gpio-pir-sensor', 'PIR Sensor'),
expect.any(Array)
);
expect(entitiesService.add).toHaveBeenCalledWith(
new BinarySensor('gpio-radar', 'Radar'),
expect.any(Array)
);
expect(entitiesService.add).toHaveBeenCalledWith(
new GpioSwitch('gpio-test-switch', 'Test Switch', expect.any(Gpio)),
expect.any(Array)
);
});
it('should pass on entity customizations', () => {
service.onApplicationBootstrap();
expect(entitiesService.add.mock.calls[1][1]).toContainEqual({
for: BinarySensorConfig,
overrides: {
deviceClass: 'motion'
}
});
});
it('should export the switches as output pins', () => {
service.onApplicationBootstrap();
expect(mockGpio).toHaveBeenCalledWith(17, 'out');
});
it('should export the binary sensors as input pins', () => {
service.onApplicationBootstrap();
expect(mockGpio).toHaveBeenCalledWith(23, 'in', 'both');
expect(mockGpio).toHaveBeenCalledWith(24, 'in', 'both');
});
it('should watch input pins for interrupts', () => {
service.onApplicationBootstrap();
expect(mockGpio.mock.instances[0].watch).toHaveBeenCalled();
expect(mockGpio.mock.instances[1].watch).toHaveBeenCalled();
});
it('should update binary sensor state using the watch callback', () => {
const binarySensor = new BinarySensor('test', 'Test');
entitiesService.add.mockReturnValue(binarySensor);
service.onApplicationBootstrap();
const callback = mocked(mockGpio.mock.instances[0].watch).mock.calls[0][0];
callback(undefined, 1);
expect(binarySensor.state).toBe(true);
callback(undefined, 0);
expect(binarySensor.state).toBe(false);
});
it('should log any errors that happened while watching for interrupts', () => {
service.onApplicationBootstrap();
const callback = mocked(mockGpio.mock.instances[0].watch).mock.calls[0][0];
callback(new Error('Test Error'), undefined);
expect(loggerService.error).toHaveBeenCalledWith(
'Test Error',
expect.any(String),
GpioService.name
);
});
it('should unexport pins on shutdown', () => {
service.onApplicationBootstrap();
service.onApplicationShutdown();
expect(mockGpio.mock.instances[0].unexport).toHaveBeenCalled();
expect(mockGpio.mock.instances[1].unexport).toHaveBeenCalled();
});
});