Skip to content

Commit c80458e

Browse files
committed
test: add tests for async setup and setup closure return type check
1 parent 36d7e58 commit c80458e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/DittoProvider.spec.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,56 @@ describe('Ditto Provider Tests', () => {
241241
expect(setup).to.have.been.calledOnce
242242
expect(init).to.have.been.calledOnce
243243
})
244+
245+
it('should work with an async setup function', async () => {
246+
const config = testIdentity()
247+
const renderFn = sinon.stub()
248+
renderFn.withArgs(sinon.match({ loading: false })).returns('loaded')
249+
250+
root.render(
251+
<DittoProvider
252+
setup={async () => {
253+
const ditto = new Ditto(config.identity, config.path)
254+
await new Promise((resolve) => setTimeout(resolve, 10))
255+
return ditto
256+
}}
257+
>
258+
{renderFn}
259+
</DittoProvider>,
260+
)
261+
262+
await waitFor(() => container.textContent === 'loaded')
263+
expect(renderFn).to.have.been.calledTwice
264+
})
265+
266+
it(`should provide an error when the setup function doesn't return a ditto instance`, async () => {
267+
const setup = sinon.stub().returns(null)
268+
269+
root.render(
270+
<DittoProvider setup={setup}>
271+
{({ loading, error }) => !loading && error?.message}
272+
</DittoProvider>,
273+
)
274+
275+
await waitFor(
276+
() =>
277+
container.textContent ===
278+
'expected a Ditto instance to be returned by the setup function, but got null',
279+
)
280+
})
281+
282+
it('should provide an error when the setup function doesn’t return an array of ditto instances', async () => {
283+
const setup = sinon.stub().returns([null])
284+
root.render(
285+
<DittoProvider setup={setup}>
286+
{({ loading, error }) => !loading && error?.message}
287+
</DittoProvider>,
288+
)
289+
290+
await waitFor(
291+
() =>
292+
container.textContent ===
293+
'expected an array of Ditto instances to be returned by the setup function, but at least one element is not a Ditto instance (got null)',
294+
)
295+
})
244296
})

0 commit comments

Comments
 (0)