@@ -3,54 +3,20 @@ import * as React from 'react'
33
44import { render } from '../src/react'
55
6- function shallowWrapper ( elements : React . ReactNode [ ] ) {
6+ function shallowWrapper ( elements : React . ReactNode [ ] | React . ReactFragment ) {
77 return shallow ( React . createElement ( 'div' , null , elements ) )
88}
99
1010describe ( 'React' , ( ) => {
1111 describe ( '#render' , ( ) => {
12- describe ( 'generatesKeys' , ( ) => {
13- it ( 'should use key if provided' , ( ) => {
14- const div = document . createElement ( 'div' )
15- const link = document . createElement ( 'a' )
16- link . textContent = 'Link'
17- link . setAttribute ( 'key' , 'link' )
18- div . appendChild ( link )
19-
20- const wrapper = shallowWrapper ( render ( div . childNodes , { generatesKeys : true } ) )
21- expect ( wrapper . find ( 'a' ) . key ( ) ) . toEqual ( 'link' )
22- } )
23-
24- it ( 'should use id if provided and key is not here' , ( ) => {
25- const div = document . createElement ( 'div' )
26- const link = document . createElement ( 'a' )
27- link . textContent = 'Link'
28- link . setAttribute ( 'id' , 'link' )
29- div . appendChild ( link )
30-
31- const wrapper = shallowWrapper ( render ( div . childNodes , { generatesKeys : true } ) )
32- expect ( wrapper . find ( 'a' ) . key ( ) ) . toEqual ( 'link' )
33- } )
34-
35- it ( 'should use a random key if key nor id are specified' , ( ) => {
36- const div = document . createElement ( 'div' )
37- const link = document . createElement ( 'a' )
38- link . textContent = 'Link'
39- div . appendChild ( link )
40-
41- const wrapper = shallowWrapper ( render ( div . childNodes , { generatesKeys : true } ) )
42- expect ( wrapper . find ( 'a' ) . key ( ) ) . toHaveLength ( 5 )
43- } )
44- } )
45-
4612 it ( 'should render a element node' , ( ) => {
4713 const div = document . createElement ( 'div' )
4814 const link = document . createElement ( 'a' )
4915 link . textContent = 'Link'
5016 link . setAttribute ( 'key' , 'link' )
5117 div . appendChild ( link )
5218
53- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
19+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
5420 expect ( wrapper . find ( 'a' ) . text ( ) ) . toEqual ( 'Link' )
5521 } )
5622
@@ -60,7 +26,7 @@ describe('React', () => {
6026 newLine . setAttribute ( 'key' , 'test' )
6127 div . appendChild ( newLine )
6228
63- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
29+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
6430 expect ( wrapper . find ( 'br' ) ) . toHaveLength ( 1 )
6531 } )
6632
@@ -69,7 +35,7 @@ describe('React', () => {
6935 const text = document . createTextNode ( 'Text' )
7036 div . appendChild ( text )
7137
72- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
38+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
7339 expect ( wrapper . text ( ) ) . toEqual ( 'Text' )
7440 } )
7541
@@ -78,7 +44,7 @@ describe('React', () => {
7844 const comment = document . createComment ( 'Comment' )
7945 div . appendChild ( comment )
8046
81- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
47+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
8248 expect ( wrapper . children ( ) ) . toHaveLength ( 0 )
8349 } )
8450
@@ -90,11 +56,59 @@ describe('React', () => {
9056 span . setAttribute ( 'key' , 'test' )
9157 div . appendChild ( span )
9258
93- const wrapper = shallowWrapper ( render ( div . childNodes , { } ) )
59+ const wrapper = shallowWrapper ( render ( div . childNodes , { useAsKey : [ 'key' ] } ) )
9460 expect ( wrapper . find ( 'span' ) ) . toHaveLength ( 1 )
9561 expect ( wrapper . find ( 'span' ) . prop ( 'htmlFor' ) ) . toEqual ( 'test' )
9662 expect ( wrapper . find ( 'span' ) . prop ( 'className' ) ) . toEqual ( 'test' )
9763 expect ( wrapper . find ( 'span' ) . key ( ) ) . toEqual ( 'test' )
9864 } )
9965 } )
66+
67+ describe ( 'Options' , ( ) => {
68+ describe ( '#useFragment' , ( ) => {
69+ it ( 'should return a Fragment' , ( ) => {
70+ const div = document . createElement ( 'div' )
71+ const link = document . createElement ( 'a' )
72+ link . textContent = 'Link'
73+ div . appendChild ( link )
74+
75+ const wrapper = shallowWrapper ( render ( div . childNodes , { useFragment : true , useAsKey : [ 'key' ] } ) )
76+ expect ( wrapper . find ( 'a' ) . text ( ) ) . toEqual ( 'Link' )
77+ } )
78+ } )
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+ } )
113+ } )
100114} )
0 commit comments