11/* eslint-disable jest/expect-expect */
2- import type { MockzillaDeep } from 'mockzilla' ;
3- import 'mockzilla-webextension' ;
2+ /* eslint-disable @typescript-eslint/no-explicit-any */
3+ import 'jest-webextension-mock' ;
4+
5+ const mockBrowser =
6+ ( globalThis as any ) . mockBrowser || ( globalThis as any ) . browser || { } ;
47
58import { getOneOption } from '../options' ;
69
@@ -38,14 +41,6 @@ afterEach(() => {
3841 jest . clearAllMocks ( ) ;
3942} ) ;
4043
41- function mockBrowserSendMessage < T extends keyof BackgroundMessage > ( ) {
42- return mockBrowser . runtime . sendMessage as unknown as MockzillaDeep < {
43- (
44- message : BackgroundMessage [ T ] [ 'message' ] ,
45- ) : Promise < BackgroundMessage [ T ] [ 'response' ] > ;
46- } > ;
47- }
48-
4944describe ( 'messagingClient' , ( ) => {
5045 it ( 'getNotificationsUrl' , async ( ) => {
5146 const url = await getNotificationsUrl ( ) ;
@@ -77,22 +72,26 @@ describe('messagingClient', () => {
7772 const mockTeamsList : BackgroundMessage [ 'UPDATE_TEAMS_LIST' ] [ 'response' ] = {
7873 cs : 'Czech' ,
7974 } ;
80- mockBrowserSendMessage < 'UPDATE_TEAMS_LIST' > ( )
81- . expect ( { type : 'update-teams-list' } )
82- . andResolve ( mockTeamsList ) ;
75+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
76+ mockTeamsList ,
77+ ) ;
8378
8479 const teams = await updateTeamsList ( ) ;
8580
81+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
82+ type : 'update-teams-list' ,
83+ } ) ;
8684 expect ( teams ) . toStrictEqual ( mockTeamsList ) ;
8785 } ) ;
8886
8987 it ( 'getUsersTeamFromPontoon' , async ( ) => {
90- mockBrowserSendMessage < 'GET_TEAM_FROM_PONTOON' > ( )
91- . expect ( { type : 'get-team-from-pontoon' } )
92- . andResolve ( 'cs' ) ;
88+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce ( 'cs' ) ;
9389
9490 const team = await getUsersTeamFromPontoon ( ) ;
9591
92+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
93+ type : 'get-team-from-pontoon' ,
94+ } ) ;
9695 expect ( team ) . toBe ( 'cs' ) ;
9796 } ) ;
9897
@@ -103,68 +102,76 @@ describe('messagingClient', () => {
103102 name : 'Firefox' ,
104103 domains : [ ] ,
105104 } ;
106- mockBrowserSendMessage < 'GET_CURRENT_TAB_PROJECT' > ( )
107- . expect ( { type : 'get-current-tab-project' } )
108- . andResolve ( mockProject ) ;
105+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
106+ mockProject ,
107+ ) ;
109108
110109 const project = await getPontoonProjectForTheCurrentTab ( ) ;
111110
111+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
112+ type : 'get-current-tab-project' ,
113+ } ) ;
112114 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
113115 expect ( project ) . toStrictEqual ( mockProject ) ;
114116 } ) ;
115117
116118 it ( 'pageLoaded' , async ( ) => {
117- mockBrowserSendMessage < 'PAGE_LOADED' > ( )
118- . expect ( {
119- type : 'pontoon-page-loaded' ,
120- documentHTML : '<html></html>' ,
121- } )
122- . andResolve ( ) ;
119+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
120+ undefined ,
121+ ) ;
123122
124123 await pageLoaded ( '<html></html>' ) ;
124+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
125+ type : 'pontoon-page-loaded' ,
126+ documentHTML : '<html></html>' ,
127+ } ) ;
125128 } ) ;
126129
127130 it ( 'markAllNotificationsAsRead' , async ( ) => {
128- mockBrowserSendMessage < 'NOTIFICATIONS_READ' > ( )
129- . expect ( { type : 'notifications-read' } )
130- . andResolve ( ) ;
131+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
132+ undefined ,
133+ ) ;
131134
132135 await markAllNotificationsAsRead ( ) ;
136+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
137+ type : 'notifications-read' ,
138+ } ) ;
133139 } ) ;
134140
135141 it ( 'searchTextInPontoon' , async ( ) => {
136- mockBrowserSendMessage < 'SEARCH_TEXT_IN_PONTOON' > ( )
137- . expect ( {
138- type : 'search-text-in-pontoon' ,
139- text : 'foo bar' ,
140- } )
141- . andResolve ( ) ;
142+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
143+ undefined ,
144+ ) ;
142145
143146 await searchTextInPontoon ( 'foo bar' ) ;
147+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
148+ type : 'search-text-in-pontoon' ,
149+ text : 'foo bar' ,
150+ } ) ;
144151 } ) ;
145152
146153 it ( 'reportTranslatedTextToBugzilla' , async ( ) => {
147- mockBrowserSendMessage < 'REPORT_TRANSLATED_TEXT_TO_BUGZILLA' > ( )
148- . expect ( {
149- type : 'report-translated-text-to-bugzilla' ,
150- text : 'foo bar' ,
151- } )
152- . andResolve ( ) ;
154+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce (
155+ undefined ,
156+ ) ;
153157
154158 await reportTranslatedTextToBugzilla ( 'foo bar' ) ;
159+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
160+ type : 'report-translated-text-to-bugzilla' ,
161+ text : 'foo bar' ,
162+ } ) ;
155163 } ) ;
156164
157165 it ( 'notificationBellIconScriptLoaded' , async ( ) => {
158- mockBrowserSendMessage < 'NOTIFICATIONS_BELL_SCRIPT_LOADED' > ( )
159- . expect ( {
160- type : 'notifications-bell-script-loaded' ,
161- } )
162- . andResolve ( {
163- type : 'enable-notifications-bell-script' ,
164- } ) ;
166+ ( mockBrowser . runtime . sendMessage as jest . Mock ) . mockResolvedValueOnce ( {
167+ type : 'enable-notifications-bell-script' ,
168+ } ) ;
165169
166170 const response = await notificationBellIconScriptLoaded ( ) ;
167171
172+ expect ( mockBrowser . runtime . sendMessage ) . toHaveBeenCalledWith ( {
173+ type : 'notifications-bell-script-loaded' ,
174+ } ) ;
168175 expect ( response ) . toStrictEqual ( {
169176 type : 'enable-notifications-bell-script' ,
170177 } ) ;
0 commit comments