Skip to content

Commit

Permalink
Merge pull request #35 from luukdv/toggled-undefined
Browse files Browse the repository at this point in the history
Check if state value is defined
Luuk de Vlieger authored May 9, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents ff95fe9 + e867a06 commit 3658f13
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/Burger.tsx
Original file line number Diff line number Diff line change
@@ -65,15 +65,13 @@ export const Burger = (({
barStyles['borderRadius'] = '9em'
}

const toggleFunction = toggle ? toggle : toggleInternal
const isToggled = toggled ? toggled : toggledInternal
const toggleFunction = toggle || toggleInternal
const isToggled = toggled !== undefined ? toggled : toggledInternal

const handler = () => {
toggleFunction(!isToggled)

if (typeof onToggle === 'function') {
onToggle(!isToggled)
}
if (typeof onToggle === 'function') onToggle(!isToggled)
}

return render({
14 changes: 12 additions & 2 deletions tests/toggled.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import React from 'react'
import Hamburger from '../src'
import { render, screen } from '@testing-library/react'
import { fireEvent, render, screen } from '@testing-library/react'

it(`accepts state value`, () => {
it(`accepts "true" as individual state value`, () => {
render(<Hamburger toggled={true} />)
expect(screen.getByTestId('bar-two')).toHaveStyle({ transform: 'scaleX(0)' })

fireEvent.click(screen.getByTestId('tilt'))
expect(screen.getByTestId('bar-two')).toHaveStyle({ transform: 'scaleX(0)' })
})

it(`accepts "false" as individual state value`, () => {
render(<Hamburger toggled={false} />)
expect(screen.getByTestId('bar-two')).toHaveStyle({ transform: 'none' })

fireEvent.click(screen.getByTestId('tilt'))
expect(screen.getByTestId('bar-two')).toHaveStyle({ transform: 'none' })
})

0 comments on commit 3658f13

Please sign in to comment.