@@ -4,21 +4,30 @@ import { render as rtlRender, screen, fireEvent } from '@testing-library/react';
4
4
import '@testing-library/jest-dom/extend-expect' ; // needed this to extend the jest-dom assertions (ex toHaveTextContent)
5
5
import Action from '../components/Action' ;
6
6
import { changeView , changeSlider } from '../RTKslices' ;
7
- import { Provider , useDispatch } from 'react-redux' ;
7
+ import { Provider } from 'react-redux' ;
8
8
import { store } from '../RTKstore' ; //importing store for testing to give us access to Redux Store we configured
9
-
9
+ import * as reactRedux from 'react-redux'
10
10
11
11
// @ts -ignore
12
12
Action . cleanTime = jest . fn ( ) . mockReturnValue ( ) ;
13
13
14
+ jest . mock ( 'react-redux' , ( ) => ( {
15
+ ...jest . requireActual ( 'react-redux' ) , // Use the actual react-redux module except for the functions you want to mock
16
+ useDispatch : jest . fn ( ) ,
17
+ } ) ) ;
18
+
14
19
const render = component => rtlRender (
15
20
< Provider store = { store } >
16
21
{ component }
17
22
</ Provider >
18
23
)
19
24
20
-
21
25
describe ( 'Unit testing for Action.tsx' , ( ) => {
26
+ // const useSelectorMock = jest.spyOn(reactRedux, 'useSelector')
27
+ // const useDispatchMock = jest.spyOn(reactRedux, 'useDispatch')
28
+ const useDispatchMock = reactRedux . useDispatch as jest . Mock ;
29
+ const dummyDispatch = jest . fn ( ) ;
30
+ useDispatchMock . mockReturnValue ( dummyDispatch ) ;
22
31
const props = {
23
32
key : 'actions2' ,
24
33
selected : true ,
@@ -27,7 +36,7 @@ describe('Unit testing for Action.tsx', () => {
27
36
sliderIndex : 2 ,
28
37
isCurrIndex : false ,
29
38
routePath : '' ,
30
- dispatch : jest . fn ( ) ,
39
+ // dispatch: jest.fn(),
31
40
displayName : '3.0' ,
32
41
componentName : 'App' ,
33
42
logChangedState : jest . fn ( ) ,
@@ -42,89 +51,93 @@ describe('Unit testing for Action.tsx', () => {
42
51
beforeEach ( ( ) => {
43
52
props . isCurrIndex = false ;
44
53
props . componentData = { actualDuration : 3.5 } ;
45
- props . dispatch . mockClear ( ) ;
54
+ // props.dispatch.mockClear();
55
+ // useSelectorMock.mockClear()
56
+ // useDispatchMock.mockClear()
46
57
} ) ;
47
58
48
59
describe ( 'When a component is shown on the page' , ( ) => {
49
- // test('Action snapshot should be shown as Snapshot: 3.0', () => {
50
- // render(
51
- // <Action {...props} />
52
- // );
53
- // expect(screen.getByPlaceholderText('Snapshot: 3.0')).toBeInTheDocument();
54
- // });
55
-
56
- // test('Two buttons with Time and Current when not at current snapshot', () => {
57
- // props.isCurrIndex = true;
58
- // render(
59
- // <Action {...props} />
60
- // );
61
- // expect(screen.getAllByRole('button')).toHaveLength(2);
62
- // expect(screen.getAllByRole('button')[0]).toHaveTextContent('+00:03.50');
63
- // expect(screen.getAllByRole('button')[1]).toHaveTextContent('Current');
64
- // });
65
-
66
- // test('Action snapshot should be shown as Snapshot: 3.0', () => {
67
- // render(
68
- // <Action {...props} />
69
- // );
70
- // expect(screen.getByPlaceholderText('Snapshot: 3.0')).toBeInTheDocument();
71
- // });
72
-
73
- // test("When there is no duration data", () => {
74
- // props.componentData = undefined;
75
- // render(
76
- // <Action {...props} />
77
- // );
78
- // expect(screen.getAllByRole('button')[0]).toHaveTextContent('NO TIME');
79
- // });
80
-
81
- // test('When actualDuration exceeds 60, time should be formatted correctly', () => {
82
- // props.componentData.actualDuration = 75;
83
- // render(
84
- // <Action {...props} />
85
- // );
86
- // expect(screen.getAllByRole('button')[0]).toHaveTextContent('+01:15.00');
87
- // });
88
-
89
- // test('Using the ArrowUp key on Action snapshot should trigger handleOnKeyDown', () => {
90
- // render(
91
- // <Action {...props} />
92
- // );
93
- // fireEvent.keyDown(screen.getByRole('presentation'), {key: 'ArrowUp', code: 'ArrowUp', charCode: 38});
94
- // expect(props.handleOnkeyDown).toHaveBeenCalled();
95
- // });
96
-
97
- // test('Using the ArrowDown key on Action snapshot should trigger handleOnKeyDown', () => {
98
- // render(
99
- // <Action {...props} />
100
- // );
101
- // fireEvent.keyDown(screen.getByRole('presentation'), {key: 'ArrowDown', code: 'ArrowDown', charCode: 40});
102
- // expect(props.handleOnkeyDown).toHaveBeenCalled();
103
- // });
104
-
105
- // test('Using the Enter key on Action snapshot should trigger handleOnKeyDown', () => {
106
- // render(
107
- // <Action {...props} />
108
- // );
109
- // fireEvent.keyDown(screen.getByRole('presentation'), {key: 'Enter', code: 'Enter', charCode: 13});
110
- // expect(props.handleOnkeyDown).toHaveBeenCalled();
111
- // });
112
-
113
- // test('Clicking the snapshot should trigger onClick', () => {
114
- // render(
115
- // <Action {...props} />
116
- // )
117
- // fireEvent.click(screen.getByRole('presentation'));
118
- // expect(props.dispatch ).toHaveBeenCalledWith(changeView(props.index));;
119
- // });
60
+ test ( 'Action snapshot should be shown as Snapshot: 3.0' , ( ) => {
61
+ render (
62
+ < Action { ...props } />
63
+ ) ;
64
+ expect ( screen . getByPlaceholderText ( 'Snapshot: 3.0' ) ) . toBeInTheDocument ( ) ;
65
+ } ) ;
66
+
67
+ test ( 'Two buttons with Time and Current when not at current snapshot' , ( ) => {
68
+ props . isCurrIndex = true ;
69
+ render (
70
+ < Action { ...props } />
71
+ ) ;
72
+ expect ( screen . getAllByRole ( 'button' ) ) . toHaveLength ( 2 ) ;
73
+ expect ( screen . getAllByRole ( 'button' ) [ 0 ] ) . toHaveTextContent ( '+00:03.50' ) ;
74
+ expect ( screen . getAllByRole ( 'button' ) [ 1 ] ) . toHaveTextContent ( 'Current' ) ;
75
+ } ) ;
76
+
77
+ test ( 'Action snapshot should be shown as Snapshot: 3.0' , ( ) => {
78
+ render (
79
+ < Action { ...props } />
80
+ ) ;
81
+ expect ( screen . getByPlaceholderText ( 'Snapshot: 3.0' ) ) . toBeInTheDocument ( ) ;
82
+ } ) ;
83
+
84
+ test ( "When there is no duration data" , ( ) => {
85
+ props . componentData = undefined ;
86
+ render (
87
+ < Action { ...props } />
88
+ ) ;
89
+ expect ( screen . getAllByRole ( 'button' ) [ 0 ] ) . toHaveTextContent ( 'NO TIME' ) ;
90
+ } ) ;
91
+
92
+ test ( 'When actualDuration exceeds 60, time should be formatted correctly' , ( ) => {
93
+ props . componentData . actualDuration = 75 ;
94
+ render (
95
+ < Action { ...props } />
96
+ ) ;
97
+ expect ( screen . getAllByRole ( 'button' ) [ 0 ] ) . toHaveTextContent ( '+01:15.00' ) ;
98
+ } ) ;
99
+
100
+ test ( 'Using the ArrowUp key on Action snapshot should trigger handleOnKeyDown' , ( ) => {
101
+ render (
102
+ < Action { ...props } />
103
+ ) ;
104
+ fireEvent . keyDown ( screen . getByRole ( 'presentation' ) , { key : 'ArrowUp' , code : 'ArrowUp' , charCode : 38 } ) ;
105
+ expect ( props . handleOnkeyDown ) . toHaveBeenCalled ( ) ;
106
+ } ) ;
107
+
108
+ test ( 'Using the ArrowDown key on Action snapshot should trigger handleOnKeyDown' , ( ) => {
109
+ render (
110
+ < Action { ...props } />
111
+ ) ;
112
+ fireEvent . keyDown ( screen . getByRole ( 'presentation' ) , { key : 'ArrowDown' , code : 'ArrowDown' , charCode : 40 } ) ;
113
+ expect ( props . handleOnkeyDown ) . toHaveBeenCalled ( ) ;
114
+ } ) ;
115
+
116
+ test ( 'Using the Enter key on Action snapshot should trigger handleOnKeyDown' , ( ) => {
117
+ render (
118
+ < Action { ...props } />
119
+ ) ;
120
+ fireEvent . keyDown ( screen . getByRole ( 'presentation' ) , { key : 'Enter' , code : 'Enter' , charCode : 13 } ) ;
121
+ expect ( props . handleOnkeyDown ) . toHaveBeenCalled ( ) ;
122
+ } ) ;
123
+
124
+ test ( 'Clicking the snapshot should trigger onClick' , ( ) => {
125
+ render (
126
+ < Action { ...props } />
127
+ )
128
+ fireEvent . click ( screen . getByRole ( 'presentation' ) ) ;
129
+ expect ( dummyDispatch ) . toHaveBeenCalledWith ( changeView ( props . index ) ) ; ;
130
+ } ) ;
120
131
121
132
test ( 'Clicking Jump button should trigger changeSlider and changeView' , ( ) => {
133
+ // const dummyDispatch = jest.fn();
134
+ // useDispatchMock.mockReturnValue(dummyDispatch);
122
135
render (
123
136
< Action { ...props } />
124
137
) ;
125
138
fireEvent . click ( screen . getAllByRole ( 'button' ) [ 1 ] ) ;
126
- expect ( props . dispatch ) . toHaveBeenCalledWith ( changeSlider ( props . index ) ) ;
127
- expect ( props . dispatch ) . toHaveBeenCalledWith ( changeView ( props . index ) ) ;
139
+ expect ( dummyDispatch ) . toHaveBeenCalledWith ( changeSlider ( props . index ) ) ;
140
+ expect ( dummyDispatch ) . toHaveBeenCalledWith ( changeView ( props . index ) ) ;
128
141
} ) ;
129
142
} ) ;
130
143
} ) ;
0 commit comments