11import React from 'react' ;
22
3- import { fireEvent , render , screen } from '@testing-library/react' ;
3+ import {
4+ fireEvent , render , screen , waitFor ,
5+ } from '@testing-library/react' ;
46import { Provider } from 'react-redux' ;
5- import { createStore } from 'redux' ;
7+ import { applyMiddleware , createStore } from 'redux' ;
8+ import thunk from 'redux-thunk' ;
69
710import { IntlProvider } from '@edx/frontend-platform/i18n' ;
811
912import MuteModalManager from './MuteModalManager' ;
1013
11- // Mock the thunks
12- const mockMuteUserThunk = jest . fn ( ( ) => ( { type : 'MUTE_USER' } ) ) ;
13- const mockUnmuteUserThunk = jest . fn ( ( ) => ( { type : 'UNMUTE_USER' } ) ) ;
14- const mockMuteAndReportUserThunk = jest . fn ( ( ) => ( { type : 'MUTE_AND_REPORT_USER' } ) ) ;
14+ const mockMuteUserThunk = jest . fn ( ) ;
15+ const mockUnmuteUserThunk = jest . fn ( ) ;
16+ const mockMuteAndReportUserThunk = jest . fn ( ) ;
17+ const mockFetchMutedUsersThunk = jest . fn ( ) ;
1518
19+ // Mock functions should return Redux action creators that return Promises for async behavior
1620jest . mock ( '../data/thunks' , ( ) => ( {
17- muteUserThunk : mockMuteUserThunk ,
18- unmuteUserThunk : mockUnmuteUserThunk ,
19- muteAndReportUserThunk : mockMuteAndReportUserThunk ,
21+ muteUserThunk : ( ...args ) => ( ) => {
22+ mockMuteUserThunk ( ...args ) ;
23+ return Promise . resolve ( { type : 'MOCK_MUTE_USER' , payload : args } ) ;
24+ } ,
25+ unmuteUserThunk : ( ...args ) => ( ) => {
26+ mockUnmuteUserThunk ( ...args ) ;
27+ return Promise . resolve ( { type : 'MOCK_UNMUTE_USER' , payload : args } ) ;
28+ } ,
29+ muteAndReportUserThunk : ( ...args ) => ( ) => {
30+ mockMuteAndReportUserThunk ( ...args ) ;
31+ return Promise . resolve ( { type : 'MOCK_MUTE_AND_REPORT_USER' , payload : args } ) ;
32+ } ,
33+ fetchMutedUsersThunk : ( ...args ) => ( ) => {
34+ mockFetchMutedUsersThunk ( ...args ) ;
35+ return Promise . resolve ( { type : 'MOCK_FETCH_MUTED_USERS' , payload : args } ) ;
36+ } ,
37+ } ) ) ;
38+
39+ // Mock posts thunks to prevent logError issues
40+ jest . mock ( '../posts/data/thunks' , ( ) => ( {
41+ fetchThreads : ( ) => ( ) => Promise . resolve ( { type : 'MOCK_FETCH_THREADS' } ) ,
2042} ) ) ;
2143
2244const mockStore = createStore ( ( ) => ( {
@@ -26,36 +48,43 @@ const mockStore = createStore(() => ({
2648 userIsStaff : true ,
2749 userHasModerationPrivileges : true ,
2850 } ,
29- } ) ) ;
51+ } ) , applyMiddleware ( thunk ) ) ;
3052
3153const mockMessages = {
3254 learnerMuteTitle : {
3355 id : 'test.learnerMuteTitle' ,
3456 defaultMessage : 'Mute this user?' ,
57+ description : 'Title for learner mute modal' ,
3558 } ,
3659 learnerMuteDescription : {
3760 id : 'test.learnerMuteDescription' ,
3861 defaultMessage : 'Are you sure you want to mute {username}?' ,
62+ description : 'Description for learner mute modal' ,
3963 } ,
4064 learnerMuteButton : {
4165 id : 'test.learnerMuteButton' ,
4266 defaultMessage : 'Mute' ,
67+ description : 'Button text for mute action' ,
4368 } ,
4469 learnerMuteAndReportButton : {
4570 id : 'test.learnerMuteAndReportButton' ,
4671 defaultMessage : 'Mute and report' ,
72+ description : 'Button text for mute and report action' ,
4773 } ,
4874 unmuteTitle : {
4975 id : 'test.unmuteTitle' ,
5076 defaultMessage : 'Unmute this user?' ,
77+ description : 'Title for unmute modal' ,
5178 } ,
5279 unmuteDescription : {
5380 id : 'test.unmuteDescription' ,
5481 defaultMessage : 'Are you sure you want to unmute {username}?' ,
82+ description : 'Description for unmute modal' ,
5583 } ,
5684 unmuteButton : {
5785 id : 'test.unmuteButton' ,
5886 defaultMessage : 'Unmute' ,
87+ description : 'Button text for unmute action' ,
5988 } ,
6089} ;
6190
@@ -112,7 +141,7 @@ describe('MuteModalManager', () => {
112141 expect ( mockProps . onCloseLearnerMuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
113142 } ) ;
114143
115- it ( 'handles learner mute action correctly' , ( ) => {
144+ it ( 'handles learner mute action correctly' , async ( ) => {
116145 renderWithProvider (
117146 < MuteModalManager
118147 { ...mockProps }
@@ -122,10 +151,13 @@ describe('MuteModalManager', () => {
122151
123152 fireEvent . click ( screen . getByRole ( 'button' , { name : 'Mute' } ) ) ;
124153 expect ( mockMuteUserThunk ) . toHaveBeenCalledWith ( 'testuser' , false ) ;
125- expect ( mockProps . onCloseLearnerMuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
154+
155+ await waitFor ( ( ) => {
156+ expect ( mockProps . onCloseLearnerMuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
157+ } ) ;
126158 } ) ;
127159
128- it ( 'handles learner mute and report action correctly' , ( ) => {
160+ it ( 'handles learner mute and report action correctly' , async ( ) => {
129161 renderWithProvider (
130162 < MuteModalManager
131163 { ...mockProps }
@@ -135,7 +167,10 @@ describe('MuteModalManager', () => {
135167
136168 fireEvent . click ( screen . getByRole ( 'button' , { name : 'Mute and report' } ) ) ;
137169 expect ( mockMuteAndReportUserThunk ) . toHaveBeenCalledWith ( 'testuser' , 'test-content-id' ) ;
138- expect ( mockProps . onCloseLearnerMuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
170+
171+ await waitFor ( ( ) => {
172+ expect ( mockProps . onCloseLearnerMuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
173+ } ) ;
139174 } ) ;
140175 } ) ;
141176
@@ -200,7 +235,7 @@ describe('MuteModalManager', () => {
200235 expect ( mockProps . onCloseUnmuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
201236 } ) ;
202237
203- it ( 'handles unmute action correctly' , ( ) => {
238+ it ( 'handles unmute action correctly' , async ( ) => {
204239 renderWithProvider (
205240 < MuteModalManager
206241 { ...mockProps }
@@ -210,7 +245,10 @@ describe('MuteModalManager', () => {
210245
211246 fireEvent . click ( screen . getByRole ( 'button' , { name : 'Unmute' } ) ) ;
212247 expect ( mockUnmuteUserThunk ) . toHaveBeenCalledWith ( 'testuser' , false ) ;
213- expect ( mockProps . onCloseUnmuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
248+
249+ await waitFor ( ( ) => {
250+ expect ( mockProps . onCloseUnmuteModal ) . toHaveBeenCalledTimes ( 1 ) ;
251+ } ) ;
214252 } ) ;
215253 } ) ;
216254
0 commit comments