diff --git a/packages/node/src/__mocks__/node-hid.ts b/packages/node/src/__mocks__/node-hid.ts index 1558093..1b72df7 100644 --- a/packages/node/src/__mocks__/node-hid.ts +++ b/packages/node/src/__mocks__/node-hid.ts @@ -6,6 +6,9 @@ let mockWriteHandler: undefined | ((hid: HIDAsync, message: number[]) => void) = export function setMockWriteHandler(handler: (hid: HIDAsync, message: number[]) => void) { mockWriteHandler = handler } +export function resetMockWriteHandler() { + mockWriteHandler = undefined +} let mockDevices: Device[] = [] export function mockSetDevices(devices: Device[]) { mockDevices = devices @@ -58,7 +61,7 @@ export class HIDAsync extends EventEmitter { throw new Error('Mock not implemented.') } async write(message: number[]): Promise { - this.mockWriteHandler?.(this, message) + await this.mockWriteHandler?.(this, message) return 0 } async setNonBlocking(_noBlock: boolean): Promise { diff --git a/packages/node/src/__tests__/recordings.spec.ts b/packages/node/src/__tests__/recordings.spec.ts index 98a6c00..ba1df1c 100644 --- a/packages/node/src/__tests__/recordings.spec.ts +++ b/packages/node/src/__tests__/recordings.spec.ts @@ -3,7 +3,7 @@ import * as HID from 'node-hid' import { Product, PRODUCTS, describeEvent } from '@xkeys-lib/core' import * as HIDMock from '../__mocks__/node-hid' import { setupXkeysPanel, XKeys, XKeysEvents } from '../' -import { getSentData, handleXkeysMessages, resetSentData } from './lib' +import { getSentData, handleXkeysMessages, resetSentData, sleep } from './lib' describe('Recorded tests', () => { async function setupTestPanel(params: { productId: number }): Promise { @@ -26,6 +26,9 @@ describe('Recorded tests', () => { expect(HID.setMockWriteHandler).toBeTruthy() }) beforeEach(() => {}) + afterEach(() => { + HIDMock.resetMockWriteHandler() + }) const dirPath = './src/__tests__/recordings/' @@ -133,6 +136,8 @@ describe('Recorded tests', () => { // @ts-expect-error hack xkeysDevice[action.method](...action.arguments) + await xkeysDevice.flush() + expect(getSentData()).toEqual(action.sentData) resetSentData() } catch (err) { diff --git a/packages/node/src/__tests__/watcher.spec.ts b/packages/node/src/__tests__/watcher.spec.ts index a8b2d3c..74448dc 100644 --- a/packages/node/src/__tests__/watcher.spec.ts +++ b/packages/node/src/__tests__/watcher.spec.ts @@ -4,6 +4,9 @@ import { NodeHIDDevice, XKeys, XKeysWatcher } from '..' import { handleXkeysMessages, sleep, sleepTicks } from './lib' describe('XKeysWatcher', () => { + afterEach(() => { + HIDMock.resetMockWriteHandler() + }) test('Detect device (w polling)', async () => { const POLL_INTERVAL = 10 NodeHIDDevice.CLOSE_WAIT_TIME = 0 // We can override this to speed up the unit tests diff --git a/packages/node/src/__tests__/xkeys.spec.ts b/packages/node/src/__tests__/xkeys.spec.ts index ab2b26f..8cab9ab 100644 --- a/packages/node/src/__tests__/xkeys.spec.ts +++ b/packages/node/src/__tests__/xkeys.spec.ts @@ -1,9 +1,12 @@ import * as HID from 'node-hid' import * as HIDMock from '../__mocks__/node-hid' import { setupXkeysPanel, XKeys } from '../' -import { getSentData, handleXkeysMessages, resetSentData } from './lib' +import { getSentData, handleXkeysMessages, resetSentData, sleep } from './lib' describe('Unit tests', () => { + afterEach(() => { + HIDMock.resetMockWriteHandler() + }) test('calculateDelta', () => { expect(XKeys.calculateDelta(100, 100)).toBe(0) expect(XKeys.calculateDelta(110, 100)).toBe(10) @@ -40,98 +43,129 @@ describe('Unit tests', () => { expect(myXkeysPanel.info).toMatchSnapshot() resetSentData() myXkeysPanel.getButtons() + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setIndicatorLED(5, true) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setIndicatorLED(5, false) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() + myXkeysPanel.setIndicatorLED(5, true, true) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, '59f') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, '5599ff') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, '#5599ff') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, { r: 45, g: 210, b: 255 }) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, true) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, false) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, null) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, null) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklight(5, '59f', true) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights('59f') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights('5599ff') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights('#5599ff') + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights({ r: 45, g: 210, b: 255 }) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights(true) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights(false) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights(null) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setAllBacklights(null) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.toggleAllBacklights() + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklightIntensity(100) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setBacklightIntensity(0, 255) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.saveBackLights() + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setFrequency(127) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.setUnitId(42) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() myXkeysPanel.rebootDevice() + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData() // expect(myXkeysPanel.writeLcdDisplay(line: number, displayChar: string, backlight: boolean) + await myXkeysPanel.flush() // expect(getSentData()).toMatchSnapshot() // resetSentData() myXkeysPanel.writeData([0, 1, 2, 3, 4]) + await myXkeysPanel.flush() expect(getSentData()).toMatchSnapshot() resetSentData()