6
6
*/
7
7
8
8
import React from 'react' ;
9
- import { mount } from 'enzyme' ;
9
+ import { render , fireEvent } from '@testing-library/react' ;
10
+ import '@testing-library/jest-dom/extend-expect' ;
10
11
11
12
import { GraphQLString } from 'graphql' ;
12
13
@@ -21,115 +22,107 @@ import {
21
22
22
23
describe ( 'TypeDoc' , ( ) => {
23
24
it ( 'renders a top-level query object type' , ( ) => {
24
- const W = mount (
25
+ const { container } = render (
25
26
< TypeDoc
26
27
schema = { ExampleSchema }
27
28
type = { ExampleQuery }
28
29
onClickType = { jest . fn ( ) }
29
30
/> ,
30
31
) ;
31
- const cats = W . find ( '.doc-category-item' ) ;
32
- expect ( cats . at ( 0 ) . text ( ) ) . toEqual ( 'string: String' ) ;
33
- expect ( cats . at ( 1 ) . text ( ) ) . toEqual ( 'union: exampleUnion' ) ;
34
- expect ( cats . at ( 2 ) . text ( ) ) . toEqual (
32
+ const cats = container . querySelectorAll ( '.doc-category-item' ) ;
33
+ expect ( cats [ 0 ] ) . toHaveTextContent ( 'string: String' ) ;
34
+ expect ( cats [ 1 ] ) . toHaveTextContent ( 'union: exampleUnion' ) ;
35
+ expect ( cats [ 2 ] ) . toHaveTextContent (
35
36
'fieldWithArgs(stringArg: String): String' ,
36
37
) ;
37
38
} ) ;
38
39
39
40
it ( 'handles onClickField and onClickType' , ( ) => {
40
41
const onClickType = jest . fn ( ) ;
41
42
const onClickField = jest . fn ( ) ;
42
- const W = mount (
43
+ const { container } = render (
43
44
< TypeDoc
44
45
schema = { ExampleSchema }
45
46
type = { ExampleQuery }
46
47
onClickType = { onClickType }
47
48
onClickField = { onClickField }
48
49
/> ,
49
50
) ;
50
- W . find ( 'TypeLink' )
51
- . at ( 0 )
52
- . simulate ( 'click' ) ;
51
+ fireEvent . click ( container . querySelector ( '.type-name' ) ) ;
53
52
expect ( onClickType . mock . calls . length ) . toEqual ( 1 ) ;
54
53
expect ( onClickType . mock . calls [ 0 ] [ 0 ] ) . toEqual ( GraphQLString ) ;
55
54
56
- W . find ( '.field-name' )
57
- . at ( 0 )
58
- . simulate ( 'click' ) ;
59
-
55
+ fireEvent . click ( container . querySelector ( '.field-name' ) ) ;
60
56
expect ( onClickField . mock . calls . length ) . toEqual ( 1 ) ;
61
57
expect ( onClickField . mock . calls [ 0 ] [ 0 ] . name ) . toEqual ( 'string' ) ;
62
58
expect ( onClickField . mock . calls [ 0 ] [ 0 ] . type ) . toEqual ( GraphQLString ) ;
63
59
expect ( onClickField . mock . calls [ 0 ] [ 1 ] ) . toEqual ( ExampleQuery ) ;
64
60
} ) ;
65
61
66
62
it ( 'renders deprecated fields when you click to see them' , ( ) => {
67
- const W = mount (
63
+ const { container } = render (
68
64
< TypeDoc
69
65
schema = { ExampleSchema }
70
66
type = { ExampleQuery }
71
67
onClickType = { jest . fn ( ) }
72
68
/> ,
73
69
) ;
74
- let cats = W . find ( '.doc-category-item' ) ;
75
- expect ( cats . length ) . toEqual ( 3 ) ;
70
+ let cats = container . querySelectorAll ( '.doc-category-item' ) ;
71
+ expect ( cats ) . toHaveLength ( 3 ) ;
76
72
77
- W . find ( '.show-btn' ) . simulate ( 'click' ) ;
73
+ fireEvent . click ( container . querySelector ( '.show-btn' ) ) ;
78
74
79
- cats = W . find ( '.doc-category-item' ) ;
80
- expect ( cats . length ) . toEqual ( 4 ) ;
81
- expect (
82
- W . find ( '.field-name' )
83
- . at ( 3 )
84
- . text ( ) ,
85
- ) . toEqual ( 'deprecatedField' ) ;
86
- expect (
87
- W . find ( '.doc-deprecation' )
88
- . at ( 0 )
89
- . text ( ) ,
90
- ) . toEqual ( 'example deprecation reason\n' ) ;
75
+ cats = container . querySelectorAll ( '.doc-category-item' ) ;
76
+ expect ( cats ) . toHaveLength ( 4 ) ;
77
+ expect ( container . querySelectorAll ( '.field-name' ) [ 3 ] ) . toHaveTextContent (
78
+ 'deprecatedField' ,
79
+ ) ;
80
+ expect ( container . querySelector ( '.doc-deprecation' ) ) . toHaveTextContent (
81
+ 'example deprecation reason' ,
82
+ ) ;
91
83
} ) ;
92
84
93
85
it ( 'renders a Union type' , ( ) => {
94
- const W = mount ( < TypeDoc schema = { ExampleSchema } type = { ExampleUnion } /> ) ;
95
- expect (
96
- W . find ( '.doc-category-title' )
97
- . at ( 0 )
98
- . text ( ) ,
99
- ) . toEqual ( 'possible types' ) ;
86
+ const { container } = render (
87
+ < TypeDoc schema = { ExampleSchema } type = { ExampleUnion } /> ,
88
+ ) ;
89
+ expect ( container . querySelector ( '.doc-category-title' ) ) . toHaveTextContent (
90
+ 'possible types' ,
91
+ ) ;
100
92
} ) ;
101
93
102
94
it ( 'renders an Enum type' , ( ) => {
103
- const W = mount ( < TypeDoc schema = { ExampleSchema } type = { ExampleEnum } /> ) ;
104
- expect (
105
- W . find ( '.doc-category-title' )
106
- . at ( 0 )
107
- . text ( ) ,
108
- ) . toEqual ( 'values' ) ;
109
- const enums = W . find ( 'EnumValue ') ;
110
- expect ( enums . at ( 0 ) . props ( ) . value . value ) . toEqual ( 'Value 1 ') ;
111
- expect ( enums . at ( 1 ) . props ( ) . value . value ) . toEqual ( 'Value 2 ') ;
95
+ const { container } = render (
96
+ < TypeDoc schema = { ExampleSchema } type = { ExampleEnum } /> ,
97
+ ) ;
98
+ expect ( container . querySelector ( '.doc-category-title' ) ) . toHaveTextContent (
99
+ 'values' ,
100
+ ) ;
101
+ const enums = container . querySelectorAll ( '.enum-value ') ;
102
+ expect ( enums [ 0 ] ) . toHaveTextContent ( 'value1 ') ;
103
+ expect ( enums [ 1 ] ) . toHaveTextContent ( 'value2 ') ;
112
104
} ) ;
113
105
114
106
it ( 'shows deprecated enum values on click' , ( ) => {
115
- const W = mount ( < TypeDoc schema = { ExampleSchema } type = { ExampleEnum } /> ) ;
116
- expect ( W . state ( ) . showDeprecated ) . toEqual ( false ) ;
117
- const titles = W . find ( '.doc-category-title' ) ;
118
- expect ( titles . at ( 0 ) . text ( ) ) . toEqual ( 'values' ) ;
119
- expect ( titles . at ( 1 ) . text ( ) ) . toEqual ( 'deprecated values' ) ;
120
- let enums = W . find ( 'EnumValue' ) ;
121
- expect ( enums . length ) . toEqual ( 2 ) ;
107
+ const { getByText, container } = render (
108
+ < TypeDoc schema = { ExampleSchema } type = { ExampleEnum } /> ,
109
+ ) ;
110
+ const showBtn = getByText ( 'Show deprecated values...' ) ;
111
+ expect ( showBtn ) . toBeInTheDocument ( ) ;
112
+ const titles = container . querySelectorAll ( '.doc-category-title' ) ;
113
+ expect ( titles [ 0 ] ) . toHaveTextContent ( 'values' ) ;
114
+ expect ( titles [ 1 ] ) . toHaveTextContent ( 'deprecated values' ) ;
115
+ let enums = container . querySelectorAll ( '.enum-value' ) ;
116
+ expect ( enums ) . toHaveLength ( 2 ) ;
122
117
123
118
// click button to show deprecated enum values
124
- W . find ( '.show-btn' ) . simulate ( 'click' ) ;
125
- expect ( W . state ( ) . showDeprecated ) . toEqual ( true ) ;
126
- enums = W . find ( 'EnumValue' ) ;
127
- expect ( enums . length ) . toEqual ( 3 ) ;
128
- expect ( enums . at ( 2 ) . props ( ) . value . value ) . toEqual ( 'Value 3' ) ;
129
- expect (
130
- W . find ( '.doc-deprecation' )
131
- . at ( 1 )
132
- . text ( ) ,
133
- ) . toEqual ( 'Only two are needed\n' ) ;
119
+ fireEvent . click ( showBtn ) ;
120
+ expect ( showBtn ) . not . toBeInTheDocument ( ) ;
121
+ enums = container . querySelectorAll ( '.enum-value' ) ;
122
+ expect ( enums ) . toHaveLength ( 3 ) ;
123
+ expect ( enums [ 2 ] ) . toHaveTextContent ( 'value3' ) ;
124
+ expect ( container . querySelector ( '.doc-deprecation' ) ) . toHaveTextContent (
125
+ 'Only two are needed' ,
126
+ ) ;
134
127
} ) ;
135
128
} ) ;
0 commit comments