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)", () => {
30
30
const microbit = createWebUSBConnection ( ) ;
31
31
expect ( microbit . status ) . toBe ( ConnectionStatus . NOT_SUPPORTED ) ;
32
32
} ) ;
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
+ } ) ;
33
50
} ) ;
34
51
35
52
describeDeviceOnly ( "MicrobitWebUSBConnection (WebUSB supported)" , ( ) => {
Original file line number Diff line number Diff line change @@ -550,11 +550,14 @@ class MicrobitWebUSBConnectionImpl
550
550
551
551
private async chooseDevice ( ) : Promise < USBDevice > {
552
552
this . dispatchTypedEvent ( "beforerequestdevice" , new BeforeRequestDevice ( ) ) ;
553
- this . device = await navigator . usb . requestDevice ( {
554
- exclusionFilters : this . exclusionFilters ,
555
- filters : defaultFilters ,
556
- } ) ;
557
- this . dispatchTypedEvent ( "afterrequestdevice" , new AfterRequestDevice ( ) ) ;
553
+ try {
554
+ this . device = await navigator . usb . requestDevice ( {
555
+ exclusionFilters : this . exclusionFilters ,
556
+ filters : defaultFilters ,
557
+ } ) ;
558
+ } finally {
559
+ this . dispatchTypedEvent ( "afterrequestdevice" , new AfterRequestDevice ( ) ) ;
560
+ }
558
561
return this . device ;
559
562
}
560
563
You can’t perform that action at this time.
0 commit comments