@@ -6,24 +6,74 @@ import {
6
6
MinutesInput ,
7
7
} from "./Minutes" ;
8
8
import * as ReactTestingLibrary from "@testing-library/react" ;
9
+ import ReactTestUtils from "react-dom/test-utils" ;
10
+ import userEvent from "@testing-library/user-event" ;
9
11
10
12
describe ( "<Minutes />" , ( ) => {
11
13
describe ( "rendering" , ( ) => {
12
- test ( "that input contains accurate value based on value prop" , ( ) => {
14
+ it ( "that input contains accurate value based on value prop" , ( ) => {
13
15
let { getByRole } = ReactTestingLibrary . render ( < Minutes value = { 5 } /> ) ;
14
16
let input = getByRole ( "textbox" ) ;
15
17
expect ( input . value ) . toBe ( String ( 5 ) ) ;
16
18
} ) ;
19
+
20
+ it ( "should increment when the add button is clicked" , ( ) => {
21
+ let { getByRole, getByLabelText } = ReactTestingLibrary . render (
22
+ < MinutesWithState />
23
+ ) ;
24
+ let input = getByRole ( "textbox" ) ;
25
+ let addButton = getByLabelText ( "Add" ) ;
26
+ userEvent . click ( addButton ) ;
27
+ expect ( input . value ) . toBe ( String ( 6 ) ) ;
28
+ } ) ;
29
+
30
+ it ( "should decrement when the subtract button is clicked" , ( ) => {
31
+ let { getByRole, getByLabelText } = ReactTestingLibrary . render (
32
+ < MinutesWithState />
33
+ ) ;
34
+ let input = getByRole ( "textbox" ) ;
35
+ let subtractButton = getByLabelText ( "Subtract" ) ;
36
+ userEvent . click ( subtractButton ) ;
37
+ expect ( input . value ) . toBe ( String ( 4 ) ) ;
38
+ } ) ;
39
+
40
+ describe ( "if the value is at 0" , ( ) => {
41
+ it ( "should not decrement when the subtract button is clicked" , ( ) => {
42
+ let { getByRole, getByLabelText } = ReactTestingLibrary . render (
43
+ < MinutesWithState defaultValue = { 0 } />
44
+ ) ;
45
+ let input = getByRole ( "textbox" ) ;
46
+ let subtractButton = getByLabelText ( "Subtract" ) ;
47
+ userEvent . click ( subtractButton ) ;
48
+ expect ( input . value ) . toBe ( String ( 0 ) ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ it ( "should update when the user types a valid value" , ( ) => {
53
+ let { getByRole } = ReactTestingLibrary . render (
54
+ < MinutesWithState defaultValue = { 5 } />
55
+ ) ;
56
+ let input = getByRole ( "textbox" ) ;
57
+
58
+ userEvent . type ( input , "{backspace}" ) ;
59
+ userEvent . type ( input , "10" ) ;
60
+
61
+ expect ( input . value ) . toBe ( String ( 10 ) ) ;
62
+ } ) ;
17
63
} ) ;
18
64
} ) ;
19
65
20
66
function Minutes ( { "aria-labelledby" : ariaLabelledBy , ...props } ) {
21
67
return (
22
68
< MinutesRoot { ...props } >
23
69
< MinutesSubtract aria-label = "Subtract" />
24
- < MinutesInput />
25
70
< MinutesInput aria-labelledby = { ariaLabelledBy } />
26
71
< MinutesAdd aria-label = "Add" />
27
72
</ MinutesRoot >
28
73
) ;
29
74
}
75
+
76
+ function MinutesWithState ( { defaultValue = 5 } ) {
77
+ let [ minutes , setMinutes ] = React . useState ( defaultValue ) ;
78
+ return < Minutes value = { minutes } onValueChange = { setMinutes } /> ;
79
+ }
0 commit comments