-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(forms): add support for disabled states
- Loading branch information
1 parent
0f4d7bc
commit e8d301c
Showing
13 changed files
with
242 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 170 additions & 2 deletions
172
packages/dnb-eufemia/src/extensions/forms/Field/Toggle/__tests__/Toggle.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,178 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import Toggle, { Props } from '..' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import Toggle, { Props } from '../Toggle' | ||
|
||
describe('Field.Toggle', () => { | ||
it('should render with props', () => { | ||
const props: Props = { valueOn: 'checked', valueOff: 'unchecked' } | ||
render(<Toggle {...props} />) | ||
}) | ||
|
||
it('should support disabled prop', () => { | ||
const { rerender } = render( | ||
<Toggle | ||
valueOn="checked" | ||
valueOff="unchecked" | ||
label="Disabled label" | ||
disabled | ||
/> | ||
) | ||
|
||
const labelElement = () => document.querySelector('label') | ||
|
||
expect(labelElement()).toHaveAttribute('disabled') | ||
|
||
rerender( | ||
<Toggle | ||
valueOn="checked" | ||
valueOff="unchecked" | ||
label="Disabled label" | ||
/> | ||
) | ||
|
||
expect(labelElement()).not.toHaveAttribute('disabled') | ||
}) | ||
|
||
describe('variants', () => { | ||
describe('button', () => { | ||
it('should render correct HTML', () => { | ||
const onChange = jest.fn() | ||
|
||
render( | ||
<Toggle | ||
valueOn="on" | ||
valueOff="off" | ||
variant="button" | ||
value="on" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
const element = document.querySelector( | ||
'.dnb-toggle-button__button' | ||
) | ||
|
||
expect(element).toBeInTheDocument() | ||
expect(element).toHaveAttribute('aria-pressed', 'true') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).toHaveAttribute('aria-pressed', 'false') | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
expect(onChange).toHaveBeenLastCalledWith('off') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).toHaveAttribute('aria-pressed', 'true') | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
expect(onChange).toHaveBeenLastCalledWith('on') | ||
}) | ||
}) | ||
|
||
describe('buttons', () => { | ||
it('should render correct HTML', () => { | ||
const onChange = jest.fn() | ||
|
||
render( | ||
<Toggle | ||
valueOn="on" | ||
valueOff="off" | ||
variant="buttons" | ||
value="on" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
const [yesElement, noElement]: Array<HTMLButtonElement> = | ||
Array.from( | ||
document.querySelectorAll('.dnb-toggle-button__button') | ||
) | ||
|
||
expect(yesElement).toHaveAttribute('aria-pressed', 'true') | ||
expect(noElement).toHaveAttribute('aria-pressed', 'false') | ||
|
||
fireEvent.click(noElement) | ||
|
||
expect(yesElement).toHaveAttribute('aria-pressed', 'false') | ||
expect(noElement).toHaveAttribute('aria-pressed', 'true') | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
expect(onChange).toHaveBeenLastCalledWith('off') | ||
|
||
fireEvent.click(yesElement) | ||
|
||
expect(yesElement).toHaveAttribute('aria-pressed', 'true') | ||
expect(noElement).toHaveAttribute('aria-pressed', 'false') | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
expect(onChange).toHaveBeenLastCalledWith('on') | ||
}) | ||
}) | ||
|
||
describe('checkbox-button', () => { | ||
it('should render correct HTML', () => { | ||
const onChange = jest.fn() | ||
|
||
render( | ||
<Toggle | ||
valueOn="on" | ||
valueOff="off" | ||
variant="checkbox-button" | ||
value="on" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
const element = document.querySelector( | ||
'.dnb-toggle-button__button .dnb-checkbox__input' | ||
) | ||
|
||
expect(element).toBeInTheDocument() | ||
expect(element).toHaveAttribute('data-checked', 'true') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).toHaveAttribute('data-checked', 'false') | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
expect(onChange).toHaveBeenLastCalledWith('off') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).toHaveAttribute('data-checked', 'true') | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
expect(onChange).toHaveBeenLastCalledWith('on') | ||
}) | ||
}) | ||
|
||
describe('checkbox', () => { | ||
it('should render correct HTML', () => { | ||
const onChange = jest.fn() | ||
|
||
render( | ||
<Toggle | ||
valueOn="on" | ||
valueOff="off" | ||
variant="checkbox" | ||
value="on" | ||
onChange={onChange} | ||
/> | ||
) | ||
|
||
const element = document.querySelector('.dnb-checkbox__input') | ||
|
||
expect(element).toBeInTheDocument() | ||
expect(element).toBeChecked() | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).not.toBeChecked() | ||
expect(onChange).toHaveBeenCalledTimes(1) | ||
expect(onChange).toHaveBeenLastCalledWith('off') | ||
|
||
fireEvent.click(element) | ||
|
||
expect(element).toBeChecked() | ||
expect(onChange).toHaveBeenCalledTimes(2) | ||
expect(onChange).toHaveBeenLastCalledWith('on') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters