Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ToggleSwitch): disable when value gets undefined #193

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/entries/ToggleSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function ToggleSwitch(props) {
onBlur={ onBlur }
name={ id }
onInput={ handleInput }
checked={ localValue } />
checked={ !!localValue } />
<span class="bio-properties-panel-toggle-switch__slider" />
</label>
<p class="bio-properties-panel-toggle-switch__label">{ switcherLabel }</p>
Expand Down
67 changes: 54 additions & 13 deletions test/spec/components/ToggleSwitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,65 @@ describe('<ToggleSwitch>', function() {
});


it('should set checked according to value', function() {
describe('set checked according to value', function() {

// [value, checked]
const possibleValues = [
[ true, true ],
[ false, false ],
[ undefined, false ],
[ {}, true ]
];

// given
const getValueFunctions = [
() => true,
() => false
possibleValues.forEach(([ value, checked ]) => {

it(`should set checked to ${checked} for ${value}`, function() {

// when
const result = createToggleSwitch({ container, getValue: () => value });

// then
const toggle = domQuery(`#bio-properties-panel-${TEST_TOGGLE_ID}`, result.container);

expect(toggle.checked).to.equal(checked);

});

});

});


describe('update on external change', function() {

// [initialValue, updatedValue, checked]
const possibleValues = [
[ true, false, false ],
[ false, true, true ],
[ undefined, {}, true ],
[ {}, undefined, false ]
];

getValueFunctions.forEach(fn => {
possibleValues.forEach(([ initial, updated, checked ]) => {

// when
const result = createToggleSwitch({ container, getValue: fn });
it(`should update ${initial} -> ${updated}`, function() {

// then
const toggle = domQuery(`#bio-properties-panel-${TEST_TOGGLE_ID}`, result.container);
// given
const givenFn = () => initial;
const updatedFn = () => updated;
const result = createToggleSwitch({ container, getValue: givenFn });

// when
createToggleSwitch({ container, getValue: updatedFn }, result.rerender);

// then
const toggle = domQuery(`#bio-properties-panel-${TEST_TOGGLE_ID}`, result.container);
expect(toggle.checked).to.equal(checked);

});

expect(toggle.checked).to.equal(fn());
});

});


Expand Down Expand Up @@ -277,7 +318,7 @@ describe('<ToggleSwitch>', function() {

// helpers ////////////////////

function createToggleSwitch(options = {}) {
function createToggleSwitch(options = {}, renderFn = render) {
const {
element,
id = TEST_TOGGLE_ID,
Expand All @@ -296,7 +337,7 @@ function createToggleSwitch(options = {}) {
getDescriptionForId
};

return render(
return renderFn(
<DescriptionContext.Provider value={ context }>
<ToggleSwitch
element={ element }
Expand Down