{
setState(value)
handleChange(value)
}}
/>
)
}
render()
// Ensure checkbox is off
assertSwitch({ state: SwitchState.Off })
// Toggle
await click(getSwitch())
// Ensure state is on
assertSwitch({ state: SwitchState.On })
// Toggle
await click(getSwitch())
// Ensure state is off
assertSwitch({ state: SwitchState.Off })
})
it('should be possible to toggle the Switch with a click on the Label', async () => {
let handleChange = jest.fn()
function Example() {
let [state, setState] = useState(false)
return (
{
setState(value)
handleChange(value)
}}
/>
The label
)
}
render()
// Ensure checkbox is off
assertSwitch({ state: SwitchState.Off })
// Toggle
await click(getSwitchLabel())
// Ensure the switch is focused
assertActiveElement(getSwitch())
// Ensure state is on
assertSwitch({ state: SwitchState.On })
// Toggle
await click(getSwitchLabel())
// Ensure the switch is focused
assertActiveElement(getSwitch())
// Ensure state is off
assertSwitch({ state: SwitchState.Off })
})
it('should not be possible to toggle the Switch with a click on the Label (passive)', async () => {
let handleChange = jest.fn()
function Example() {
let [state, setState] = useState(false)
return (
{
setState(value)
handleChange(value)
}}
/>
The label
)
}
render()
// Ensure checkbox is off
assertSwitch({ state: SwitchState.Off })
// Toggle
await click(getSwitchLabel())
// Ensure state is still off
assertSwitch({ state: SwitchState.Off })
})
xit('should be possible to hover the label and trigger a hover on the switch', async () => {
// This test doesn't work in JSDOM :(
// Keeping it here for reference when we can test this in a real browser
function Example() {
let [state] = useState(false)
return (
The label
)
}
render()
// Verify the switch is not hovered
expect(window.getComputedStyle(getSwitch()!).backgroundColor).toBe('rgb(0, 255, 0)')
// Hover over the *label*
await mouseEnter(getSwitchLabel())
// Make sure the switch gets hover styles
expect(window.getComputedStyle(getSwitch()!).backgroundColor).toBe('rgb(255, 0, 0)')
})
})
describe('Form compatibility', () => {
it('should be possible to set the `form`, which is forwarded to the hidden inputs', async () => {
let submits = jest.fn()
function Example() {
let [state, setState] = useState(false)
return (
Enable notifications
)
}
render()
// Toggle
await click(getSwitchLabel())
// Submit the form again
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([['notifications', 'on']])
})
it('should be possible to submit a form with an boolean value', async () => {
let submits = jest.fn()
function Example() {
let [state, setState] = useState(false)
return (
)
}
render()
// Submit the form
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([]) // no data
// Toggle
await click(getSwitchLabel())
// Submit the form again
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([['notifications', 'on']])
})
it('should be possible to submit a form with a provided string value', async () => {
let submits = jest.fn()
function Example() {
let [state, setState] = useState(false)
return (
)
}
render()
// Submit the form
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([]) // no data
// Toggle
await click(getSwitchLabel())
// Submit the form again
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([['fruit', 'apple']])
})
it('should not submit the data if the Switch is disabled', async () => {
let submits = jest.fn()
function Example() {
let [state, setState] = useState(true)
return (
)
}
render()
// Submit the form
await click(getByText('Submit'))
// Verify that the form has been submitted
expect(submits).toHaveBeenLastCalledWith([
['foo', 'bar'], // The only available field
])
})
})