File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed
Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,23 @@ describe("MicrobitWebUSBConnection (WebUSB unsupported)", () => {
3030 const microbit = createWebUSBConnection ( ) ;
3131 expect ( microbit . status ) . toBe ( ConnectionStatus . NOT_SUPPORTED ) ;
3232 } ) ;
33+ it ( "still triggers afterrequestdevice if requestDevice throws" , async ( ) => {
34+ ( global as any ) . navigator = {
35+ usb : {
36+ requestDevice : ( ) => {
37+ throw new Error ( ) ;
38+ } ,
39+ } ,
40+ } ;
41+ const microbit = createWebUSBConnection ( ) ;
42+ expect ( microbit . status ) . toBe ( ConnectionStatus . NO_AUTHORIZED_DEVICE ) ;
43+ const afterRequestDevice = vi . fn ( ) ;
44+ microbit . addEventListener ( "afterrequestdevice" , afterRequestDevice ) ;
45+
46+ await expect ( ( ) => microbit . connect ( ) ) . rejects . toThrow ( ) ;
47+
48+ expect ( afterRequestDevice . mock . calls . length ) . toEqual ( 1 ) ;
49+ } ) ;
3350} ) ;
3451
3552describeDeviceOnly ( "MicrobitWebUSBConnection (WebUSB supported)" , ( ) => {
Original file line number Diff line number Diff line change @@ -562,11 +562,14 @@ class MicrobitWebUSBConnectionImpl
562562
563563 private async chooseDevice ( ) : Promise < USBDevice > {
564564 this . dispatchTypedEvent ( "beforerequestdevice" , new BeforeRequestDevice ( ) ) ;
565- this . device = await navigator . usb . requestDevice ( {
566- exclusionFilters : this . exclusionFilters ,
567- filters : defaultFilters ,
568- } ) ;
569- this . dispatchTypedEvent ( "afterrequestdevice" , new AfterRequestDevice ( ) ) ;
565+ try {
566+ this . device = await navigator . usb . requestDevice ( {
567+ exclusionFilters : this . exclusionFilters ,
568+ filters : defaultFilters ,
569+ } ) ;
570+ } finally {
571+ this . dispatchTypedEvent ( "afterrequestdevice" , new AfterRequestDevice ( ) ) ;
572+ }
570573 return this . device ;
571574 }
572575
You can’t perform that action at this time.
0 commit comments