-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
39 lines (33 loc) · 1.12 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @jest-environment jsdom
*/
import { act, renderHook } from '@testing-library/react';
import { describe, expect, afterEach, jest, test } from '@jest/globals';
import { useRefEffect } from './index';
describe('useRefEffect', () => {
const uneffect = jest.fn(() => console.log(`detached`));
const effect = jest.fn((el: Element) => {
console.log(`${el} attached`);
return uneffect.bind(null, el);
});
const instance = document.createElement('div');
test('should update ref, and call listeners correctly', () => {
const {
result: { current: ref },
} = renderHook(() => useRefEffect(effect));
expect(effect).not.toHaveBeenCalled();
expect(uneffect).not.toHaveBeenCalled();
act(() => ref(instance));
expect(effect).toHaveBeenCalledTimes(1);
expect(effect).toHaveBeenCalledWith(instance);
expect(uneffect).not.toHaveBeenCalled();
act(() => ref(null));
expect(effect).toHaveBeenCalledTimes(1);
expect(uneffect).toHaveBeenCalledTimes(1);
expect(uneffect).toHaveBeenCalledWith(instance);
});
afterEach(() => {
effect.mockClear();
uneffect.mockClear();
});
});