|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +// Because we're using fake timers here and I don't want these tests to run |
| 4 | +// for the actual length of the test (because it's waiting for a timeout error) |
| 5 | +// we'll mock the setTimeout, clearTimeout, and setImmediate to be the ones |
| 6 | +// that jest will mock for us. |
| 7 | +jest.mock('../helpers', () => { |
| 8 | + const actualHelpers = jest.requireActual('../helpers') |
| 9 | + return { |
| 10 | + ...actualHelpers, |
| 11 | + setTimeout, |
| 12 | + clearTimeout, |
| 13 | + setImmediate, |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +jest.useFakeTimers() |
| 18 | + |
| 19 | +// Because of the way jest mocking works here's the order of things (and no, the order of the code above doesn't make a difference): |
| 20 | +// 1. Just mocks '../helpers' and setTimeout/clearTimeout/setImmediate are set to their "correct" values |
| 21 | +// 2. We tell Jest to use fake timers |
| 22 | +// 3. We reset the modules and we mock '../helpers' again so now setTimeout/clearTimeout/setImmediate are set to their mocked values |
| 23 | +// We're only doing this because want to mock those values so this test doesn't take 4501ms to run. |
| 24 | +jest.resetModules() |
| 25 | + |
| 26 | +const { |
| 27 | + waitForElement, |
| 28 | + waitForDomChange, |
| 29 | + waitForElementToBeRemoved, |
| 30 | +} = require('../') |
| 31 | + |
| 32 | +test('waitForElementToBeRemoved: times out after 4500ms by default', () => { |
| 33 | + const {container} = render(`<div></div>`) |
| 34 | + const promise = expect( |
| 35 | + waitForElementToBeRemoved(() => container), |
| 36 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 37 | + `"Timed out in waitForElementToBeRemoved."`, |
| 38 | + ) |
| 39 | + jest.advanceTimersByTime(4501) |
| 40 | + return promise |
| 41 | +}) |
| 42 | + |
| 43 | +test('waitForElement: can time out', async () => { |
| 44 | + const promise = waitForElement(() => {}) |
| 45 | + jest.advanceTimersByTime(4600) |
| 46 | + await expect(promise).rejects.toThrow(/timed out/i) |
| 47 | +}) |
| 48 | + |
| 49 | +test('waitForElement: can specify our own timeout time', async () => { |
| 50 | + const promise = waitForElement(() => {}, {timeout: 4700}) |
| 51 | + const handler = jest.fn() |
| 52 | + promise.then(handler, handler) |
| 53 | + // advance beyond the default |
| 54 | + jest.advanceTimersByTime(4600) |
| 55 | + // promise was neither rejected nor resolved |
| 56 | + expect(handler).toHaveBeenCalledTimes(0) |
| 57 | + |
| 58 | + // advance beyond our specified timeout |
| 59 | + jest.advanceTimersByTime(150) |
| 60 | + |
| 61 | + // timed out |
| 62 | + await expect(promise).rejects.toThrow(/timed out/i) |
| 63 | +}) |
| 64 | + |
| 65 | +test('waitForDomChange: can time out', async () => { |
| 66 | + const promise = waitForDomChange() |
| 67 | + jest.advanceTimersByTime(4600) |
| 68 | + await expect(promise).rejects.toThrow(/timed out/i) |
| 69 | +}) |
| 70 | + |
| 71 | +test('waitForDomChange: can specify our own timeout time', async () => { |
| 72 | + const promise = waitForDomChange({timeout: 4700}) |
| 73 | + const handler = jest.fn() |
| 74 | + promise.then(handler, handler) |
| 75 | + // advance beyond the default |
| 76 | + jest.advanceTimersByTime(4600) |
| 77 | + // promise was neither rejected nor resolved |
| 78 | + expect(handler).toHaveBeenCalledTimes(0) |
| 79 | + |
| 80 | + // advance beyond our specified timeout |
| 81 | + jest.advanceTimersByTime(150) |
| 82 | + |
| 83 | + // timed out |
| 84 | + await expect(promise).rejects.toThrow(/timed out/i) |
| 85 | +}) |
0 commit comments