Skip to content

Commit 1bca269

Browse files
authored
refactor: add type safety in utils.test.ts (coder#4091)
This makes a few changes to the typings in site/src/components/GlobalSnackbar/utils.test.ts to more accurately represent the types we're using. It allows us to remove from type assertion and one eslin-disable comment..
1 parent 77acf0c commit 1bca269

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

site/src/components/GlobalSnackbar/utils.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,23 @@ describe("Snackbar", () => {
4848

4949
describe("displaySuccess", () => {
5050
const originalWindowDispatchEvent = window.dispatchEvent
51-
let dispatchEventMock: jest.Mock
51+
type TDispatchEventMock = jest.MockedFunction<(msg: CustomEvent<NotificationMsg>) => boolean>
52+
let dispatchEventMock: TDispatchEventMock
5253

5354
// Helper function to extract the notification event
5455
// that was sent to `dispatchEvent`. This lets us validate
5556
// the contents of the notification event are what we expect.
56-
const extractNotificationEvent = (dispatchEventMock: jest.Mock): NotificationMsg => {
57-
// The jest mock API isn't typesafe - but we know in our usage that
58-
// this will always be a `NotificationMsg`.
59-
57+
const extractNotificationEvent = (dispatchEventMock: TDispatchEventMock): NotificationMsg => {
6058
// calls[0] is the first call made to the mock (this is reset in `beforeEach`)
6159
// calls[0][0] is the first argument of the first call
6260
// calls[0][0].detail is the 'detail' argument passed to the `CustomEvent` -
6361
// this is the `NotificationMsg` object that gets sent to `dispatchEvent`
64-
65-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
66-
return dispatchEventMock.mock.calls[0][0].detail as NotificationMsg
62+
return dispatchEventMock.mock.calls[0][0].detail
6763
}
6864

6965
beforeEach(() => {
7066
dispatchEventMock = jest.fn()
71-
window.dispatchEvent = dispatchEventMock
67+
window.dispatchEvent = dispatchEventMock as unknown as typeof window.dispatchEvent
7268
})
7369

7470
afterEach(() => {

0 commit comments

Comments
 (0)