Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Nov 29, 2023
1 parent 61257df commit 64b04b0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
17 changes: 11 additions & 6 deletions packages/node/src/__mocks__/hid.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
throw new Error('Not implemented')
Expand Down Expand Up @@ -39,4 +35,13 @@ export class DummyHID extends EventEmitter implements HIDAsync {
public async setNonBlocking(_no_block: boolean): Promise<void> {
throw new Error('Method not implemented.')
}

public async generateDeviceInfo(): Promise<Device> {
// TODO - remove this!
throw new Error('Not implemented')
}

public getDeviceInfo = jest.fn(async (): Promise<Device> => {
throw new Error('Not implemented')
})
}
67 changes: 44 additions & 23 deletions packages/node/src/__tests__/device.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<HIDAsync>, [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 () => {
Expand Down Expand Up @@ -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')
})
})

0 comments on commit 64b04b0

Please sign in to comment.