@@ -20,13 +20,14 @@ describe("Testing Context", () => {
20
20
</ Show >
21
21
) ;
22
22
} ;
23
- const div = document . createElement ( "div" ) ;
23
+
24
24
it ( "should create context properly" , ( ) => {
25
25
expect ( ThemeContext . id ) . toBeDefined ( ) ;
26
26
expect ( ThemeContext . defaultValue ) . toBe ( "light" ) ;
27
27
} ) ;
28
28
29
29
it ( "should work with single provider child" , ( ) => {
30
+ const div = document . createElement ( "div" ) ;
30
31
render (
31
32
( ) => (
32
33
< ThemeContext . Provider value = "dark" >
@@ -35,11 +36,11 @@ describe("Testing Context", () => {
35
36
) ,
36
37
div
37
38
) ;
38
- expect ( ( div . firstChild as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
39
- div . innerHTML = "" ;
39
+ expect ( div . children [ 0 ] . innerHTML ) . toBe ( "dark" ) ;
40
40
} ) ;
41
41
42
42
it ( "should work with single conditional provider child" , ( ) => {
43
+ const div = document . createElement ( "div" ) ;
43
44
render (
44
45
( ) => (
45
46
< ThemeContext . Provider value = "dark" >
@@ -48,11 +49,11 @@ describe("Testing Context", () => {
48
49
) ,
49
50
div
50
51
) ;
51
- expect ( ( div . firstChild as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
52
- div . innerHTML = "" ;
52
+ expect ( div . children [ 0 ] . innerHTML ) . toBe ( "dark" ) ;
53
53
} ) ;
54
54
55
55
it ( "should work with multi provider child" , ( ) => {
56
+ const div = document . createElement ( "div" ) ;
56
57
render (
57
58
( ) => (
58
59
< ThemeContext . Provider value = "dark" >
@@ -62,11 +63,11 @@ describe("Testing Context", () => {
62
63
) ,
63
64
div
64
65
) ;
65
- expect ( ( div . firstChild ! . nextSibling ! as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
66
- div . innerHTML = "" ;
66
+ expect ( div . children [ 1 ] . innerHTML ) . toBe ( "dark" ) ;
67
67
} ) ;
68
68
69
69
it ( "should work with multi conditional provider child" , ( ) => {
70
+ const div = document . createElement ( "div" ) ;
70
71
render (
71
72
( ) => (
72
73
< ThemeContext . Provider value = "dark" >
@@ -76,11 +77,11 @@ describe("Testing Context", () => {
76
77
) ,
77
78
div
78
79
) ;
79
- expect ( ( div . firstChild ! . nextSibling ! as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
80
- div . innerHTML = "" ;
80
+ expect ( div . children [ 1 ] . innerHTML ) . toBe ( "dark" ) ;
81
81
} ) ;
82
82
83
83
it ( "should work with dynamic multi provider child" , ( ) => {
84
+ const div = document . createElement ( "div" ) ;
84
85
const child = ( ) => < Component /> ;
85
86
render (
86
87
( ) => (
@@ -91,11 +92,11 @@ describe("Testing Context", () => {
91
92
) ,
92
93
div
93
94
) ;
94
- expect ( ( div . firstChild ! . nextSibling ! as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
95
- div . innerHTML = "" ;
95
+ expect ( div . children [ 1 ] . innerHTML ) . toBe ( "dark" ) ;
96
96
} ) ;
97
97
98
98
it ( "should work with dynamic multi conditional provider child" , ( ) => {
99
+ const div = document . createElement ( "div" ) ;
99
100
const child = ( ) => < CondComponent /> ;
100
101
render (
101
102
( ) => (
@@ -106,7 +107,29 @@ describe("Testing Context", () => {
106
107
) ,
107
108
div
108
109
) ;
109
- expect ( ( div . firstChild ! . nextSibling ! as HTMLDivElement ) . innerHTML ) . toBe ( "dark" ) ;
110
- div . innerHTML = "" ;
110
+ expect ( div . children [ 1 ] . innerHTML ) . toBe ( "dark" ) ;
111
+ } ) ;
112
+
113
+ const ThemeContextWithoutDefault = createContext < string > ( ) ;
114
+ const ComponentWithoutDefault = ( ) => {
115
+ const theme = useContext ( ThemeContextWithoutDefault ) ;
116
+ return < div > { theme ?? 'no-default' } </ div > ;
117
+ } ;
118
+
119
+ it ( "should work with no default provided" , ( ) => {
120
+ const div = document . createElement ( "div" ) ;
121
+ render (
122
+ ( ) => (
123
+ < >
124
+ < ComponentWithoutDefault />
125
+ < ThemeContextWithoutDefault . Provider value = "dark" >
126
+ < ComponentWithoutDefault />
127
+ </ ThemeContextWithoutDefault . Provider >
128
+ </ >
129
+ ) ,
130
+ div
131
+ ) ;
132
+ expect ( div . children [ 0 ] . innerHTML ! ) . toBe ( "no-default" ) ;
133
+ expect ( div . children [ 1 ] . innerHTML ! ) . toBe ( "dark" ) ;
111
134
} ) ;
112
135
} ) ;
0 commit comments