@@ -2,11 +2,11 @@ import {
22 ErrorBoundary ,
33 ParentProps ,
44 Suspense ,
5- catchError ,
65 createRoot ,
76 createSignal
87} from "solid-js" ;
98import { render } from "solid-js/web" ;
9+ import { vi } from "vitest" ;
1010import { createAsync } from "../src/data/createAsync.js" ;
1111import { awaitPromise } from "./helpers.js" ;
1212
@@ -22,36 +22,76 @@ async function getError(arg?: any): Promise<any> {
2222}
2323
2424describe ( "createAsync should" , ( ) => {
25- test ( "return 'fallback'" , ( ) => {
26- createRoot ( ( ) => {
27- const data = createAsync ( ( ) => getText ( ) ) ;
28- setTimeout ( ( ) => expect ( data ( ) ) . toBe ( "fallback" ) , 1 ) ;
29- } ) ;
25+ test ( "return 'fallback'" , async ( ) => {
26+ let dispose ! : ( ) => void ;
27+ try {
28+ await new Promise < void > ( resolve => {
29+ createRoot ( cleanup => {
30+ dispose = cleanup ;
31+ const data = createAsync ( ( ) => getText ( ) ) ;
32+ setTimeout ( ( ) => {
33+ expect ( data ( ) ) . toBe ( "fallback" ) ;
34+ resolve ( ) ;
35+ } , 1 ) ;
36+ } ) ;
37+ } ) ;
38+ } finally {
39+ dispose ?.( ) ;
40+ }
3041 } ) ;
31- test ( "return 'text'" , ( ) => {
32- createRoot ( ( ) => {
33- const data = createAsync ( ( ) => getText ( "text" ) ) ;
34- setTimeout ( ( ) => expect ( data ( ) ) . toBe ( "text" ) , 1 ) ;
35- } ) ;
42+ test ( "return 'text'" , async ( ) => {
43+ let dispose ! : ( ) => void ;
44+ try {
45+ await new Promise < void > ( resolve => {
46+ createRoot ( cleanup => {
47+ dispose = cleanup ;
48+ const data = createAsync ( ( ) => getText ( "text" ) ) ;
49+ setTimeout ( ( ) => {
50+ expect ( data ( ) ) . toBe ( "text" ) ;
51+ resolve ( ) ;
52+ } , 1 ) ;
53+ } ) ;
54+ } ) ;
55+ } finally {
56+ dispose ?.( ) ;
57+ }
3658 } ) ;
37- test ( "initial error to be caught " , ( ) => {
38- createRoot ( ( ) => {
59+ test ( "initial error to be caught " , async ( ) => {
60+ const consoleError = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
61+ const dispose = render ( ( ) => {
3962 const data = createAsync ( ( ) => getError ( ) ) ;
40- setTimeout ( ( ) => catchError ( data , err => expect ( err ) . toBeInstanceOf ( Error ) ) , 1 ) ;
41- } ) ;
63+
64+ return (
65+ < ErrorBoundary fallback = { < div id = "childError" /> } >
66+ < Suspense > { data ( ) } </ Suspense >
67+ </ ErrorBoundary >
68+ ) ;
69+ } , document . body ) ;
70+
71+ try {
72+ await awaitPromise ( ) ;
73+ expect ( document . getElementById ( "childError" ) ) . not . toBeNull ( ) ;
74+ } finally {
75+ consoleError . mockRestore ( ) ;
76+ document . body . innerHTML = "" ;
77+ dispose ( ) ;
78+ }
4279 } ) ;
43- test ( "catch error after arg change - initial valid" , ( ) =>
44- createRoot ( async dispose => {
45- async function throwWhenError ( arg : string ) : Promise < string > {
46- if ( arg === "error" ) throw new Error ( "error" ) ;
47- return arg ;
48- }
49-
50- const [ arg , setArg ] = createSignal ( "" ) ;
51- function Child ( ) {
52- const data = createAsync ( ( ) => throwWhenError ( arg ( ) ) ) ;
53-
54- return (
80+ test ( "catch error after arg change - initial valid" , async ( ) => {
81+ async function throwWhenError ( arg : string ) : Promise < string > {
82+ if ( arg === "error" ) throw new Error ( "error" ) ;
83+ return arg ;
84+ }
85+
86+ let setArg ! : ( value : string ) => string ;
87+ const dispose = render ( ( ) => {
88+ const [ arg , updateArg ] = createSignal ( "" ) ;
89+ setArg = updateArg ;
90+
91+ const data = createAsync ( ( ) => throwWhenError ( arg ( ) ) ) ;
92+
93+ return (
94+ < Parent >
5595 < div id = "child" >
5696 < ErrorBoundary
5797 fallback = { ( _ , reset ) => (
@@ -72,48 +112,45 @@ describe("createAsync should", () => {
72112 </ Suspense >
73113 </ ErrorBoundary >
74114 </ div >
75- ) ;
76- }
77- await render (
78- ( ) => (
79- < Parent >
80- < Child />
81- </ Parent >
82- ) ,
83- document . body
115+ </ Parent >
84116 ) ;
117+ } , document . body ) ;
118+
119+ try {
85120 const childErrorElement = ( ) => document . getElementById ( "childError" ) ;
86121 const parentErrorElement = document . getElementById ( "parentError" ) ;
87122 expect ( childErrorElement ( ) ) . toBeNull ( ) ;
88123 expect ( parentErrorElement ) . toBeNull ( ) ;
124+
89125 setArg ( "error" ) ;
90126 await awaitPromise ( ) ;
91127
92- // after changing the arg the error should still be caught by the Child's ErrorBoundary
93128 expect ( childErrorElement ( ) ) . not . toBeNull ( ) ;
94129 expect ( parentErrorElement ) . toBeNull ( ) ;
95130
96- //reset ErrorBoundary
97131 document . getElementById ( "reset" ) ?. click ( ) ;
98132
99133 expect ( childErrorElement ( ) ) . toBeNull ( ) ;
100134 await awaitPromise ( ) ;
101- const dataEl = ( ) => document . getElementById ( "data" ) ;
102135
103- expect ( dataEl ( ) ) . not . toBeNull ( ) ;
136+ expect ( document . getElementById ( "data" ) ) . not . toBeNull ( ) ;
104137 expect ( document . getElementById ( "data" ) ?. innerHTML ) . toBe ( "true" ) ;
105138 expect ( document . getElementById ( "latest" ) ?. innerHTML ) . toBe ( "true" ) ;
106-
139+ } finally {
107140 document . body . innerHTML = "" ;
108141 dispose ( ) ;
109- } ) ) ;
110- test ( "catch consecutive error after initial error change to be caught after arg change" , ( ) =>
111- createRoot ( async cleanup => {
112- const [ arg , setArg ] = createSignal ( "error" ) ;
113- function Child ( ) {
114- const data = createAsync ( ( ) => getError ( arg ( ) ) ) ;
115-
116- return (
142+ }
143+ } ) ;
144+ test ( "catch consecutive error after initial error change to be caught after arg change" , async ( ) => {
145+ let setArg ! : ( value : string ) => string ;
146+ const dispose = render ( ( ) => {
147+ const [ arg , updateArg ] = createSignal ( "error" ) ;
148+ setArg = updateArg ;
149+
150+ const data = createAsync ( ( ) => getError ( arg ( ) ) ) ;
151+
152+ return (
153+ < Parent >
117154 < div id = "child" >
118155 < ErrorBoundary
119156 fallback = { ( _ , reset ) => (
@@ -125,32 +162,28 @@ describe("createAsync should", () => {
125162 < Suspense > { data ( ) } </ Suspense >
126163 </ ErrorBoundary >
127164 </ div >
128- ) ;
129- }
130- await render (
131- ( ) => (
132- < Parent >
133- < Child />
134- </ Parent >
135- ) ,
136- document . body
165+ </ Parent >
137166 ) ;
167+ } , document . body ) ;
138168
139- // Child's ErrorBoundary should catch the error
169+ try {
170+ await awaitPromise ( ) ;
140171 expect ( document . getElementById ( "childError" ) ) . not . toBeNull ( ) ;
141172 expect ( document . getElementById ( "parentError" ) ) . toBeNull ( ) ;
173+
142174 setArg ( "error_2" ) ;
143175 await awaitPromise ( ) ;
144- // after changing the arg the error should still be caught by the Child's ErrorBoundary
176+
145177 expect ( document . getElementById ( "childError" ) ) . not . toBeNull ( ) ;
146178 expect ( document . getElementById ( "parentError" ) ) . toBeNull ( ) ;
147179
148180 document . getElementById ( "reset" ) ?. click ( ) ;
149181 await awaitPromise ( ) ;
150182 expect ( document . getElementById ( "childError" ) ) . not . toBeNull ( ) ;
151183 expect ( document . getElementById ( "parentError" ) ) . toBeNull ( ) ;
152-
184+ } finally {
153185 document . body . innerHTML = "" ;
154- cleanup ( ) ;
155- } ) ) ;
186+ dispose ( ) ;
187+ }
188+ } ) ;
156189} ) ;
0 commit comments