Skip to content

Commit

Permalink
Toggle component should toggle on spacebar press
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed Dec 13, 2019
1 parent 6eb6d32 commit 050f60a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
9 changes: 9 additions & 0 deletions ui/app/components/toggle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Component from '@ember/component';

const SPACE = 32;

export default Component.extend({
tagName: 'label',
classNames: ['toggle'],
Expand All @@ -10,4 +12,11 @@ export default Component.extend({
isActive: false,
isDisabled: false,
onToggle() {},
onSpacebar() {},

onKeypress(e) {
if (e.keyCode === SPACE) {
this.onSpacebar(e);
}
},
});
3 changes: 2 additions & 1 deletion ui/app/templates/components/toggle.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
checked={{isActive}}
disabled={{isDisabled}}
class="input"
onchange={{action onToggle}} />
onchange={{action onToggle}}
onkeypress={{action onKeypress}}/>
<span data-test-toggler class="toggler" />
{{yield}}
16 changes: 15 additions & 1 deletion ui/tests/integration/toggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ module('Integration | Component | toggle', function(hooks) {
isDisabled: false,
label: 'Label',
onToggle: sinon.spy(),
onSpacebar: sinon.spy(),
});

const commonTemplate = hbs`
{{#toggle
isActive=isActive
isDisabled=isDisabled
onToggle=onToggle}}
onToggle=onToggle
onSpacebar=onSpacebar}}
{{label}}
{{/toggle}}
`;
Expand Down Expand Up @@ -86,4 +88,16 @@ module('Integration | Component | toggle', function(hooks) {
await Toggle.toggle();
assert.equal(props.onToggle.callCount, 1);
});

test('toggles can be toggled with the spacebar', async function(assert) {
const props = commonProperties();
this.setProperties(props);
await this.render(commonTemplate);

await Toggle.focus();
await Toggle.spaceBar();

assert.equal(props.onSpacebar.callCount, 1, 'This works');
assert.equal(props.onToggle.callCount, 1, 'This does not');
});
});
16 changes: 15 additions & 1 deletion ui/tests/pages/components/toggle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { attribute, property, clickable, hasClass, isPresent, text } from 'ember-cli-page-object';
import {
attribute,
property,
clickable,
focusable,
hasClass,
isPresent,
text,
triggerable,
} from 'ember-cli-page-object';

const SPACE = 32;

export default scope => ({
scope,
Expand All @@ -13,4 +24,7 @@ export default scope => ({
label: text('[data-test-label]'),

toggle: clickable('[data-test-input]'),

focus: focusable('[data-test-input]'),
spaceBar: triggerable('keypress', '[data-test-input]', { eventProperties: { keyCode: SPACE } }),
});

0 comments on commit 050f60a

Please sign in to comment.