-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merl 1153 faustwp block editor block controls implement checkbox cont…
…rol (#1573) * Added Checkbox component * Added Checkbox to registerControl index * Updated unit testing * Removed help label from control * Create curly-toes-cross.md * Test run of unit testing for GitHub actions
- Loading branch information
1 parent
ee8c08e
commit ae69cfb
Showing
4 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@faustwp/block-editor-utils": patch | ||
--- | ||
|
||
Feat: Add `CheckboxControl` field in `block-editor-utils`. | ||
|
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,21 @@ | ||
import * as React from 'react'; | ||
import { CheckboxControl } from '@wordpress/components'; | ||
import { ControlProps } from '../types/index.js'; | ||
|
||
function Checkbox<T extends Record<string, any>>({ | ||
config, | ||
props, | ||
}: ControlProps<T>) { | ||
const onChange = (newContent: boolean) => { | ||
props.setAttributes({ [config.name]: newContent }); | ||
}; | ||
return ( | ||
<CheckboxControl | ||
label={config.label} | ||
checked={props.attributes[config.name]} | ||
onChange={onChange} | ||
/> | ||
); | ||
} | ||
|
||
export default Checkbox; |
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
78 changes: 78 additions & 0 deletions
78
packages/block-editor-utils/tests/controls/Checkbox.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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent } from '@testing-library/react'; | ||
import Checkbox from '../../src/controls/Checkbox'; | ||
import { Field } from '../../src/types'; | ||
import { useState } from 'react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // Deprecated | ||
removeListener: jest.fn(), // Deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}); | ||
|
||
const config: Field = { | ||
location: 'inspector', | ||
control: 'checkbox', | ||
label: 'checkboxField', | ||
default: 'false', | ||
name: 'checkboxField', | ||
type: 'boolean', | ||
}; | ||
|
||
// This should set the checkbox value (aka check the box) | ||
const props = { | ||
setAttributes: jest.fn(), | ||
attributes: { | ||
checkboxField: '1', | ||
}, | ||
}; | ||
|
||
const setup = () => { | ||
const utils = render(<Checkbox config={config} props={props} />); | ||
const input = screen.findAllByDisplayValue(props.attributes.checkboxField!); | ||
return { | ||
input, | ||
...utils, | ||
}; | ||
}; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Checkbox', () => { | ||
it('should mount', () => { | ||
const { input } = setup(); | ||
expect(input).toBeTruthy(); | ||
}); | ||
|
||
it('checks that checkbox gets checked', () => { | ||
render( | ||
<input | ||
type="checkbox" | ||
aria-label="checkbox" | ||
/>, | ||
); | ||
|
||
const checkbox = screen.getByRole('checkbox', { name: 'checkbox' }); | ||
expect(checkbox).not.toBeChecked(); | ||
fireEvent.click(checkbox); | ||
expect(checkbox).toBeChecked(); | ||
}); | ||
|
||
it('should have correct display value from props', () => { | ||
setup(); | ||
expect( | ||
screen.getByDisplayValue(props.attributes.checkboxField), | ||
).toBeTruthy(); | ||
}); | ||
}); |
ae69cfb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the recent updates to your Atlas environment:
Learn more about building on Atlas in our documentation.