@@ -8,26 +8,49 @@ describe("Button ", () => {
8
8
const wrapper = shallow ( < Button > Test content</ Button > ) ;
9
9
expect ( wrapper ) . toMatchSnapshot ( ) ;
10
10
} ) ;
11
-
12
11
it ( "renders as a link" , ( ) => {
13
12
const wrapper = shallow ( < Button element = "a" > Test content</ Button > ) ;
14
13
expect ( wrapper ) . toMatchSnapshot ( ) ;
15
- expect ( wrapper . prop ( "onClick" ) ) . toBe ( undefined ) ;
14
+ } ) ;
15
+
16
+ it ( "can handle button click events" , ( ) => {
17
+ const onClick = jest . fn ( ) ;
18
+ const wrapper = shallow ( < Button onClick = { onClick } /> ) ;
19
+ wrapper . simulate ( "click" ) ;
20
+ expect ( onClick ) . toHaveBeenCalled ( ) ;
21
+ } ) ;
22
+
23
+ it ( "can handle anchor click events" , ( ) => {
24
+ const onClick = jest . fn ( ) ;
25
+ const wrapper = shallow ( < Button element = "a" onClick = { onClick } /> ) ;
26
+ wrapper . simulate ( "click" ) ;
27
+ expect ( onClick ) . toHaveBeenCalled ( ) ;
16
28
} ) ;
17
29
18
30
it ( "correctly disables a button" , ( ) => {
19
- const wrapper = shallow ( < Button disabled = { true } /> ) ;
31
+ const onClick = jest . fn ( ) ;
32
+ const wrapper = shallow ( < Button disabled = { true } onClick = { onClick } /> ) ;
20
33
expect ( wrapper . prop ( "disabled" ) ) . toBe ( true ) ;
21
34
expect ( wrapper . prop ( "className" ) . includes ( "is-disabled" ) ) . toBe ( false ) ;
22
35
expect ( wrapper . prop ( "aria-disabled" ) ) . toBe ( undefined ) ;
36
+ const preventDefault = jest . fn ( ) ;
37
+ wrapper . simulate ( "click" , { preventDefault } ) ;
38
+ expect ( preventDefault ) . toHaveBeenCalled ( ) ;
39
+ expect ( onClick ) . not . toHaveBeenCalled ( ) ;
23
40
} ) ;
24
41
25
42
it ( "correctly disables a link" , ( ) => {
26
- const wrapper = shallow ( < Button element = "a" disabled = { true } /> ) ;
43
+ const onClick = jest . fn ( ) ;
44
+ const wrapper = shallow (
45
+ < Button element = "a" disabled = { true } onClick = { onClick } />
46
+ ) ;
27
47
expect ( wrapper . prop ( "className" ) . includes ( "is-disabled" ) ) . toBe ( true ) ;
28
48
expect ( wrapper . prop ( "aria-disabled" ) ) . toBe ( true ) ;
29
49
expect ( wrapper . prop ( "disabled" ) ) . toBe ( undefined ) ;
30
- expect ( wrapper . prop ( "onClick" ) ) . not . toBe ( undefined ) ;
50
+ const preventDefault = jest . fn ( ) ;
51
+ wrapper . simulate ( "click" , { preventDefault } ) ;
52
+ expect ( preventDefault ) . toHaveBeenCalled ( ) ;
53
+ expect ( onClick ) . not . toHaveBeenCalled ( ) ;
31
54
} ) ;
32
55
33
56
it ( "correctly handle icons" , ( ) => {
0 commit comments