|
1 |
| -import {waitForElementToBeRemoved} from '../' |
2 | 1 | import {renderIntoDocument} from './helpers/test-utils'
|
3 | 2 |
|
| 3 | +function importModule() { |
| 4 | + return require('../').waitForElementToBeRemoved |
| 5 | +} |
| 6 | + |
| 7 | +let waitForElementToBeRemoved |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + jest.useRealTimers() |
| 11 | + jest.resetModules() |
| 12 | + waitForElementToBeRemoved = importModule() |
| 13 | +}) |
| 14 | + |
4 | 15 | test('resolves on mutation only when the element is removed', async () => {
|
5 | 16 | const {queryAllByTestId} = renderIntoDocument(`
|
6 | 17 | <div data-testid="div"></div>
|
@@ -57,3 +68,52 @@ test('requires an unempty array of elements to exist first', () => {
|
57 | 68 | `"The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal."`,
|
58 | 69 | )
|
59 | 70 | })
|
| 71 | + |
| 72 | +describe('timers', () => { |
| 73 | + const expectElementToBeRemoved = async () => { |
| 74 | + const importedWaitForElementToBeRemoved = importModule() |
| 75 | + |
| 76 | + const {queryAllByTestId} = renderIntoDocument(` |
| 77 | + <div data-testid="div"></div> |
| 78 | + <div data-testid="div"></div> |
| 79 | +`) |
| 80 | + const divs = queryAllByTestId('div') |
| 81 | + // first mutation |
| 82 | + setTimeout(() => { |
| 83 | + divs.forEach(d => d.setAttribute('id', 'mutated')) |
| 84 | + }) |
| 85 | + // removal |
| 86 | + setTimeout(() => { |
| 87 | + divs.forEach(div => div.parentElement.removeChild(div)) |
| 88 | + }, 100) |
| 89 | + |
| 90 | + const promise = importedWaitForElementToBeRemoved( |
| 91 | + () => queryAllByTestId('div'), |
| 92 | + { |
| 93 | + timeout: 200, |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + if (setTimeout._isMockFunction) { |
| 98 | + jest.advanceTimersByTime(110) |
| 99 | + } |
| 100 | + |
| 101 | + await promise |
| 102 | + } |
| 103 | + |
| 104 | + it('works with real timers', async () => { |
| 105 | + jest.useRealTimers() |
| 106 | + await expectElementToBeRemoved() |
| 107 | + }) |
| 108 | + it('works with fake timers', async () => { |
| 109 | + jest.useFakeTimers() |
| 110 | + await expectElementToBeRemoved() |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +test("doesn't change jest's timers value when importing the module", () => { |
| 115 | + jest.useFakeTimers() |
| 116 | + importModule() |
| 117 | + |
| 118 | + expect(window.setTimeout._isMockFunction).toEqual(true) |
| 119 | +}) |
0 commit comments