-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add none checker checkbox * Update readme
- Loading branch information
1 parent
3b731c2
commit 0cb0659
Showing
32 changed files
with
1,008 additions
and
296 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import {cleanup, fireEvent, render} from '@testing-library/react'; | ||
import React from 'react'; | ||
import {AllCheckerCheckbox, Checkbox, CheckboxGroup} from "../src"; | ||
|
||
// automatically unmount and cleanup DOM after the test is finished. | ||
afterEach(cleanup); | ||
|
||
it('Unchecked allCheckerCheckbox will check on click', () => { | ||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"test-checkbox"} data-testid="test-checkbox" /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const checkbox = component.getByTestId('test-checkbox') as HTMLInputElement; | ||
|
||
expect(checkbox.checked).toEqual(false); | ||
fireEvent.click(checkbox); | ||
expect(checkbox.checked).toEqual(true); | ||
}); | ||
|
||
it('Checked allCheckerCheckbox will uncheck on click', () => { | ||
const component = render( | ||
<CheckboxGroup defaultChecked> | ||
<AllCheckerCheckbox id={"test-checkbox"} data-testid="test-checkbox" /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const checkbox = component.getByTestId('test-checkbox') as HTMLInputElement; | ||
|
||
expect(checkbox.checked).toEqual(true); | ||
fireEvent.click(checkbox); | ||
expect(checkbox.checked).toEqual(false); | ||
}); | ||
|
||
it('All checkboxes will check on allCheckboxesChecker click', () => { | ||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"all-checker-checkbox"} data-testid="all-checker-checkbox" /> | ||
<Checkbox id={"test-checkbox-1"} data-testid="test-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-2"} data-testid="test-checkbox-2" /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const allCheckerCheckbox = component.getByTestId('all-checker-checkbox') as HTMLInputElement; | ||
const checkbox1 = component.getByTestId('test-checkbox-1') as HTMLInputElement; | ||
const checkbox2 = component.getByTestId('test-checkbox-2') as HTMLInputElement; | ||
|
||
expect(checkbox1.checked).toEqual(false); | ||
expect(checkbox2.checked).toEqual(false); | ||
fireEvent.click(allCheckerCheckbox); | ||
expect(checkbox1.checked).toEqual(true); | ||
expect(checkbox2.checked).toEqual(true); | ||
}); | ||
|
||
it('All checkboxes will uncheck on allCheckboxesChecker click', () => { | ||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"all-checker-checkbox"} data-testid="all-checker-checkbox" /> | ||
<Checkbox id={"test-checkbox-1"} data-testid="test-checkbox-1" checked/> | ||
<Checkbox id={"test-checkbox-2"} data-testid="test-checkbox-2" checked/> | ||
</CheckboxGroup> | ||
); | ||
|
||
const allCheckerCheckbox = component.getByTestId('all-checker-checkbox') as HTMLInputElement; | ||
const checkbox1 = component.getByTestId('test-checkbox-1') as HTMLInputElement; | ||
const checkbox2 = component.getByTestId('test-checkbox-2') as HTMLInputElement; | ||
|
||
expect(checkbox1.checked).toEqual(true); | ||
expect(checkbox2.checked).toEqual(true); | ||
fireEvent.click(allCheckerCheckbox); | ||
expect(checkbox1.checked).toEqual(false); | ||
expect(checkbox2.checked).toEqual(false); | ||
}); | ||
|
||
|
||
it('All allCheckerCheckboxes will uncheck when not all checkboxes are checked', () => { | ||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-1"} data-testid="all-checker-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-1"} data-testid="test-checkbox" checked/> | ||
<Checkbox id={"test-checkbox-2"} checked/> | ||
<Checkbox id={"test-checkbox-3"} checked/> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-2"} data-testid="all-checker-checkbox-2" /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const allCheckerCheckbox1 = component.getByTestId('all-checker-checkbox-1') as HTMLInputElement; | ||
const allCheckerCheckbox2 = component.getByTestId('all-checker-checkbox-2') as HTMLInputElement; | ||
const checkbox = component.getByTestId('test-checkbox') as HTMLInputElement; | ||
|
||
expect(allCheckerCheckbox1.checked).toEqual(true); | ||
expect(allCheckerCheckbox2.checked).toEqual(true); | ||
fireEvent.click(checkbox); | ||
expect(allCheckerCheckbox1.checked).toEqual(false); | ||
expect(allCheckerCheckbox2.checked).toEqual(false); | ||
}); | ||
|
||
it('All allCheckerCheckboxes will check when all checkboxes are checked', () => { | ||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-1"} data-testid="all-checker-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-1"} data-testid="test-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-2"} data-testid="test-checkbox-2" /> | ||
<Checkbox id={"test-checkbox-3"} data-testid="test-checkbox-3" /> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-2"} data-testid="all-checker-checkbox-2" /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const allCheckerCheckbox1 = component.getByTestId('all-checker-checkbox-1') as HTMLInputElement; | ||
const allCheckerCheckbox2 = component.getByTestId('all-checker-checkbox-2') as HTMLInputElement; | ||
const checkbox1 = component.getByTestId('test-checkbox-1') as HTMLInputElement; | ||
const checkbox2 = component.getByTestId('test-checkbox-2') as HTMLInputElement; | ||
const checkbox3 = component.getByTestId('test-checkbox-3') as HTMLInputElement; | ||
|
||
expect(allCheckerCheckbox1.checked).toEqual(false); | ||
expect(allCheckerCheckbox2.checked).toEqual(false); | ||
fireEvent.click(checkbox1); | ||
fireEvent.click(checkbox2); | ||
fireEvent.click(checkbox3); | ||
expect(allCheckerCheckbox1.checked).toEqual(true); | ||
expect(allCheckerCheckbox2.checked).toEqual(true); | ||
}); | ||
|
||
it('Click on allCheckerCheckbox will trigger onChange on checkboxGroup', () => { | ||
const testOnChange = jest.fn(); | ||
|
||
const component = render( | ||
<CheckboxGroup onChange={testOnChange}> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-1"} data-testid="all-checker-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-1"} data-testid="test-checkbox-1" /> | ||
<Checkbox id={"test-checkbox-2"} data-testid="test-checkbox-2" /> | ||
<Checkbox id={"test-checkbox-3"} data-testid="test-checkbox-3" /> | ||
<AllCheckerCheckbox id={"all-checker-checkbox-2"} data-testid="all-checker-checkbox-2" /> | ||
</CheckboxGroup> | ||
); | ||
setTimeout(() => { | ||
const allCheckerCheckbox1 = component.getByTestId('all-checker-checkbox-1') as HTMLInputElement; | ||
fireEvent.click(allCheckerCheckbox1); | ||
expect(testOnChange.mock.calls.length).toBe(1); | ||
}, 251); | ||
}); | ||
|
||
it('Click will trigger onChange on allCheckerCheckbox', () => { | ||
const testOnChange = jest.fn(); | ||
|
||
const component = render( | ||
<CheckboxGroup> | ||
<AllCheckerCheckbox id={"test-checkbox"} data-testid="test-checkbox" onChange={testOnChange} /> | ||
</CheckboxGroup> | ||
); | ||
|
||
const checkbox1 = component.getByTestId('test-checkbox') as HTMLInputElement; | ||
|
||
fireEvent.click(checkbox1); | ||
expect(testOnChange.mock.calls.length).toBe(1); | ||
}); | ||
|
||
|
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
Oops, something went wrong.