From dd1aeb0d3d9f9cd1a5efa231a8394bef06cf288a Mon Sep 17 00:00:00 2001
From: Corbin Crutchley
Date: Mon, 25 Oct 2021 13:29:54 -0700
Subject: [PATCH] Initial tests for waitFor, fix error with error retries
---
src/__tests__/wait-for.js | 257 ++++++++++++++++++++++++++++++++++++++
src/wait-for.js | 4 +-
2 files changed, 260 insertions(+), 1 deletion(-)
create mode 100644 src/__tests__/wait-for.js
diff --git a/src/__tests__/wait-for.js b/src/__tests__/wait-for.js
new file mode 100644
index 0000000..f24837d
--- /dev/null
+++ b/src/__tests__/wait-for.js
@@ -0,0 +1,257 @@
+import {waitFor} from '../'
+// import {configure, getConfig} from '../config'
+// import {render} from '../pure'
+
+function deferred() {
+ let resolve, reject
+ const promise = new Promise((res, rej) => {
+ resolve = res
+ reject = rej
+ })
+ return {promise, resolve, reject}
+}
+
+test('waits callback to not throw an error', async () => {
+ const spy = jest.fn()
+ // we are using random timeout here to simulate a real-time example
+ // of an async operation calling a callback at a non-deterministic time
+ const randomTimeout = Math.floor(Math.random() * 60)
+ setTimeout(spy, randomTimeout)
+
+ await waitFor(() => expect(spy).toHaveBeenCalledTimes(1))
+ expect(spy).toHaveBeenCalledWith()
+})
+
+// we used to have a limitation where we had to set an interval of 0 to 1
+// otherwise there would be problems. I don't think this limitation exists
+// anymore, but we'll keep this test around to make sure a problem doesn't
+// crop up.
+test('can accept an interval of 0', () => waitFor(() => {}, {interval: 0}))
+
+test('can timeout after the given timeout time', async () => {
+ const error = new Error('throws every time')
+ const result = await waitFor(
+ () => {
+ throw error
+ },
+ {timeout: 8, interval: 5},
+ ).catch(e => e)
+ expect(result).toBe(error)
+})
+
+test('if no error is thrown then throws a timeout error', async () => {
+ const result = await waitFor(
+ () => {
+ // eslint-disable-next-line no-throw-literal
+ throw undefined
+ },
+ {timeout: 8, interval: 5, onTimeout: e => e},
+ ).catch(e => e)
+ expect(result).toMatchInlineSnapshot(`[Error: Timed out in waitFor.]`)
+})
+
+test('if showOriginalStackTrace on a timeout error then the stack trace does not include this file', async () => {
+ const result = await waitFor(
+ () => {
+ // eslint-disable-next-line no-throw-literal
+ throw undefined
+ },
+ {timeout: 8, interval: 5, showOriginalStackTrace: true},
+ ).catch(e => e)
+ expect(result.stack).not.toMatch(__dirname)
+})
+
+test('uses full stack error trace when showOriginalStackTrace present', async () => {
+ const error = new Error('Throws the full stack trace')
+ // even if the error is a TestingLibraryElementError
+ error.name = 'TestingLibraryElementError'
+ const originalStackTrace = error.stack
+ const result = await waitFor(
+ () => {
+ throw error
+ },
+ {timeout: 8, interval: 5, showOriginalStackTrace: true},
+ ).catch(e => e)
+ expect(result.stack).toBe(originalStackTrace)
+})
+
+test('does not change the stack trace if the thrown error is not a TestingLibraryElementError', async () => {
+ const error = new Error('Throws the full stack trace')
+ const originalStackTrace = error.stack
+ const result = await waitFor(
+ () => {
+ throw error
+ },
+ {timeout: 8, interval: 5},
+ ).catch(e => e)
+ expect(result.stack).toBe(originalStackTrace)
+})
+
+test('provides an improved stack trace if the thrown error is a TestingLibraryElementError', async () => {
+ const error = new Error('Throws the full stack trace')
+ error.name = 'TestingLibraryElementError'
+ const originalStackTrace = error.stack
+ const result = await waitFor(
+ () => {
+ throw error
+ },
+ {timeout: 8, interval: 5},
+ ).catch(e => e)
+ // too hard to test that the stack trace is what we want it to be
+ // so we'll just make sure that it's not the same as the original
+ expect(result.stack).not.toBe(originalStackTrace)
+})
+
+// test('throws nice error if provided callback is not a function', () => {
+// const {queryByTestId} = renderIntoDocument(`
+//
+// `)
+// const someElement = queryByTestId('div')
+// expect(() => waitFor(someElement)).toThrow(
+// 'Received `callback` arg must be a function',
+// )
+// })
+
+// test('timeout logs a pretty DOM', async () => {
+// renderIntoDocument(`how pretty
`)
+// const error = await waitFor(
+// () => {
+// throw new Error('always throws')
+// },
+// {timeout: 1},
+// ).catch(e => e)
+// expect(error.message).toMatchInlineSnapshot(`
+// always throws
+//
+// Ignored nodes: comments, ,
+//
+//
+//
+//
+// how pretty
+//
+//
+//
+// `)
+// })
+
+// test('should delegate to config.getElementError', async () => {
+// const originalConfig = getConfig()
+// const elementError = new Error('Custom element error')
+// const getElementError = jest.fn().mockImplementation(() => elementError)
+// configure({getElementError})
+//
+// renderIntoDocument(`how pretty
`)
+// const error = await waitFor(
+// () => {
+// throw new Error('always throws')
+// },
+// {timeout: 1},
+// ).catch(e => e)
+//
+// expect(getElementError).toBeCalledTimes(1)
+// expect(error.message).toMatchInlineSnapshot(`Custom element error`)
+// configure(originalConfig)
+// })
+
+test('when a promise is returned, it does not call the callback again until that promise rejects', async () => {
+ const sleep = t => new Promise(r => setTimeout(r, t))
+ const p1 = deferred()
+ const waitForCb = jest.fn(() => p1.promise)
+ const waitForPromise = waitFor(waitForCb, {interval: 1})
+ expect(waitForCb).toHaveBeenCalledTimes(1)
+ waitForCb.mockClear()
+ await sleep(50)
+ expect(waitForCb).toHaveBeenCalledTimes(0)
+
+ const p2 = deferred()
+ waitForCb.mockImplementation(() => p2.promise)
+
+ p1.reject('p1 rejection (should not fail this test)')
+ await sleep(50)
+
+ expect(waitForCb).toHaveBeenCalledTimes(1)
+ p2.resolve()
+
+ await waitForPromise
+})
+
+// test('when a promise is returned, if that is not resolved within the timeout, then waitFor is rejected', async () => {
+// const sleep = t => new Promise(r => setTimeout(r, t))
+// const {promise} = deferred()
+// const waitForError = waitFor(() => promise, {timeout: 1}).catch(e => e)
+// await sleep(5)
+//
+// expect((await waitForError).message).toMatchInlineSnapshot(`
+// Timed out in waitFor.
+//
+// Ignored nodes: comments, ,
+//
+//