Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit 74a0dc8

Browse files
committed
test: add useSingleton tests
1 parent 844d092 commit 74a0dc8

File tree

5 files changed

+180
-4
lines changed

5 files changed

+180
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"dev": "parcel demo/index.html -d .devserver --no-cache",
1212
"build": "rollup --config",
13-
"test": "jest --coverage --silent",
13+
"test": "jest --coverage",
1414
"lint": "eslint \"{src,test}/**/*.js\"",
1515
"format": "prettier --write \"{src,test,demo}/**/*.{js,ts,json,css,md}\""
1616
},

test/Tippy.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22
import TippyBase from '../src';
33
import {render, cleanup} from '@testing-library/react';
44

5+
jest.useFakeTimers();
6+
57
afterEach(cleanup);
68

79
describe('<Tippy />', () => {
@@ -370,6 +372,18 @@ describe('<Tippy />', () => {
370372

371373
expect(instance.plugins).toMatchSnapshot();
372374
});
375+
376+
test('render prop', () => {
377+
render(
378+
<Tippy render={attrs => <div {...attrs}>Hello</div>} showOnCreate={true}>
379+
<button />
380+
</Tippy>,
381+
);
382+
383+
jest.runAllTimers();
384+
385+
expect(instance.popper).toMatchSnapshot();
386+
});
373387
});
374388

375389
describe('Tippy.propTypes', () => {

test/__snapshots__/Tippy.test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ Array [
2727
},
2828
]
2929
`;
30+
31+
exports[`<Tippy /> render prop 1`] = `
32+
<div
33+
data-tippy-root=""
34+
id="tippy-29"
35+
style="pointer-events: none; transition: none; position: absolute; left: 0px; top: 0px; margin: 0px;"
36+
>
37+
<div>
38+
Hello
39+
</div>
40+
</div>
41+
`;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`useSingleton headless mode updates content correctly 1`] = `
4+
<div
5+
data-tippy-root=""
6+
id="tippy-1"
7+
style="pointer-events: none; transition: none; position: absolute; left: 0px; top: 0px; margin: 0px;"
8+
>
9+
<div>
10+
a
11+
</div>
12+
</div>
13+
`;
14+
15+
exports[`useSingleton headless mode updates content correctly 2`] = `
16+
<div
17+
data-tippy-root=""
18+
id="tippy-1"
19+
style="pointer-events: none; transition: none; position: absolute; left: 0px; top: 0px; margin: 0px;"
20+
>
21+
<div>
22+
b
23+
</div>
24+
</div>
25+
`;

test/useSingleton.test.js

Lines changed: 128 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,135 @@
11
/* eslint-disable */
22
import React from 'react';
3-
import TippyBase, {useSingleton as useSingletonBase} from '../src';
4-
import {render, cleanup} from '@testing-library/react';
3+
import Tippy, {useSingleton} from '../src';
4+
import {useSingleton as useSingletonHeadless} from '../src/headless';
5+
import {render, cleanup, fireEvent} from '@testing-library/react';
56

67
jest.useFakeTimers();
78

89
afterEach(cleanup);
910

10-
it('works', () => {});
11+
let instance;
12+
function onCreate(i) {
13+
instance = i;
14+
}
15+
16+
it('changes the singleton content correctly', () => {
17+
function App() {
18+
const [source, target] = useSingleton();
19+
20+
return (
21+
<>
22+
<Tippy
23+
onCreate={onCreate}
24+
singleton={source}
25+
trigger="click"
26+
hideOnClick={false}
27+
/>
28+
<Tippy content="a" singleton={target}>
29+
<button data-testid="a" />
30+
</Tippy>
31+
<Tippy content="b" singleton={target}>
32+
<button data-testid="b" />
33+
</Tippy>
34+
</>
35+
);
36+
}
37+
38+
const {getByTestId} = render(<App />);
39+
40+
const buttonA = getByTestId('a');
41+
const buttonB = getByTestId('b');
42+
43+
fireEvent.click(buttonA);
44+
45+
expect(instance.state.isVisible).toBe(true);
46+
expect(instance.props.content.textContent).toBe('a');
47+
48+
fireEvent.click(buttonB);
49+
50+
expect(instance.props.content.textContent).toBe('b');
51+
});
52+
53+
describe('disabled prop', () => {
54+
function App({disabled}) {
55+
const [source, target] = useSingleton({disabled});
56+
57+
return (
58+
<>
59+
<Tippy
60+
onCreate={onCreate}
61+
singleton={source}
62+
trigger="click"
63+
hideOnClick={false}
64+
/>
65+
<Tippy content="a" singleton={target}>
66+
<button data-testid="a" />
67+
</Tippy>
68+
<Tippy content="b" singleton={target}>
69+
<button data-testid="b" />
70+
</Tippy>
71+
</>
72+
);
73+
}
74+
75+
it('it is set correctly on mount and on updates', () => {
76+
const {getByTestId, rerender} = render(<App disabled={true} />);
77+
const buttonA = getByTestId('a');
78+
79+
fireEvent.click(buttonA);
80+
81+
expect(instance.state.isVisible).toBe(false);
82+
83+
rerender(<App disabled={false} />);
84+
85+
fireEvent.click(buttonA);
86+
87+
expect(instance.state.isVisible).toBe(true);
88+
89+
rerender(<App disabled={true} />);
90+
91+
fireEvent.click(buttonA);
92+
93+
expect(instance.state.isVisible).toBe(false);
94+
});
95+
});
96+
97+
describe('useSingleton headless mode', () => {
98+
function App() {
99+
const [source, target] = useSingletonHeadless();
100+
101+
return (
102+
<>
103+
<Tippy
104+
onCreate={onCreate}
105+
render={(attrs, content) => <div {...attrs}>{content}</div>}
106+
singleton={source}
107+
trigger="click"
108+
hideOnClick={false}
109+
/>
110+
<Tippy content="a" singleton={target}>
111+
<button data-testid="a" />
112+
</Tippy>
113+
<Tippy content="b" singleton={target}>
114+
<button data-testid="b" />
115+
</Tippy>
116+
</>
117+
);
118+
}
119+
120+
it('updates content correctly', () => {
121+
const {getByTestId} = render(<App />);
122+
123+
const buttonA = getByTestId('a');
124+
const buttonB = getByTestId('b');
125+
126+
fireEvent.click(buttonA);
127+
128+
expect(instance.state.isVisible).toBe(true);
129+
expect(instance.popper).toMatchSnapshot();
130+
131+
fireEvent.click(buttonB);
132+
133+
expect(instance.popper).toMatchSnapshot();
134+
});
135+
});

0 commit comments

Comments
 (0)