一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
56 行
1.6 KiB
TypeScript
56 行
1.6 KiB
TypeScript
import { act, renderHook } from '@testing-library/react';
|
|
import useRootElement from './useRootElement';
|
|
|
|
describe('useRootElement', () => {
|
|
beforeEach(() => {
|
|
jest.useFakeTimers({ advanceTimers: true });
|
|
});
|
|
|
|
test('should find an element with a matching ID', async () => {
|
|
const testElement = document.createElement('div');
|
|
testElement.id = 'test-element-id';
|
|
document.body.appendChild(testElement);
|
|
|
|
const { result } = renderHook(useRootElement, {
|
|
initialProps: testElement.id,
|
|
});
|
|
await act(async () => {
|
|
await jest.advanceTimersByTimeAsync(100);
|
|
});
|
|
expect(result.current).toBe(testElement);
|
|
|
|
testElement.remove();
|
|
});
|
|
|
|
test('should redo the element search when the elementId prop changes', async () => {
|
|
const testElement = document.createElement('div');
|
|
document.body.appendChild(testElement);
|
|
|
|
const { rerender, result } = renderHook(useRootElement, {
|
|
initialProps: 'some-id-here',
|
|
});
|
|
await jest.advanceTimersByTimeAsync(100);
|
|
expect(result.current).toBe(null);
|
|
|
|
// Searching for another non-existent ID: Should not match
|
|
rerender('updated-id');
|
|
await jest.advanceTimersByTimeAsync(100);
|
|
expect(result.current).toBe(null);
|
|
|
|
// Should not match the first element if its ID is set to the original (search
|
|
// should be cancelled).
|
|
testElement.id = 'some-id-here';
|
|
await jest.advanceTimersByTimeAsync(100);
|
|
expect(result.current).toBe(null);
|
|
|
|
// Should match if the element ID changes to the updated ID.
|
|
await act(async () => {
|
|
testElement.id = 'updated-id';
|
|
await jest.advanceTimersByTimeAsync(100);
|
|
});
|
|
expect(result.current).toBe(testElement);
|
|
|
|
testElement.remove();
|
|
});
|
|
});
|