-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unit tests on WebUsbHidTransport
- Loading branch information
1 parent
a86b263
commit 02478dd
Showing
3 changed files
with
194 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/core/src/internal/usb/transport/WebUsbHidTransport.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import { NoAccessibleDevice, UsbHidTransportNotSupported } from "./UsbHidTransport"; | ||
import { WebUsbHidTransport } from "./WebUsbHidTransport"; | ||
|
||
describe("WebUsbHidTransport", () => { | ||
describe("When WebHID API is not supported", () => { | ||
test("isSupported should return false", () => { | ||
const transport = new WebUsbHidTransport(); | ||
expect(transport.isSupported()).toBe(false); | ||
}); | ||
|
||
test("startDiscovering should emit an error", (done) => { | ||
const transport = new WebUsbHidTransport(); | ||
transport.startDiscovering().subscribe({ | ||
next: () => { | ||
done("Should not emit any value"); | ||
}, | ||
error: (error) => { | ||
expect(error).toBeInstanceOf(UsbHidTransportNotSupported); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("When WebHID API is supported", () => { | ||
const mockedGetDevices = jest.fn(); | ||
const mockedRequestDevice = jest.fn(); | ||
|
||
beforeAll(() => { | ||
global.navigator = { | ||
hid: { | ||
getDevices: mockedGetDevices, | ||
requestDevice: mockedRequestDevice, | ||
addEventListener: jest.fn(), | ||
} | ||
} as any; | ||
}); | ||
|
||
afterAll(() => { | ||
global.navigator = undefined as any; | ||
}); | ||
|
||
it("isSupported should return true", () => { | ||
const transport = new WebUsbHidTransport(); | ||
expect(transport.isSupported()).toBe(true); | ||
}); | ||
|
||
describe("startDiscovering", () => { | ||
test("If the user grant us access to a device, we should emit it", (done) => { | ||
mockedRequestDevice.mockResolvedValueOnce([{ | ||
opened: false, | ||
productId: 0x0001, | ||
vendorId: 0x2c97, | ||
productName: "Ledger Nano X", | ||
collections: [], | ||
}]); | ||
|
||
const transport = new WebUsbHidTransport(); | ||
transport.startDiscovering().subscribe({ | ||
next: (discoveredDevice) => { | ||
try { | ||
expect(discoveredDevice).toEqual(expect.objectContaining({ | ||
deviceModel: "nanoX", | ||
})); | ||
|
||
done(); | ||
} catch (expectError) { | ||
done(expectError); | ||
} | ||
}, | ||
error: (error) => { | ||
done(error); | ||
}, | ||
}); | ||
}); | ||
|
||
// It does not seem possible for a user to select several devices on the browser popup. | ||
// But if it was possible, we should emit them | ||
test("If the user grant us access to several devices, we should emit them", (done) => { | ||
mockedRequestDevice.mockResolvedValueOnce([{ | ||
opened: false, | ||
productId: 0x0004, | ||
vendorId: 0x2c97, | ||
productName: "Ledger Nano X", | ||
collections: [], | ||
}, { | ||
opened: false, | ||
productId: 0x0005, | ||
vendorId: 0x2c97, | ||
productName: "Ledger Nano S Plus", | ||
collections: [], | ||
}]); | ||
|
||
const transport = new WebUsbHidTransport(); | ||
|
||
let count = 0; | ||
transport.startDiscovering().subscribe({ | ||
next: (discoveredDevice) => { | ||
console.log("🦄 discoveredDevice", discoveredDevice); | ||
try { | ||
switch(count) { | ||
case 0: | ||
expect(discoveredDevice).toEqual(expect.objectContaining({ | ||
deviceModel: "nanoX", | ||
})); | ||
break; | ||
case 1: | ||
expect(discoveredDevice).toEqual(expect.objectContaining({ | ||
deviceModel: "nanoSPlus", | ||
})); | ||
|
||
done(); | ||
break; | ||
} | ||
|
||
count++; | ||
} catch (expectError) { | ||
done(expectError); | ||
} | ||
}, | ||
error: (error) => { | ||
done(error); | ||
}, | ||
}); | ||
}); | ||
|
||
// [ASK] Is this the behavior we want when the user does not select any device ? | ||
test("If the user did not grant us access to a device (clicking on cancel on the browser popup for ex), we should emit an error", (done) => { | ||
// When the user does not select any device, the `requestDevice` will return an empty array | ||
mockedRequestDevice.mockResolvedValueOnce([]); | ||
|
||
const transport = new WebUsbHidTransport(); | ||
transport.startDiscovering().subscribe({ | ||
next: (discoveredDevice) => { | ||
done(`Should not emit any value, but emitted ${JSON.stringify(discoveredDevice)}`); | ||
}, | ||
error: (error) => { | ||
try { | ||
expect(error).toBeInstanceOf(NoAccessibleDevice); | ||
done(); | ||
} catch (expectError) { | ||
done(expectError); | ||
} | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters