1
1
import React from 'react' ;
2
2
import { unmountComponentAtNode } from 'react-dom' ;
3
3
import { act } from 'react-dom/test-utils' ;
4
- import { fireEvent , render , screen , waitFor } from '../../../../test-utils' ;
4
+ import { fireEvent , render , screen } from '../../../../test-utils' ;
5
5
import Preferences from './index' ;
6
6
7
7
/* props to pass in:
@@ -57,19 +57,17 @@ const renderComponent = (extraProps = {}, container) => {
57
57
setSoundOutput : jest . fn ( ) ,
58
58
...extraProps
59
59
} ;
60
- render ( < Preferences { ...props } /> , container ) ;
60
+ render ( < Preferences { ...props } /> , { container } ) ;
61
61
62
62
return props ;
63
63
} ;
64
64
65
- // TODOS
66
- // do I need to think about the component functions? like increaseFontSize?
67
- // is that possible?
68
65
describe ( '<Preferences />' , ( ) => {
69
66
let container = null ;
70
67
beforeEach ( ( ) => {
71
68
// setup a DOM element as a render target
72
69
container = document . createElement ( 'div' ) ;
70
+ container . classList . add ( 'testing-container' ) ;
73
71
document . body . appendChild ( container ) ;
74
72
} ) ;
75
73
@@ -82,98 +80,104 @@ describe('<Preferences />', () => {
82
80
83
81
describe ( 'font tests' , ( ) => {
84
82
it ( 'font size increase button says increase' , ( ) => {
85
- let props ;
86
- // render the component with autosave set to false as default
83
+ // render the component
87
84
act ( ( ) => {
88
- props = renderComponent ( { fontSize : 12 } , container ) ;
85
+ renderComponent ( { } , container ) ;
89
86
} ) ;
90
87
91
- // get ahold of the radio buttons for toggling autosave
88
+ // get ahold of the button for increasing text size
92
89
const fontPlusButton = screen . getByTestId ( 'font-plus-button' ) ;
93
90
94
- // make button says "Increase"
91
+ // check that button says says "Increase"
95
92
expect ( fontPlusButton . textContent . toLowerCase ( ) ) . toBe ( 'increase' ) ;
96
93
} ) ;
97
94
98
95
it ( 'increase font size by 2 when clicking plus button' , ( ) => {
99
96
let props ;
100
- // render the component with autosave set to false as default
97
+ // render the component with font size set to 12
101
98
act ( ( ) => {
102
99
props = renderComponent ( { fontSize : 12 } , container ) ;
103
100
} ) ;
104
101
105
- // get ahold of the radio buttons for toggling autosave
102
+ // get ahold of the button for increasing text size
106
103
const fontPlusButton = screen . getByTestId ( 'font-plus-button' ) ;
107
104
105
+ // click the button
108
106
act ( ( ) => {
109
107
fireEvent . click ( fontPlusButton ) ;
110
108
} ) ;
111
109
110
+ // expect that setFontSize has been called once with the argument 14
112
111
expect ( props . setFontSize ) . toHaveBeenCalledTimes ( 1 ) ;
113
112
expect ( props . setFontSize . mock . calls [ 0 ] [ 0 ] ) . toBe ( 14 ) ;
114
113
} ) ;
115
114
116
115
it ( 'font size decrease button says decrease' , ( ) => {
117
- let props ;
118
- // render the component with autosave set to false as default
116
+ // render the component with font size set to 12
119
117
act ( ( ) => {
120
- props = renderComponent ( { fontSize : 12 } , container ) ;
118
+ renderComponent ( { fontSize : 12 } , container ) ;
121
119
} ) ;
122
120
123
- // get ahold of the radio buttons for toggling autosave
121
+ // get ahold of the button for decreasing font size
124
122
const fontPlusButton = screen . getByTestId ( 'font-minus-button' ) ;
125
123
126
- // make button says "decrease"
124
+ // check that button says "decrease"
127
125
expect ( fontPlusButton . textContent . toLowerCase ( ) ) . toBe ( 'decrease' ) ;
128
126
} ) ;
129
127
130
128
it ( 'decrease font size by 2 when clicking minus button' , ( ) => {
131
129
let props ;
132
- // render the component with autosave set to false as default
130
+ // render the component with font size set to 12
133
131
act ( ( ) => {
134
132
props = renderComponent ( { fontSize : 12 } , container ) ;
135
133
} ) ;
136
134
137
- // get ahold of the radio buttons for toggling autosave
135
+ // get ahold of the button for decreasing text size
138
136
const fontMinusButton = screen . getByTestId ( 'font-minus-button' ) ;
139
137
138
+ // click it
140
139
act ( ( ) => {
141
140
fireEvent . click ( fontMinusButton ) ;
142
141
} ) ;
143
142
143
+ // expect that setFontSize would have been called once with argument 10
144
144
expect ( props . setFontSize ) . toHaveBeenCalledTimes ( 1 ) ;
145
145
expect ( props . setFontSize . mock . calls [ 0 ] [ 0 ] ) . toBe ( 10 ) ;
146
146
} ) ;
147
147
148
148
it ( 'font text field changes on manual text input' , ( ) => {
149
149
let props ;
150
- // render the component with autosave set to false as default
150
+ // render the component with font size set to 12
151
151
act ( ( ) => {
152
152
props = renderComponent ( { fontSize : 12 } , container ) ;
153
153
} ) ;
154
154
155
- // get ahold of the radio buttons for toggling autosave
155
+ // get ahold of the text field
156
156
const input = screen . getByTestId ( 'font-size-text-field' ) ;
157
157
158
+ // change input to 24
158
159
act ( ( ) => {
159
160
fireEvent . change ( input , { target : { value : '24' } } ) ;
160
161
} ) ;
162
+
163
+ // submit form
161
164
act ( ( ) => {
162
165
fireEvent . submit ( screen . getByTestId ( 'font-size-form' ) ) ;
163
166
} ) ;
164
167
168
+ // expect that setFontSize was called once with 24
165
169
expect ( props . setFontSize ) . toHaveBeenCalledTimes ( 1 ) ;
166
170
expect ( props . setFontSize . mock . calls [ 0 ] [ 0 ] ) . toBe ( 24 ) ;
167
171
} ) ;
168
172
169
173
it ( 'font size CAN NOT go over 36' , ( ) => {
170
174
let props ;
171
- // render the component with autosave set to false as default
175
+ // render the component
172
176
act ( ( ) => {
173
177
props = renderComponent ( { fontSize : 12 } , container ) ;
174
178
} ) ;
175
179
176
- // get ahold of the radio buttons for toggling autosave
180
+ // get ahold of the text field
177
181
const input = screen . getByTestId ( 'font-size-text-field' ) ;
178
182
179
183
act ( ( ) => {
@@ -192,12 +196,12 @@ describe('<Preferences />', () => {
192
196
193
197
it ( 'font size CAN NOT go under 8' , ( ) => {
194
198
let props ;
195
- // render the component with autosave set to false as default
199
+ // render the component
196
200
act ( ( ) => {
197
201
props = renderComponent ( { fontSize : 12 } , container ) ;
198
202
} ) ;
199
203
200
- // get ahold of the radio buttons for toggling autosave
204
+ // get ahold of the text field
201
205
const input = screen . getByTestId ( 'font-size-text-field' ) ;
202
206
203
207
act ( ( ) => {
@@ -218,38 +222,39 @@ describe('<Preferences />', () => {
218
222
// h and then i, but it tests the same idea
219
223
it ( 'font size input field does NOT take non-integers' , ( ) => {
220
224
let props ;
221
- // render the component with autosave set to false as default
225
+ // render the component
222
226
act ( ( ) => {
223
227
props = renderComponent ( { fontSize : 12 } , container ) ;
224
228
} ) ;
225
229
226
- // get ahold of the radio buttons for toggling autosave
230
+ // get ahold of the text field
227
231
const input = screen . getByTestId ( 'font-size-text-field' ) ;
228
232
229
233
act ( ( ) => {
230
234
fireEvent . change ( input , { target : { value : 'hi' } } ) ;
231
235
} ) ;
232
236
237
+ // it shouldnt have changed at all
233
238
expect ( input . value ) . toBe ( '12' ) ;
234
239
240
+ // we hit submit
235
241
act ( ( ) => {
236
242
fireEvent . submit ( screen . getByTestId ( 'font-size-form' ) ) ;
237
243
} ) ;
238
244
245
+ // it still sets the font size but it's still 12
239
246
expect ( props . setFontSize ) . toHaveBeenCalledTimes ( 1 ) ;
240
247
expect ( props . setFontSize . mock . calls [ 0 ] [ 0 ] ) . toBe ( 12 ) ;
241
248
} ) ;
242
249
243
- // this case is a bit synthetic because we wouldn't be able to type
244
- // h and then i, but it tests the same idea
245
250
it ( 'font size input field does NOT take "-"' , ( ) => {
246
251
let props ;
247
- // render the component with autosave set to false as default
252
+ // render the component
248
253
act ( ( ) => {
249
254
props = renderComponent ( { fontSize : 12 } , container ) ;
250
255
} ) ;
251
256
252
- // get ahold of the radio buttons for toggling autosave
257
+ // get ahold of the text field
253
258
const input = screen . getByTestId ( 'font-size-text-field' ) ;
254
259
255
260
act ( ( ) => {
@@ -277,7 +282,7 @@ describe('<Preferences />', () => {
277
282
expect ( checkedRadio . checked ) . toBe ( true ) ;
278
283
expect ( uncheckedRadio . checked ) . toBe ( false ) ;
279
284
280
- // click om the one already selected, the false one
285
+ // click on the one already selected
281
286
act ( ( ) => {
282
287
fireEvent . click ( checkedRadio ) ;
283
288
} ) ;
@@ -293,7 +298,7 @@ describe('<Preferences />', () => {
293
298
fireEvent . click ( uncheckedRadio ) ;
294
299
} ) ;
295
300
296
- // expect that the setAutosave function was called with the value true
301
+ // expect that the setter function was called with the value true
297
302
expect ( setter ) . toHaveBeenCalledTimes ( 1 ) ;
298
303
expect ( setter . mock . calls [ 0 ] [ 0 ] ) . toBe ( setterExpectedArgument ) ;
299
304
} ;
@@ -466,10 +471,9 @@ describe('<Preferences />', () => {
466
471
467
472
describe ( 'can toggle between general settings and accessibility tabs successfully' , ( ) => {
468
473
it ( 'can toggle sucessfully' , ( ) => {
469
- let props ;
470
474
// render the component with lineNumbers prop set to false
471
475
act ( ( ) => {
472
- props = renderComponent ( { } , container ) ;
476
+ renderComponent ( { } , container ) ;
473
477
} ) ;
474
478
475
479
// switch to accessibility
@@ -656,9 +660,8 @@ describe('<Preferences />', () => {
656
660
} ) ;
657
661
658
662
it ( 'multiple checkboxes can be selected' , ( ) => {
659
- let props ;
660
663
act ( ( ) => {
661
- props = renderComponent (
664
+ renderComponent (
662
665
{ textOutput : true , soundOutput : true , gridOutput : true } ,
663
666
container
664
667
) ;
@@ -679,9 +682,8 @@ describe('<Preferences />', () => {
679
682
} ) ;
680
683
681
684
it ( 'none of the checkboxes can be selected' , ( ) => {
682
- let props ;
683
685
act ( ( ) => {
684
- props = renderComponent (
686
+ renderComponent (
685
687
{ textOutput : false , soundOutput : false , gridOutput : false } ,
686
688
container
687
689
) ;
0 commit comments