diff --git a/.all-contributorsrc b/.all-contributorsrc index 8871e62..36ff4f2 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" + ] + } ] } diff --git a/README.md b/README.md index 7650843..e43e494 100644 --- a/README.md +++ b/README.md @@ -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] @@ -196,11 +196,9 @@ are tons of them, so just Thanks goes to these people ([emoji key][emojis]): - -| [
Kent C. Dodds](https://kentcdodds.com)
[💻](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") | [
Frank Tan](https://github.com/tansongyang)
[💻](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") | [
Oliver](http://www.oliverjam.es)
[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") | [
Jedrzej Lewandowski](http://www.thefullresolution.com/)
[💻](https://github.com/kentcdodds/react-toggled/commits?author=TheFullResolution "Code") | -| :---: | :---: | :---: | :---: | - +| [
Kent C. Dodds](https://kentcdodds.com)
[💻](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") | [
Frank Tan](https://github.com/tansongyang)
[💻](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") | [
Oliver](http://www.oliverjam.es)
[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") | [
Jedrzej Lewandowski](http://www.thefullresolution.com/)
[💻](https://github.com/kentcdodds/react-toggled/commits?author=TheFullResolution "Code") | [
Ben Slinger](https://github.com/bslinger)
[💻](https://github.com/kentcdodds/react-toggled/commits?author=bslinger "Code") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=bslinger "Tests") | +| :---: | :---: | :---: | :---: | :---: | This project follows the [all-contributors][all-contributors] specification. diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 46f4ea3..3dd5999 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -159,6 +159,7 @@ 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', () => { @@ -166,6 +167,17 @@ test('onToggle gets called with fresh state in controlled prop scenario', () => 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 = () =>
, ...props} = {}) { diff --git a/src/index.js b/src/index.js index 4b9a398..15bd57a 100644 --- a/src/index.js +++ b/src/index.js @@ -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}) }