From 64b04b045efcce33d5e7b0283195fd11207e81a7 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 29 Nov 2023 23:10:52 +0000 Subject: [PATCH] chore: fix tests --- packages/node/src/__mocks__/hid.ts | 17 ++++-- packages/node/src/__tests__/device.spec.ts | 67 ++++++++++++++-------- 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/packages/node/src/__mocks__/hid.ts b/packages/node/src/__mocks__/hid.ts index 776dd24..94e6352 100644 --- a/packages/node/src/__mocks__/hid.ts +++ b/packages/node/src/__mocks__/hid.ts @@ -1,13 +1,9 @@ /* eslint-disable jest/no-standalone-expect */ import { EventEmitter } from 'events' -import type { HIDAsync } from 'node-hid' +import type { Device, HIDAsync } from 'node-hid' export class DummyHID extends EventEmitter implements HIDAsync { - public path: string - - constructor(devicePath: string) { + constructor() { super() - expect(typeof devicePath).toEqual('string') - this.path = devicePath } public async close(): Promise { throw new Error('Not implemented') @@ -39,4 +35,13 @@ export class DummyHID extends EventEmitter implements HIDAsync { public async setNonBlocking(_no_block: boolean): Promise { throw new Error('Method not implemented.') } + + public async generateDeviceInfo(): Promise { + // TODO - remove this! + throw new Error('Not implemented') + } + + public getDeviceInfo = jest.fn(async (): Promise => { + throw new Error('Not implemented') + }) } diff --git a/packages/node/src/__tests__/device.spec.ts b/packages/node/src/__tests__/device.spec.ts index 56628da..48dc6e5 100644 --- a/packages/node/src/__tests__/device.spec.ts +++ b/packages/node/src/__tests__/device.spec.ts @@ -4,12 +4,14 @@ import { mocked } from 'jest-mock' import { DummyHID } from '../__mocks__/hid' jest.mock('node-hid') -import { devicesAsync, openAsyncHIDDevice } from 'node-hid' +import { devicesAsync, HIDAsync } from 'node-hid' // Forcing path to be string, as there are multiple constructor options, we require the string one -mocked(openAsyncHIDDevice).mockImplementation(async (path: string | number) => new DummyHID(path as string)) +const hidOpenMock = jest.fn, [path: string]>() +HIDAsync.open = hidOpenMock as any // Must be required after we register a mock for `node-hid`. import { getStreamDeckInfo, listStreamDecks, openStreamDeck } from '../' +import { VENDOR_ID } from '@elgato-stream-deck/core' describe('StreamDeck Devices', () => { test('no devices', async () => { @@ -128,31 +130,50 @@ describe('StreamDeck Devices', () => { serialNumber: 'some-number-again', }) }) - test('create for bad path', async () => { - mocked(devicesAsync).mockImplementation(async () => [ - { - productId: 0x0060, - vendorId: 0x0fd9, - interface: 0, - path: 'path-original', - serialNumber: 'some-number', + test('create for bad ids', async () => { + const mockHid = new DummyHID() + hidOpenMock.mockImplementation(async () => mockHid) + + // bad productId + mockHid.getDeviceInfo.mockImplementationOnce(async () => { + return { + vendorId: VENDOR_ID, + productId: 0x0050, release: 0, - }, - { - productId: 0x0022, - vendorId: 0x0fd9, interface: 0, - path: 'path-bad-product', - release: 0, - }, - ]) - - await expect(openStreamDeck('not-a-real-path')).rejects.toThrow( - new Error(`Device "not-a-real-path" was not found`) - ) + } + }) + await expect(openStreamDeck('not-a-real-path')).rejects.toThrow(new Error(`Stream Deck is of unexpected type.`)) + expect(hidOpenMock).toHaveBeenLastCalledWith('not-a-real-path') + // bad vendorId + mockHid.getDeviceInfo.mockImplementationOnce(async () => { + return { + vendorId: VENDOR_ID + 1, + productId: 0x0060, + release: 0, + interface: 0, + } + }) await expect(openStreamDeck('path-bad-product')).rejects.toThrow( - new Error(`Device "path-bad-product" was not found`) + new Error(`Stream Deck is of unexpected type.`) ) + expect(hidOpenMock).toHaveBeenLastCalledWith('path-bad-product') + }) + + test('create for good ids', async () => { + const mockHid = new DummyHID() + hidOpenMock.mockImplementation(async () => mockHid) + + mockHid.getDeviceInfo.mockImplementationOnce(async () => { + return { + vendorId: VENDOR_ID, + productId: 0x0060, + release: 0, + interface: 0, + } + }) + await expect(openStreamDeck('not-a-real-path')).resolves.toBeTruthy() + expect(hidOpenMock).toHaveBeenLastCalledWith('not-a-real-path') }) })