11import { waitFor } from '../'
2- // import {configure, getConfig} from '../config'
2+ import { configure , getConfig } from '../config'
33// import {render} from '../pure'
44
55function deferred ( ) {
@@ -102,15 +102,12 @@ test('provides an improved stack trace if the thrown error is a TestingLibraryEl
102102 expect ( result . stack ) . not . toBe ( originalStackTrace )
103103} )
104104
105- // test('throws nice error if provided callback is not a function', () => {
106- // const {queryByTestId} = renderIntoDocument(`
107- // <div data-testid="div"></div>
108- // `)
109- // const someElement = queryByTestId('div')
110- // expect(() => waitFor(someElement)).toThrow(
111- // 'Received `callback` arg must be a function',
112- // )
113- // })
105+ test ( 'throws nice error if provided callback is not a function' , ( ) => {
106+ const someElement = 'Hello'
107+ expect ( ( ) => waitFor ( someElement ) ) . toThrow (
108+ 'Received `callback` arg must be a function' ,
109+ )
110+ } )
114111
115112// test('timeout logs a pretty DOM', async () => {
116113// renderIntoDocument(`<div id="pretty">how pretty</div>`)
@@ -137,24 +134,23 @@ test('provides an improved stack trace if the thrown error is a TestingLibraryEl
137134// `)
138135// })
139136
140- // test('should delegate to config.getElementError', async () => {
141- // const originalConfig = getConfig()
142- // const elementError = new Error('Custom element error')
143- // const getElementError = jest.fn().mockImplementation(() => elementError)
144- // configure({getElementError})
145- //
146- // renderIntoDocument(`<div id="pretty">how pretty</div>`)
147- // const error = await waitFor(
148- // () => {
149- // throw new Error('always throws')
150- // },
151- // {timeout: 1},
152- // ).catch(e => e)
153- //
154- // expect(getElementError).toBeCalledTimes(1)
155- // expect(error.message).toMatchInlineSnapshot(`Custom element error`)
156- // configure(originalConfig)
157- // })
137+ test ( 'should delegate to config.getInstanceError' , async ( ) => {
138+ const originalConfig = getConfig ( )
139+ const elementError = new Error ( 'Custom instance error' )
140+ const getInstanceError = jest . fn ( ) . mockImplementation ( ( ) => elementError )
141+ configure ( { getInstanceError} )
142+
143+ const error = await waitFor (
144+ ( ) => {
145+ throw new Error ( 'always throws' )
146+ } ,
147+ { timeout : 1 } ,
148+ ) . catch ( e => e )
149+
150+ expect ( getInstanceError ) . toBeCalledTimes ( 1 )
151+ expect ( error . message ) . toMatchInlineSnapshot ( `Custom instance error` )
152+ configure ( originalConfig )
153+ } )
158154
159155test ( 'when a promise is returned, it does not call the callback again until that promise rejects' , async ( ) => {
160156 const sleep = t => new Promise ( r => setTimeout ( r , t ) )
@@ -178,22 +174,16 @@ test('when a promise is returned, it does not call the callback again until that
178174 await waitForPromise
179175} )
180176
181- // test('when a promise is returned, if that is not resolved within the timeout, then waitFor is rejected', async () => {
182- // const sleep = t => new Promise(r => setTimeout(r, t))
183- // const {promise} = deferred()
184- // const waitForError = waitFor(() => promise, {timeout: 1}).catch(e => e)
185- // await sleep(5)
186- //
187- // expect((await waitForError).message).toMatchInlineSnapshot(`
188- // Timed out in waitFor.
189- //
190- // Ignored nodes: comments, <script />, <style />
191- // <html>
192- // <head />
193- // <body />
194- // </html>
195- // `)
196- // })
177+ test ( 'when a promise is returned, if that is not resolved within the timeout, then waitFor is rejected' , async ( ) => {
178+ const sleep = t => new Promise ( r => setTimeout ( r , t ) )
179+ const { promise} = deferred ( )
180+ const waitForError = waitFor ( ( ) => promise , { timeout : 1 } ) . catch ( e => e )
181+ await sleep ( 5 )
182+
183+ expect ( ( await waitForError ) . message ) . toMatchInlineSnapshot (
184+ `Timed out in waitFor.` ,
185+ )
186+ } )
197187
198188test ( 'if you switch from fake timers to real timers during the wait period you get an error' , async ( ) => {
199189 jest . useFakeTimers ( )
0 commit comments