Skip to content

Commit

Permalink
chore: add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-dewit committed Aug 26, 2024
1 parent 10dbf34 commit f5afd01
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface ConnectionManagerIntEvents {
connectionRemoved: [id: string]
}
export type MappedDeviceEvents = {
[T in keyof DeviceInstanceEvents as `connectionEvent:${T}`]: [deviceId: string, ...DeviceInstanceEvents[T]]
[T in keyof DeviceInstanceEvents as `connectionEvent:${T}`]: [string, ...DeviceInstanceEvents[T]]
}

export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DeviceType, OSCDeviceType } from 'timeline-state-resolver-types'
import { ConstructedMockDevices, MockDeviceInstanceWrapper } from '../../__tests__/mockDeviceInstanceWrapper'
import { ConnectionManager } from '../ConnectionManager'

// Mock explicitly the 'dist' version, as that is what threadedClass is being told to load
jest.mock('../../../dist/service/DeviceInstance', () => ({
DeviceInstanceWrapper: MockDeviceInstanceWrapper,
}))
jest.mock('../DeviceInstance', () => ({
DeviceInstanceWrapper: MockDeviceInstanceWrapper,
}))

describe('ConnectionManager', () => {
const connManager = new ConnectionManager()

test('adding/removing a device', async () => {
let resolveAdded: undefined | (() => void) = undefined
const psAdded = new Promise<void>((resolveCb) => (resolveAdded = resolveCb))
connManager.on('connectionAdded', () => {
if (resolveAdded) resolveAdded()
})

let resolveRemoved: undefined | (() => void) = undefined
const psRemoved = new Promise<void>((resolveCb) => (resolveRemoved = resolveCb))
connManager.on('connectionRemoved', () => {
if (resolveRemoved) resolveRemoved()
})

connManager.setConnections(
new Map(
Object.entries({
osc0: {
type: DeviceType.OSC,
options: {
host: '127.0.0.1',
port: 5250,
type: OSCDeviceType.UDP,
},
},
})
)
)

await psAdded

expect(ConstructedMockDevices['osc0']).toBeTruthy()

connManager.setConnections(new Map())

await psRemoved

expect(ConstructedMockDevices['osc0']).toBeFalsy()
})
})

0 comments on commit f5afd01

Please sign in to comment.