Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds test for unmount function; Adds example of useEffect
  • Loading branch information
Andrewmat committed Feb 9, 2019
commit 53d2b3844f8fcd4c1332a58f44d5f5d97c58bf9c
91 changes: 57 additions & 34 deletions examples/__tests__/react-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,76 @@
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing you had to skip the pre-commit hook or something. This should have auto-formatted when you commit it. Could you run npx kcd-scripts format please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran it and it added a linebreak in ISSUE_TEMPLATE.md and mock.react-transition-group.js. Should I also add it to the commit?
captura de tela de 2019-02-10 12-48-41
captura de tela de 2019-02-10 12-48-23

import {testHook, act, cleanup} from 'react-testing-library'

import useCounter from '../react-hooks'
import { useCounter, useDocumentTitle } from '../react-hooks'

afterEach(cleanup)

test('accepts default initial values', () => {
let count
testHook(() => ({count} = useCounter()))
describe('useCounter', () => {
test('accepts default initial values', () => {
let count
testHook(() => ({count} = useCounter()))

expect(count).toBe(0)
})
expect(count).toBe(0)
})

test('accepts a default initial value for `count`', () => {
let count
testHook(() => ({count} = useCounter({})))
test('accepts a default initial value for `count`', () => {
let count
testHook(() => ({count} = useCounter({})))

expect(count).toBe(0)
})
expect(count).toBe(0)
})

test('provides an `increment` function', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({step: 2})))
test('provides an `increment` function', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({step: 2})))

expect(count).toBe(0)
act(() => {
increment()
expect(count).toBe(0)
act(() => {
increment()
})
expect(count).toBe(2)
})
expect(count).toBe(2)
})

test('provides an `decrement` function', () => {
let count, decrement
testHook(() => ({count, decrement} = useCounter({step: 2})))
test('provides an `decrement` function', () => {
let count, decrement
testHook(() => ({count, decrement} = useCounter({step: 2})))

expect(count).toBe(0)
act(() => {
decrement()
expect(count).toBe(0)
act(() => {
decrement()
})
expect(count).toBe(-2)
})
expect(count).toBe(-2)
})

test('accepts a default initial value for `step`', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({})))
test('accepts a default initial value for `step`', () => {
let count, increment
testHook(() => ({count, increment} = useCounter({})))

expect(count).toBe(0)
act(() => {
increment()
expect(count).toBe(0)
act(() => {
increment()
})
expect(count).toBe(1)
})
expect(count).toBe(1)
})

describe('useDocumentTitle', () => {
test('sets a title', () => {
document.title = 'original title'
testHook(() => {
useDocumentTitle('modified title')
})

expect(document.title).toBe('modified title')
})

test('returns to original title when component is unmounted', () => {
document.title = 'original title'
const { unmount } = testHook(() => {
useDocumentTitle('modified title')
})

unmount()
expect(document.title).toBe('original title')
})
})
15 changes: 12 additions & 3 deletions examples/react-hooks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {useState} from 'react'
import {useState, useEffect} from 'react'

function useCounter({initialCount = 0, step = 1} = {}) {
export function useCounter({initialCount = 0, step = 1} = {}) {
const [count, setCount] = useState(initialCount)
const increment = () => setCount(c => c + step)
const decrement = () => setCount(c => c - step)
return {count, increment, decrement}
}

export default useCounter
export function useDocumentTitle(title) {
const [originalTitle, setOriginalTitle] = useState(document.title)
useEffect(() => {
setOriginalTitle(document.title)
document.title = title
return () => {
document.title = originalTitle
}
}, [title])
}
16 changes: 15 additions & 1 deletion src/__tests__/test-hook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useState} from 'react'
import {useState, useEffect} from 'react'
import 'jest-dom/extend-expect'
import {testHook, cleanup} from '../'

Expand All @@ -12,3 +12,17 @@ test('testHook calls the callback', () => {
test('confirm we can safely call a React Hook from within the callback', () => {
testHook(() => useState())
})
test('returns a function to unmount the component', () => {
let isMounted
const { unmount } = testHook(() => {
useEffect(() => {
isMounted = true
return () => {
isMounted = false
}
})
})
expect(isMounted).toBe(true)
unmount()
expect(isMounted).toBe(false)
})
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function TestHook({callback}) {

function testHook(callback) {
const { unmount } = render(<TestHook callback={callback} />)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also return rerender as well I think.

return unmount
return { unmount }
}

function cleanup() {
Expand Down