11const React = require ( 'react' ) ;
2- const { shallow } = require ( 'enzyme ' ) ;
2+ const { fireEvent , render } = require ( '@testing-library/react ' ) ;
33
44const { Variable, VARIABLE_REGEXP } = require ( '../index' ) ;
55
@@ -13,45 +13,47 @@ describe('single variable', () => {
1313 } ;
1414
1515 it ( 'should render value' , ( ) => {
16- const variable = shallow ( < Variable { ...props } /> ) ;
16+ const { container } = render ( < Variable { ...props } /> ) ;
1717
18- expect ( variable . text ( ) ) . toBe ( '123456' ) ;
18+ expect ( container ) . toHaveTextContent ( '123456' ) ;
1919 } ) ;
2020
2121 it ( 'should render default if value not set' , ( ) => {
22- const variable = shallow ( < Variable { ...props } defaults = { [ { name : 'apiKey' , default : 'default' } ] } user = { { } } /> ) ;
22+ const { container } = render ( < Variable { ...props } defaults = { [ { name : 'apiKey' , default : 'default' } ] } user = { { } } /> ) ;
2323
24- expect ( variable . text ( ) ) . toBe ( 'default' ) ;
24+ expect ( container ) . toHaveTextContent ( 'default' ) ;
2525 } ) ;
2626
2727 it ( 'should render uppercase if no value and no default' , ( ) => {
28- const variable = shallow ( < Variable { ...props } defaults = { [ ] } user = { { } } /> ) ;
28+ const { container } = render ( < Variable { ...props } defaults = { [ ] } user = { { } } /> ) ;
2929
30- expect ( variable . text ( ) ) . toBe ( 'APIKEY' ) ;
30+ expect ( container ) . toHaveTextContent ( 'APIKEY' ) ;
3131 } ) ;
3232
3333 it ( 'should render auth dropdown if default and oauth enabled' , ( ) => {
34- const variable = shallow (
34+ const { container } = render (
3535 < Variable { ...props } defaults = { [ { name : 'apiKey' , default : 'default' } ] } oauth user = { { } } />
3636 ) ;
37- variable . find ( '.variable-underline' ) . simulate ( 'click' ) ;
3837
39- expect ( variable . find ( '#loginDropdown' ) ) . toHaveLength ( 1 ) ;
38+ fireEvent . click ( container . querySelector ( '.variable-underline' ) ) ;
39+
40+ expect ( container . querySelectorAll ( '#loginDropdown' ) ) . toHaveLength ( 1 ) ;
4041 } ) ;
4142
4243 it ( 'should render auth dropdown if no default and oauth enabled' , ( ) => {
43- const variable = shallow ( < Variable { ...props } defaults = { [ ] } oauth user = { { } } /> ) ;
44- variable . find ( '.variable-underline' ) . simulate ( 'click' ) ;
44+ const { container } = render ( < Variable { ...props } defaults = { [ ] } oauth user = { { } } /> ) ;
45+
46+ fireEvent . click ( container . querySelector ( '.variable-underline' ) ) ;
4547
46- expect ( variable . find ( '#loginDropdown' ) ) . toHaveLength ( 1 ) ;
48+ expect ( container . querySelectorAll ( '#loginDropdown' ) ) . toHaveLength ( 1 ) ;
4749 } ) ;
4850
4951 it . todo ( 'should set `selected` if nothing is selected' ) ;
5052
5153 it ( 'should render objects as strings' , ( ) => {
52- const variable = shallow ( < Variable { ...props } user = { { apiKey : { renderTo : 'string' } } } /> ) ;
54+ const { container } = render ( < Variable { ...props } user = { { apiKey : { renderTo : 'string' } } } /> ) ;
5355
54- expect ( variable . text ( ) ) . toBe ( JSON . stringify ( { renderTo : 'string' } ) ) ;
56+ expect ( container ) . toHaveTextContent ( JSON . stringify ( { renderTo : 'string' } ) ) ;
5557 } ) ;
5658} ) ;
5759
@@ -70,32 +72,34 @@ describe('multiple variables', () => {
7072 } ;
7173
7274 it ( 'should render the first of multiple values' , ( ) => {
73- const variable = shallow ( < Variable { ...props } /> ) ;
75+ const { container } = render ( < Variable { ...props } /> ) ;
7476
75- expect ( variable . text ( ) ) . toBe ( '123' ) ;
77+ expect ( container ) . toHaveTextContent ( '123' ) ;
7678 } ) ;
7779
7880 it ( 'should render whatever the selected name is' , ( ) => {
79- const variable = shallow ( < Variable { ...props } selected = "project2" /> ) ;
81+ const { container } = render ( < Variable { ...props } selected = "project2" /> ) ;
8082
81- expect ( variable . text ( ) ) . toBe ( '456' ) ;
83+ expect ( container ) . toHaveTextContent ( '456' ) ;
8284 } ) ;
8385
8486 it ( 'should show dropdown when clicked' , ( ) => {
85- const variable = shallow ( < Variable { ...props } selected = "project2" /> ) ;
87+ const { container } = render ( < Variable { ...props } selected = "project2" /> ) ;
8688
87- variable . find ( '.variable-underline' ) . simulate ( 'click' ) ;
89+ fireEvent . click ( container . querySelector ( '.variable-underline' ) ) ;
8890
89- expect ( variable . find ( 'select option' ) . map ( el => el . text ( ) ) ) . toStrictEqual ( [ 'project1' , 'project2' ] ) ;
91+ const options = [ ] ;
92+ container . querySelectorAll ( 'select option' ) . forEach ( tk => {
93+ options . push ( tk . text ) ;
94+ } ) ;
95+
96+ expect ( options ) . toStrictEqual ( [ 'project1' , 'project2' ] ) ;
9097 } ) ;
9198
9299 it ( 'should select value when clicked' , ( ) => {
93- let called = false ;
94- function changeSelected ( selected ) {
95- expect ( selected ) . toBe ( 'project2' ) ;
96- called = true ;
97- }
98- const variable = shallow (
100+ const changeSelected = jest . fn ( ) ;
101+
102+ const { container } = render (
99103 < Variable
100104 { ...props }
101105 changeSelected = { changeSelected }
@@ -106,16 +110,16 @@ describe('multiple variables', () => {
106110 />
107111 ) ;
108112
109- variable . find ( '.variable-underline' ) . simulate ( 'click' ) ;
110- variable . find ( 'select' ) . simulate ( 'change' , {
113+ fireEvent . click ( container . querySelector ( '.variable-underline' ) ) ;
114+ fireEvent . change ( container . querySelector ( 'select' ) , {
111115 target : {
112- value : variable . find ( 'select option' ) . at ( 1 ) . text ( ) ,
116+ value : container . querySelectorAll ( 'select option' ) [ 1 ] . text ,
113117 } ,
114118 } ) ;
115119
116- expect ( called ) . toBe ( true ) ;
120+ expect ( changeSelected ) . toHaveBeenCalledWith ( 'project2' ) ;
117121
118- expect ( variable . state ( 'showDropdown ') ) . toBe ( false ) ;
122+ expect ( container . querySelector ( 'select ') ) . not . toBeInTheDocument ( ) ;
119123 } ) ;
120124
121125 it . todo ( 'should render auth dropdown if default and oauth enabled' ) ;
0 commit comments