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

Commit eb45eab

Browse files
committed
Use rerender() for tests
1 parent 0fae719 commit eb45eab

File tree

1 file changed

+73
-78
lines changed

1 file changed

+73
-78
lines changed

test/Tippy.test.js

Lines changed: 73 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import Tippy from '../src/Tippy'
33
import ReactDOMServer from 'react-dom/server'
4-
import { render, fireEvent, cleanup } from 'react-testing-library'
4+
import { render, cleanup } from 'react-testing-library'
55

66
afterEach(cleanup)
77

@@ -49,8 +49,8 @@ describe('<Tippy />', () => {
4949
<button />
5050
</Tippy>,
5151
)
52-
const tip = container.querySelector('button')._tippy
53-
expect(tip.popper.querySelector('strong')).not.toBeNull()
52+
const instance = container.querySelector('button')._tippy
53+
expect(instance.popper.querySelector('strong')).not.toBeNull()
5454
})
5555

5656
test('unmount destroys the tippy instance and allows garbage collection', () => {
@@ -64,34 +64,20 @@ describe('<Tippy />', () => {
6464
expect(button._tippy).toBeUndefined()
6565
})
6666

67-
test('updating state updates the tippy instance', done => {
68-
function App() {
69-
const [arrow, setArrow] = React.useState(false)
70-
const [interactive, setInteractive] = React.useState(false)
71-
const ref = React.useRef()
72-
73-
React.useEffect(() => {
74-
const instance = ref.current._tippy
75-
expect(instance.props.arrow).toBe(arrow)
76-
expect(instance.props.interactive).toBe(interactive)
77-
done()
78-
})
79-
80-
function handleClick() {
81-
setArrow(true)
82-
setInteractive(true)
83-
}
84-
85-
return (
86-
<Tippy content="tooltip" arrow={arrow} interactive={interactive}>
87-
<button ref={ref} onClick={handleClick} />
88-
</Tippy>
89-
)
90-
}
91-
92-
const { container } = render(<App />)
93-
const button = container.querySelector('button')
94-
fireEvent.click(button)
67+
test('updating props updates the tippy instance', () => {
68+
const { container, rerender } = render(
69+
<Tippy content="tooltip" arrow={false}>
70+
<button />
71+
</Tippy>,
72+
)
73+
const instance = container.querySelector('button')._tippy
74+
expect(instance.props.arrow).toBe(false)
75+
rerender(
76+
<Tippy content="tooltip" arrow={true}>
77+
<button />
78+
</Tippy>,
79+
)
80+
expect(instance.props.arrow).toBe(true)
9581
})
9682

9783
test('component as a child', () => {
@@ -152,58 +138,67 @@ describe('<Tippy />', () => {
152138
)
153139
})
154140

155-
test('props.isEnabled initially `true`', done => {
156-
booleanPropsBoilerplate('isEnabled', true, done)
141+
test('props.isEnabled initially `true`', () => {
142+
const { container, rerender } = render(
143+
<Tippy content="tooltip" isEnabled={true}>
144+
<button />
145+
</Tippy>,
146+
)
147+
const instance = container.querySelector('button')._tippy
148+
expect(instance.state.isEnabled).toBe(true)
149+
rerender(
150+
<Tippy content="tooltip" isEnabled={false}>
151+
<button />
152+
</Tippy>,
153+
)
154+
expect(instance.state.isEnabled).toBe(false)
157155
})
158156

159-
test('props.isEnabled initially `false`', done => {
160-
booleanPropsBoilerplate('isEnabled', false, done)
157+
test('props.isEnabled initially `false`', () => {
158+
const { container, rerender } = render(
159+
<Tippy content="tooltip" isEnabled={false}>
160+
<button />
161+
</Tippy>,
162+
)
163+
const instance = container.querySelector('button')._tippy
164+
expect(instance.state.isEnabled).toBe(false)
165+
rerender(
166+
<Tippy content="tooltip" isEnabled={true}>
167+
<button />
168+
</Tippy>,
169+
)
170+
expect(instance.state.isEnabled).toBe(true)
161171
})
162172

163-
test('props.isVisible initially `true`', done => {
164-
booleanPropsBoilerplate('isVisible', true, done)
173+
test('props.isVisible initially `true`', () => {
174+
const { container, rerender } = render(
175+
<Tippy content="tooltip" isVisible={true}>
176+
<button />
177+
</Tippy>,
178+
)
179+
const instance = container.querySelector('button')._tippy
180+
expect(instance.state.isVisible).toBe(true)
181+
rerender(
182+
<Tippy content="tooltip" isVisible={false}>
183+
<button />
184+
</Tippy>,
185+
)
186+
expect(instance.state.isVisible).toBe(false)
165187
})
166188

167-
test('props.isVisible initially `false`', done => {
168-
booleanPropsBoilerplate('isVisible', false, done)
189+
test('props.isVisible initially `false`', () => {
190+
const { container, rerender } = render(
191+
<Tippy content="tooltip" isVisible={false}>
192+
<button />
193+
</Tippy>,
194+
)
195+
const instance = container.querySelector('button')._tippy
196+
expect(instance.state.isVisible).toBe(false)
197+
rerender(
198+
<Tippy content="tooltip" isVisible={true}>
199+
<button />
200+
</Tippy>,
201+
)
202+
expect(instance.state.isVisible).toBe(true)
169203
})
170204
})
171-
172-
// ************************************************************
173-
174-
function booleanPropsBoilerplate(prop, bool, done) {
175-
function App() {
176-
const [value, setValue] = React.useState(bool)
177-
const ref = React.useRef()
178-
const passes = React.useRef(0)
179-
180-
React.useEffect(() => {
181-
const instance = ref.current._tippy
182-
expect(instance.state[prop]).toBe(value)
183-
184-
passes.current++
185-
186-
if (passes.current === 2) {
187-
done()
188-
}
189-
})
190-
191-
function handleClick() {
192-
setValue(!bool)
193-
}
194-
195-
const props = {
196-
[prop]: value,
197-
}
198-
199-
return (
200-
<Tippy content="tooltip" {...props}>
201-
<button ref={ref} onClick={handleClick} />
202-
</Tippy>
203-
)
204-
}
205-
206-
const { container } = render(<App />)
207-
const button = container.querySelector('button')
208-
fireEvent.click(button)
209-
}

0 commit comments

Comments
 (0)