Added new switch component for toggle UI

no issue

- Mimics behavior and UI of toggle button in Ghost Admin
This commit is contained in:
Rish 2020-05-01 20:12:13 +05:30
parent ee1dfbc0af
commit 9cc5badd2f
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,20 @@
import React from 'react';
function Switch({id, label, onToggle, checked = false}) {
return (
<div className="for-switch">
<label className="switch" htmlFor={id}>
<input
type="checkbox"
checked={checked}
id={id}
onChange={e => onToggle(e)}
aria-label={label}
/>
<span className="input-toggle-component"></span>
</label>
</div>
);
}
export default Switch;

View File

@ -0,0 +1,36 @@
import React from 'react';
import {render, fireEvent} from '@testing-library/react';
import Switch from './Switch';
const setup = (overrides = {}) => {
const mockOnToggle = jest.fn();
const props = {
onToggle: mockOnToggle,
label: 'Test Switch',
id: 'test-switch'
};
const utils = render(
<Switch {...props} />
);
const checkboxEl = utils.getByLabelText(props.label);
return {
checkboxEl,
mockOnToggle,
...utils
};
};
describe('Switch', () => {
test('renders', () => {
const {checkboxEl} = setup();
expect(checkboxEl).toBeInTheDocument();
});
test('calls onToggle on click', () => {
const {checkboxEl, mockOnToggle} = setup();
fireEvent.click(checkboxEl);
expect(mockOnToggle).toHaveBeenCalled();
});
});