1
1
import { shallow } from 'enzyme' ;
2
2
import { noop } from 'lodash' ;
3
+ import { mockWindowMatchMedia , mockDomReset } from 'test-utils/mockDom' ;
3
4
import ModuleFinderPagerButton from 'views/modules/ModuleFinderPagerButton' ;
4
- import { displayPageRange , ModuleFinderPagerComponent } from './ModuleFinderPager' ;
5
+ import ModuleFinderPager , { displayPageRange } from './ModuleFinderPager' ;
5
6
6
7
describe ( displayPageRange , ( ) => {
7
8
test ( 'calculate page range correctly' , ( ) => {
@@ -33,9 +34,17 @@ describe(displayPageRange, () => {
33
34
} ) ;
34
35
} ) ;
35
36
36
- describe ( ModuleFinderPagerComponent , ( ) => {
37
- const DESKTOP = false ;
38
- const MOBILE = true ;
37
+ const DESKTOP = false ;
38
+ const MOBILE = true ;
39
+
40
+ describe ( 'ModuleFinderPager' , ( ) => {
41
+ beforeAll ( ( ) => {
42
+ mockWindowMatchMedia ( ) ;
43
+ } ) ;
44
+
45
+ afterAll ( ( ) => {
46
+ mockDomReset ( ) ;
47
+ } ) ;
39
48
40
49
const defaultProps = {
41
50
selectedPage : 1 ,
@@ -45,66 +54,73 @@ describe(ModuleFinderPagerComponent, () => {
45
54
onGoToPage : noop ,
46
55
onGoToNext : noop ,
47
56
onGoToLast : noop ,
48
- matchBreakpoint : DESKTOP ,
49
57
} ;
50
58
51
59
test ( 'should not render if totalNumPages <= 0' , ( ) => {
52
- const zeroPagesPager = shallow (
53
- < ModuleFinderPagerComponent { ...defaultProps } totalNumPages = { 0 } /> ,
54
- ) ;
60
+ const zeroPagesPager = shallow ( < ModuleFinderPager { ...defaultProps } totalNumPages = { 0 } /> ) ;
55
61
expect ( zeroPagesPager . type ( ) ) . toEqual ( null ) ;
56
62
57
- const negativePagesPager = shallow (
58
- < ModuleFinderPagerComponent { ...defaultProps } totalNumPages = { - 1 } /> ,
59
- ) ;
63
+ const negativePagesPager = shallow ( < ModuleFinderPager { ...defaultProps } totalNumPages = { - 1 } /> ) ;
60
64
expect ( negativePagesPager . type ( ) ) . toEqual ( null ) ;
61
65
} ) ;
62
66
63
- test ( 'should contain pager buttons' , ( ) => {
64
- const onDesktop = shallow ( < ModuleFinderPagerComponent { ...defaultProps } /> ) ;
65
- expect ( onDesktop . find ( ModuleFinderPagerButton ) ) . toHaveLength ( 5 ) ;
66
-
67
- const onMobile = shallow ( < ModuleFinderPagerComponent { ...defaultProps } matchBreakpoint /> ) ;
68
- expect ( onMobile . find ( ModuleFinderPagerButton ) ) . toHaveLength ( 4 ) ;
69
- } ) ;
70
-
71
- test ( 'should respond to clicks on buttons on desktop' , ( ) => {
72
- const props = {
73
- ...defaultProps ,
74
- onGoToFirst : jest . fn ( ) ,
75
- onGoToPrevious : jest . fn ( ) ,
76
- onGoToPage : jest . fn ( ) ,
77
- onGoToNext : jest . fn ( ) ,
78
- onGoToLast : jest . fn ( ) ,
79
- matchBreakpoint : DESKTOP ,
80
- } ;
81
-
82
- const actual = shallow ( < ModuleFinderPagerComponent { ...props } /> ) ;
83
- actual . find ( ModuleFinderPagerButton ) . forEach ( ( n ) => n . simulate ( 'click' ) ) ;
84
- expect ( props . onGoToFirst ) . toHaveBeenCalled ( ) ;
85
- expect ( props . onGoToPrevious ) . toHaveBeenCalled ( ) ;
86
- expect ( props . onGoToPage ) . toHaveBeenCalledWith ( 1 ) ;
87
- expect ( props . onGoToNext ) . toHaveBeenCalled ( ) ;
88
- expect ( props . onGoToLast ) . toHaveBeenCalled ( ) ;
67
+ describe ( 'when on desktop' , ( ) => {
68
+ beforeAll ( ( ) => {
69
+ mockWindowMatchMedia ( { matches : DESKTOP } ) ;
70
+ } ) ;
71
+
72
+ test ( 'should contain pager buttons' , ( ) => {
73
+ const onDesktop = shallow ( < ModuleFinderPager { ...defaultProps } /> ) ;
74
+ expect ( onDesktop . find ( ModuleFinderPagerButton ) ) . toHaveLength ( 5 ) ;
75
+ } ) ;
76
+
77
+ test ( 'should respond to clicks on buttons' , ( ) => {
78
+ const props = {
79
+ ...defaultProps ,
80
+ onGoToFirst : jest . fn ( ) ,
81
+ onGoToPrevious : jest . fn ( ) ,
82
+ onGoToPage : jest . fn ( ) ,
83
+ onGoToNext : jest . fn ( ) ,
84
+ onGoToLast : jest . fn ( ) ,
85
+ } ;
86
+
87
+ const actual = shallow ( < ModuleFinderPager { ...props } /> ) ;
88
+ actual . find ( ModuleFinderPagerButton ) . forEach ( ( n ) => n . simulate ( 'click' ) ) ;
89
+ expect ( props . onGoToFirst ) . toHaveBeenCalled ( ) ;
90
+ expect ( props . onGoToPrevious ) . toHaveBeenCalled ( ) ;
91
+ expect ( props . onGoToPage ) . toHaveBeenCalledWith ( 1 ) ;
92
+ expect ( props . onGoToNext ) . toHaveBeenCalled ( ) ;
93
+ expect ( props . onGoToLast ) . toHaveBeenCalled ( ) ;
94
+ } ) ;
89
95
} ) ;
90
96
91
- test ( 'should respond to clicks on buttons on mobile' , ( ) => {
92
- const props = {
93
- ...defaultProps ,
94
- onGoToFirst : jest . fn ( ) ,
95
- onGoToPrevious : jest . fn ( ) ,
96
- onGoToPage : jest . fn ( ) ,
97
- onGoToNext : jest . fn ( ) ,
98
- onGoToLast : jest . fn ( ) ,
99
- matchBreakpoint : MOBILE ,
100
- } ;
101
-
102
- const actual = shallow ( < ModuleFinderPagerComponent { ...props } /> ) ;
103
- actual . find ( ModuleFinderPagerButton ) . forEach ( ( n ) => n . simulate ( 'click' ) ) ;
104
- expect ( props . onGoToFirst ) . toHaveBeenCalled ( ) ;
105
- expect ( props . onGoToPrevious ) . toHaveBeenCalled ( ) ;
106
- expect ( props . onGoToPage ) . not . toHaveBeenCalled ( ) ; // No page buttons
107
- expect ( props . onGoToNext ) . toHaveBeenCalled ( ) ;
108
- expect ( props . onGoToLast ) . toHaveBeenCalled ( ) ;
97
+ describe ( 'when on mobile' , ( ) => {
98
+ beforeAll ( ( ) => {
99
+ mockWindowMatchMedia ( { matches : MOBILE } ) ;
100
+ } ) ;
101
+
102
+ test ( 'should contain pager buttons' , ( ) => {
103
+ const onMobile = shallow ( < ModuleFinderPager { ...defaultProps } /> ) ;
104
+ expect ( onMobile . find ( ModuleFinderPagerButton ) ) . toHaveLength ( 4 ) ;
105
+ } ) ;
106
+
107
+ test ( 'should respond to clicks on buttons' , ( ) => {
108
+ const props = {
109
+ ...defaultProps ,
110
+ onGoToFirst : jest . fn ( ) ,
111
+ onGoToPrevious : jest . fn ( ) ,
112
+ onGoToPage : jest . fn ( ) ,
113
+ onGoToNext : jest . fn ( ) ,
114
+ onGoToLast : jest . fn ( ) ,
115
+ } ;
116
+
117
+ const actual = shallow ( < ModuleFinderPager { ...props } /> ) ;
118
+ actual . find ( ModuleFinderPagerButton ) . forEach ( ( n ) => n . simulate ( 'click' ) ) ;
119
+ expect ( props . onGoToFirst ) . toHaveBeenCalled ( ) ;
120
+ expect ( props . onGoToPrevious ) . toHaveBeenCalled ( ) ;
121
+ expect ( props . onGoToPage ) . not . toHaveBeenCalled ( ) ; // No page buttons
122
+ expect ( props . onGoToNext ) . toHaveBeenCalled ( ) ;
123
+ expect ( props . onGoToLast ) . toHaveBeenCalled ( ) ;
124
+ } ) ;
109
125
} ) ;
110
126
} ) ;
0 commit comments