import React, {useState} from 'react';
import Tippy, {useSingleton} from '../src';
import TippyHeadless, {
useSingleton as useSingletonHeadless,
} from '../src/headless';
import {render, cleanup, fireEvent} from '@testing-library/react';
jest.useFakeTimers();
afterEach(cleanup);
let instance;
function onCreate(i) {
instance = i;
}
it('changes the singleton content correctly', () => {
function App() {
const [source, target] = useSingleton();
return (
<>
>
);
}
const {getByTestId} = render();
const buttonA = getByTestId('a');
const buttonB = getByTestId('b');
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(true);
expect(instance.props.content.textContent).toBe('a');
fireEvent.click(buttonB);
expect(instance.props.content.textContent).toBe('b');
});
it('updates `className` correctly', () => {
function App({className}) {
const [source, target] = useSingleton();
return (
<>
>
);
}
const {rerender} = render();
expect(
instance.popper.firstElementChild.className.includes('some class names'),
).toBe(true);
rerender();
expect(
instance.popper.firstElementChild.className.includes('other names'),
).toBe(true);
});
it('errors if source variable has not been passed to a ', () => {
const spy = jest.spyOn(console, 'error');
function App1() {
// eslint-disable-next-line
const [source, target] = useSingleton();
return (
<>
>
);
}
function App2() {
const [source, target] = useSingleton();
return (
<>
>
);
}
render();
expect(spy).toHaveBeenCalledWith(
[
'@tippyjs/react: The `source` variable from `useSingleton()` has',
'not been passed to a component.',
].join(' '),
);
spy.mockReset();
render();
expect(spy).not.toHaveBeenCalled();
});
describe('disabled prop', () => {
function App({disabled}) {
const [source, target] = useSingleton({disabled});
return (
<>
>
);
}
it('it is set correctly on mount and on updates', () => {
const {getByTestId, rerender} = render();
const buttonA = getByTestId('a');
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(false);
rerender();
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(true);
rerender();
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(false);
});
});
it('when new Tippys are added to DOM, they are registered with singleton', () => {
const TippyCreator = ({target}) => {
const [isTippyAdded, setIsTippyAdded] = useState(false);
return (
);
};
function App() {
const [source, target] = useSingleton();
return (
<>
>
);
}
const {getByTestId} = render();
const addTippyButton = getByTestId('add-tippy');
fireEvent.click(addTippyButton);
const buttonA = getByTestId('a');
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(true);
expect(instance.props.content.textContent).toBe('a');
});
describe('useSingleton headless mode', () => {
function App() {
const [source, target] = useSingletonHeadless();
return (
<>
{content}
}
singleton={source}
trigger="click"
hideOnClick={false}
/>
>
);
}
it('updates content correctly', () => {
const {getByTestId} = render();
const buttonA = getByTestId('a');
const buttonB = getByTestId('b');
fireEvent.click(buttonA);
expect(instance.state.isVisible).toBe(true);
expect(instance.popper).toMatchSnapshot();
fireEvent.click(buttonB);
expect(instance.popper).toMatchSnapshot();
});
});