Skip to content

Commit

Permalink
Merl 1153 faustwp block editor block controls implement checkbox cont…
Browse files Browse the repository at this point in the history
…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
TeresaGobble authored Sep 14, 2023
1 parent ee8c08e commit ae69cfb
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/curly-toes-cross.md
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`.

21 changes: 21 additions & 0 deletions packages/block-editor-utils/src/controls/Checkbox.tsx
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;
2 changes: 2 additions & 0 deletions packages/block-editor-utils/src/controls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import registerControl from '../helpers/registerControl.js';
import Text from './Text.js';
import NumberField from './Number.js';
import Color from './Color.js';
import Checkbox from './Checkbox.js';
import Select from './Select.js';
import Radio from './Radio.js';
import Range from './Range.js';

registerControl('text', Text);
registerControl('number', NumberField);
registerControl('color', Color);
registerControl('checkbox', Checkbox);
registerControl('select', Select);
registerControl('radio', Radio);
registerControl('range', Range);
78 changes: 78 additions & 0 deletions packages/block-editor-utils/tests/controls/Checkbox.test.tsx
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();
});
});

1 comment on commit ae69cfb

@headless-platform-by-wp-engine

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:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.