finish overlayscrollbars-react

This commit is contained in:
Rene Haas
2022-11-01 13:25:44 +01:00
parent fe6262939e
commit 3c882f5475
7 changed files with 197 additions and 31 deletions
@@ -122,4 +122,50 @@ describe('OverlayScrollbarsComponent', () => {
expect(onUpdatedInitial).toHaveBeenCalledTimes(2);
expect(onUpdated).toHaveBeenCalledTimes(2);
});
test('events', () => {
const ref: RefObject<OverlayScrollbarsComponentRef> = { current: null };
const onUpdatedInitial = vitest.fn();
const onUpdated = vitest.fn();
const { rerender } = render(
<OverlayScrollbarsComponent events={{ updated: onUpdatedInitial }} ref={ref} />
);
expect(onUpdatedInitial).toHaveBeenCalledTimes(1);
rerender(<OverlayScrollbarsComponent events={{ updated: onUpdated }} ref={ref} />);
expect(onUpdated).not.toHaveBeenCalled();
ref.current!.instance()!.update(true);
expect(onUpdatedInitial).toHaveBeenCalledTimes(1);
expect(onUpdated).toHaveBeenCalledTimes(1);
rerender(<OverlayScrollbarsComponent events={{ updated: onUpdatedInitial }} ref={ref} />);
ref.current!.instance()!.update(true);
expect(onUpdatedInitial).toHaveBeenCalledTimes(2);
expect(onUpdated).toHaveBeenCalledTimes(1);
rerender(<OverlayScrollbarsComponent events={{ updated: onUpdated }} ref={ref} />);
ref.current!.instance()!.update(true);
expect(onUpdatedInitial).toHaveBeenCalledTimes(2);
expect(onUpdated).toHaveBeenCalledTimes(2);
rerender(
<OverlayScrollbarsComponent events={{ updated: [onUpdated, onUpdatedInitial] }} ref={ref} />
);
ref.current!.instance()!.update(true);
expect(onUpdatedInitial).toHaveBeenCalledTimes(3);
expect(onUpdated).toHaveBeenCalledTimes(3);
// unregister works with `[]`, `null` or `undefined`
rerender(<OverlayScrollbarsComponent events={{ updated: null }} ref={ref} />);
ref.current!.instance()!.update(true);
expect(onUpdatedInitial).toHaveBeenCalledTimes(3);
expect(onUpdated).toHaveBeenCalledTimes(3);
});
});
@@ -0,0 +1,43 @@
import { useRef } from 'react';
import { describe, test, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useOverlayScrollbars } from '~/overlayscrollbars-react';
import type { OverlayScrollbars } from 'overlayscrollbars';
describe('useOverlayScrollbars', () => {
test('re-initialization', () => {
const Test = () => {
const instanceRef = useRef<OverlayScrollbars | null>(null);
const [initialize, instance] = useOverlayScrollbars();
return (
<>
<button
onClick={(event) => {
const osInstance = initialize(event.target as HTMLElement);
if (instanceRef.current) {
expect(instanceRef.current).toBe(osInstance);
expect(instanceRef.current).toBe(instance());
}
instanceRef.current = osInstance;
expect(instanceRef.current).toBe(instance());
}}
>
initialize
</button>
</>
);
};
render(<Test />);
const initializeBtn = screen.getByRole('button');
userEvent.click(initializeBtn);
// taking snapshot here wouldn't be equal because of "tabindex" attribute of the viewport element
userEvent.click(initializeBtn);
const snapshot = initializeBtn.innerHTML;
userEvent.click(initializeBtn);
expect(snapshot).toBe(initializeBtn.innerHTML);
});
});