Skip to content

Commit

Permalink
chore: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Dec 5, 2024
1 parent f0ade46 commit f69a067
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/node/src/__mocks__/node-hid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +61,7 @@ export class HIDAsync extends EventEmitter {
throw new Error('Mock not implemented.')
}
async write(message: number[]): Promise<number> {
this.mockWriteHandler?.(this, message)
await this.mockWriteHandler?.(this, message)
return 0
}
async setNonBlocking(_noBlock: boolean): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion packages/node/src/__tests__/recordings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<XKeys> {
Expand All @@ -26,6 +26,9 @@ describe('Recorded tests', () => {
expect(HID.setMockWriteHandler).toBeTruthy()
})
beforeEach(() => {})
afterEach(() => {
HIDMock.resetMockWriteHandler()
})

const dirPath = './src/__tests__/recordings/'

Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/__tests__/watcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion packages/node/src/__tests__/xkeys.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f69a067

Please sign in to comment.