Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
fix: ensure onToggle is called when on is controlled but setOnState i…
Browse files Browse the repository at this point in the history
…s called (#18)

* Ensure onToggle is called when on is controlled but setOnState is called

* Alter test to use setOn and setOff directly rather than trigger click to ensure code coverage

* Add bslinger to contributors table
  • Loading branch information
bslinger authored and Kent C. Dodds committed Mar 24, 2018
1 parent 7aa7805 commit f8e32df
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
12 changes: 11 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
"contributions": [
"code"
]
}
},
{
"login": "bslinger",
"name": "Ben Slinger",
"avatar_url": "https://avatars1.githubusercontent.com/u/603386?v=4",
"profile": "https://github.com/bslinger",
"contributions": [
"code",
"test"
]
}
]
}
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[![version][version-badge]][package]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Chat][chat-badge]][chat]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -196,11 +196,9 @@ are tons of them, so just
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Documentation") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Tests") | [<img src="https://avatars1.githubusercontent.com/u/9408641?v=4" width="100px;"/><br /><sub><b>Oliver</b></sub>](http://www.oliverjam.es)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") | [<img src="https://avatars2.githubusercontent.com/u/11708648?v=4" width="100px;"/><br /><sub><b>Jedrzej Lewandowski</b></sub>](http://www.thefullresolution.com/)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=TheFullResolution "Code") |
| :---: | :---: | :---: | :---: |

| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Documentation") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Tests") | [<img src="https://avatars1.githubusercontent.com/u/9408641?v=4" width="100px;"/><br /><sub><b>Oliver</b></sub>](http://www.oliverjam.es)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") | [<img src="https://avatars2.githubusercontent.com/u/11708648?v=4" width="100px;"/><br /><sub><b>Jedrzej Lewandowski</b></sub>](http://www.thefullresolution.com/)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=TheFullResolution "Code") | [<img src="https://avatars1.githubusercontent.com/u/603386?v=4" width="100px;"/><br /><sub><b>Ben Slinger</b></sub>](https://github.com/bslinger)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=bslinger "Code") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=bslinger "Tests") |
| :---: | :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,25 @@ test('onToggle gets called in controlled prop scenario', () => {
expect(spy).not.toHaveBeenCalled()
wrapper.setProps({on: true})
expect(spy).toHaveBeenCalled()
expect(spy.mock.calls.length).toBe(1)
})

test('onToggle gets called with fresh state in controlled prop scenario', () => {
const spy = jest.fn()
const {wrapper} = setup({on: false, onToggle: spy})
wrapper.setProps({on: true})
expect(spy).toHaveBeenLastCalledWith(true, expect.anything())
expect(spy.mock.calls.length).toBe(1)
})

test('onToggle gets called when setOnState is called in controlled prop scenario', () => {
const spy = jest.fn()
const {setOn, setOff} = setup({on: false, onToggle: spy})
setOff()
expect(spy).not.toHaveBeenCalled()
setOn()
expect(spy).toHaveBeenLastCalledWith(true, expect.anything())
expect(spy.mock.calls.length).toBe(1)
})

function setup({children = () => <div />, ...props} = {}) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class Toggle extends Component {
}

setOnState = (state = !this.getOn()) => {
if (this.getOn() !== state) {
this.props.onToggle(state, this.getTogglerStateAndHelpers())
}
this.setState({on: state})
}

Expand Down

0 comments on commit f8e32df

Please sign in to comment.