|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing'; |
2 | | -import { UPSResolver } from './ups.resolver.js'; |
3 | | -import { UPSService, UPSData } from './ups.service.js'; |
4 | | -import { describe, it, expect, beforeEach, vi } from 'vitest'; |
5 | | -import { UPSConfigInput } from './ups.inputs.js'; |
| 2 | + |
6 | 3 | import { PubSub } from 'graphql-subscriptions'; |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { UPSConfigInput } from '@app/unraid-api/graph/resolvers/ups/ups.inputs.js'; |
| 7 | +import { UPSResolver } from '@app/unraid-api/graph/resolvers/ups/ups.resolver.js'; |
| 8 | +import { UPSData, UPSService } from '@app/unraid-api/graph/resolvers/ups/ups.service.js'; |
7 | 9 |
|
8 | 10 | describe('UPSResolver', () => { |
9 | | - let resolver: UPSResolver; |
10 | | - let service: UPSService; |
11 | | - let pubSub: PubSub; |
| 11 | + let resolver: UPSResolver; |
| 12 | + let service: UPSService; |
| 13 | + let pubSub: PubSub; |
12 | 14 |
|
13 | | - const mockUPSData: UPSData = { |
14 | | - MODEL: 'Test UPS', |
15 | | - STATUS: 'Online', |
16 | | - BCHARGE: '100', |
17 | | - TIMELEFT: '3600', |
18 | | - LINEV: '120.5', |
19 | | - OUTPUTV: '120.5', |
20 | | - LOADPCT: '25', |
21 | | - }; |
| 15 | + const mockUPSData: UPSData = { |
| 16 | + MODEL: 'Test UPS', |
| 17 | + STATUS: 'Online', |
| 18 | + BCHARGE: '100', |
| 19 | + TIMELEFT: '3600', |
| 20 | + LINEV: '120.5', |
| 21 | + OUTPUTV: '120.5', |
| 22 | + LOADPCT: '25', |
| 23 | + }; |
22 | 24 |
|
23 | | - beforeEach(async () => { |
24 | | - const module: TestingModule = await Test.createTestingModule({ |
25 | | - providers: [ |
26 | | - UPSResolver, |
27 | | - { |
28 | | - provide: UPSService, |
29 | | - useValue: { |
30 | | - getUPSData: vi.fn().mockResolvedValue(mockUPSData), |
31 | | - configureUPS: vi.fn().mockResolvedValue(undefined), |
32 | | - }, |
33 | | - }, |
34 | | - { |
35 | | - provide: PubSub, |
36 | | - useValue: { |
37 | | - publish: vi.fn(), |
38 | | - asyncIterator: vi.fn(), |
39 | | - }, |
40 | | - }, |
41 | | - ], |
42 | | - }).compile(); |
| 25 | + beforeEach(async () => { |
| 26 | + const module: TestingModule = await Test.createTestingModule({ |
| 27 | + providers: [ |
| 28 | + UPSResolver, |
| 29 | + { |
| 30 | + provide: UPSService, |
| 31 | + useValue: { |
| 32 | + getUPSData: vi.fn().mockResolvedValue(mockUPSData), |
| 33 | + configureUPS: vi.fn().mockResolvedValue(undefined), |
| 34 | + getCurrentConfig: vi.fn().mockResolvedValue({}), |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + provide: PubSub, |
| 39 | + useValue: { |
| 40 | + publish: vi.fn(), |
| 41 | + asyncIterableIterator: vi.fn(), |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }).compile(); |
43 | 46 |
|
44 | | - resolver = module.get<UPSResolver>(UPSResolver); |
45 | | - service = module.get<UPSService>(UPSService); |
46 | | - pubSub = module.get<PubSub>(PubSub); |
47 | | - }); |
| 47 | + resolver = module.get<UPSResolver>(UPSResolver); |
| 48 | + service = module.get<UPSService>(UPSService); |
| 49 | + pubSub = module.get<PubSub>(PubSub); |
| 50 | + }); |
48 | 51 |
|
49 | | - it('should be defined', () => { |
50 | | - expect(resolver).toBeDefined(); |
51 | | - }); |
| 52 | + it('should be defined', () => { |
| 53 | + expect(resolver).toBeDefined(); |
| 54 | + }); |
52 | 55 |
|
53 | | - describe('upsDevices', () => { |
54 | | - it('should return an array of UPS devices', async () => { |
55 | | - const result = await resolver.upsDevices(); |
56 | | - expect(result).toBeInstanceOf(Array); |
57 | | - expect(result[0].model).toBe('Test UPS'); |
58 | | - expect(service.getUPSData).toHaveBeenCalled(); |
| 56 | + describe('upsDevices', () => { |
| 57 | + it('should return an array of UPS devices', async () => { |
| 58 | + const result = await resolver.upsDevices(); |
| 59 | + expect(result).toBeInstanceOf(Array); |
| 60 | + expect(result[0].model).toBe('Test UPS'); |
| 61 | + expect(service.getUPSData).toHaveBeenCalled(); |
| 62 | + }); |
59 | 63 | }); |
60 | | - }); |
61 | 64 |
|
62 | | - describe('configureUps', () => { |
63 | | - it('should call the configureUPS service method and return true', async () => { |
64 | | - const config: UPSConfigInput = { |
65 | | - SERVICE: 'enable', |
66 | | - UPSCABLE: 'usb', |
67 | | - UPSTYPE: 'usb', |
68 | | - BATTERYLEVEL: 10, |
69 | | - MINUTES: 5, |
70 | | - TIMEOUT: 0, |
71 | | - KILLUPS: 'no', |
72 | | - }; |
73 | | - const result = await resolver.configureUps(config); |
74 | | - expect(result).toBe(true); |
75 | | - expect(service.configureUPS).toHaveBeenCalledWith(config); |
76 | | - expect(pubSub.publish).toHaveBeenCalledWith('upsUpdates', expect.any(Object)); |
| 65 | + describe('configureUps', () => { |
| 66 | + it('should call the configureUPS service method and return true', async () => { |
| 67 | + const config: UPSConfigInput = { |
| 68 | + SERVICE: 'enable', |
| 69 | + UPSCABLE: 'usb', |
| 70 | + UPSTYPE: 'usb', |
| 71 | + BATTERYLEVEL: 10, |
| 72 | + MINUTES: 5, |
| 73 | + TIMEOUT: 0, |
| 74 | + KILLUPS: 'no', |
| 75 | + }; |
| 76 | + const result = await resolver.configureUps(config); |
| 77 | + expect(result).toBe(true); |
| 78 | + expect(service.configureUPS).toHaveBeenCalledWith(config); |
| 79 | + expect(pubSub.publish).toHaveBeenCalledWith('upsUpdates', expect.any(Object)); |
| 80 | + }); |
77 | 81 | }); |
78 | | - }); |
79 | 82 |
|
80 | | - describe('upsUpdates', () => { |
81 | | - it('should return an async iterator', () => { |
82 | | - resolver.upsUpdates(); |
83 | | - expect(pubSub.asyncIterator).toHaveBeenCalledWith('upsUpdates'); |
| 83 | + describe('upsUpdates', () => { |
| 84 | + it('should return an async iterator', () => { |
| 85 | + resolver.upsUpdates(); |
| 86 | + expect(pubSub.asyncIterableIterator).toHaveBeenCalledWith('upsUpdates'); |
| 87 | + }); |
84 | 88 | }); |
85 | | - }); |
86 | 89 | }); |
0 commit comments