aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts')
-rw-r--r--src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts b/src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts
new file mode 100644
index 0000000..982c0ee
--- /dev/null
+++ b/src/utils/hooks/use-on-click-outside/use-on-click-outside.test.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it, jest } from '@jest/globals';
+import { renderHook } from '@testing-library/react';
+import { userEvent } from '@testing-library/user-event';
+import { useOnClickOutside } from './use-on-click-outside';
+
+describe('useOnClickOutside', () => {
+ it('can execute a function on click outside the given ref', async () => {
+ const user = userEvent.setup();
+ const cb = jest.fn();
+ const wrapper = document.createElement('div');
+ const el = document.createElement('div');
+
+ wrapper.append(el);
+ document.body.append(wrapper);
+
+ const { result } = renderHook(() => useOnClickOutside<HTMLDivElement>(cb));
+
+ result.current.current = el;
+
+ await user.click(wrapper);
+
+ expect(cb).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not execute the callback on click inside the given ref', async () => {
+ const user = userEvent.setup();
+ const cb = jest.fn();
+ const wrapper = document.createElement('div');
+ const el = document.createElement('div');
+
+ wrapper.append(el);
+ document.body.append(wrapper);
+
+ const { result } = renderHook(() => useOnClickOutside(cb));
+
+ result.current.current = wrapper;
+
+ await user.click(el);
+
+ expect(cb).not.toHaveBeenCalled();
+ });
+});