@@ -16,7 +16,7 @@ describe('React', () => {
1616 link . setAttribute ( 'key' , 'link' )
1717 div . appendChild ( link )
1818
19- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
19+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
2020 expect ( wrapper . find ( 'a' ) . text ( ) ) . toEqual ( 'Link' )
2121 } )
2222
@@ -26,7 +26,7 @@ describe('React', () => {
2626 newLine . setAttribute ( 'key' , 'test' )
2727 div . appendChild ( newLine )
2828
29- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
29+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
3030 expect ( wrapper . find ( 'br' ) ) . toHaveLength ( 1 )
3131 } )
3232
@@ -35,7 +35,7 @@ describe('React', () => {
3535 const text = document . createTextNode ( 'Text' )
3636 div . appendChild ( text )
3737
38- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
38+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
3939 expect ( wrapper . text ( ) ) . toEqual ( 'Text' )
4040 } )
4141
@@ -44,7 +44,7 @@ describe('React', () => {
4444 const comment = document . createComment ( 'Comment' )
4545 div . appendChild ( comment )
4646
47- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
47+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
4848 expect ( wrapper . children ( ) ) . toHaveLength ( 0 )
4949 } )
5050
@@ -56,7 +56,7 @@ describe('React', () => {
5656 span . setAttribute ( 'key' , 'test' )
5757 div . appendChild ( span )
5858
59- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
59+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
6060 expect ( wrapper . find ( 'span' ) ) . toHaveLength ( 1 )
6161 expect ( wrapper . find ( 'span' ) . prop ( 'htmlFor' ) ) . toEqual ( 'test' )
6262 expect ( wrapper . find ( 'span' ) . prop ( 'className' ) ) . toEqual ( 'test' )
@@ -72,9 +72,43 @@ describe('React', () => {
7272 link . textContent = 'Link'
7373 div . appendChild ( link )
7474
75- const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true } ) )
75+ const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true , useAsKey : [ 'key' ] } ) )
7676 expect ( wrapper . find ( 'a' ) . text ( ) ) . toEqual ( 'Link' )
7777 } )
7878 } )
79+
80+ describe ( '#useAsKey' , ( ) => {
81+ it ( 'should use key as a default key' , ( ) => {
82+ const div = document . createElement ( 'div' )
83+ const link = document . createElement ( 'a' )
84+ link . textContent = 'Link'
85+ link . setAttribute ( 'key' , 'link' )
86+ div . appendChild ( link )
87+
88+ const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true , useAsKey : [ 'key' ] } ) )
89+ expect ( wrapper . find ( 'a' ) . key ( ) ) . toEqual ( 'link' )
90+ } )
91+
92+ it ( 'should use other element in the list as fallback key' , ( ) => {
93+ const div = document . createElement ( 'div' )
94+ const link = document . createElement ( 'a' )
95+ link . textContent = 'Link'
96+ link . setAttribute ( 'class' , 'fallback key' )
97+ div . appendChild ( link )
98+
99+ const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true , useAsKey : [ 'key' , 'id' , 'class' ] } ) )
100+ expect ( wrapper . find ( 'a' ) . key ( ) ) . toEqual ( 'fallback key' )
101+ } )
102+
103+ it ( 'should use null if no key match' , ( ) => {
104+ const div = document . createElement ( 'div' )
105+ const link = document . createElement ( 'a' )
106+ link . textContent = 'Link'
107+ div . appendChild ( link )
108+
109+ const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true , useAsKey : [ 'key' , 'id' ] } ) )
110+ expect ( wrapper . find ( 'a' ) . key ( ) ) . toEqual ( null )
111+ } )
112+ } )
79113 } )
80114} )
0 commit comments