Skip to content

Commit

Permalink
write unit test for select
Browse files Browse the repository at this point in the history
  • Loading branch information
nielslyngsoe committed Nov 18, 2024
1 parent 974daf3 commit 9634f72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/uui-base/lib/mixins/SelectableMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const SelectableMixin = <T extends Constructor<LitElement>>(
constructor(...args: any[]) {
super(...args);
this.addEventListener('click', this._handleClick);
this.addEventListener('keydown', this.handleSelectKeydown);
this.addEventListener('keypress', this.handleSelectKeydown);
}

private handleSelectKeydown = (e: KeyboardEvent) => {
Expand Down
19 changes: 13 additions & 6 deletions packages/uui-card-content-node/lib/uui-card-content-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,32 @@ describe('UUICardContentNodeElement', () => {
it('emits a selected event when selectable', async () => {
element.selectable = true;
await elementUpdated(element);
const listener = oneEvent(element, UUISelectableEvent.SELECTED, false);
const listener = oneEvent(element, UUISelectableEvent.SELECTED);
element.click();
const event = await listener;
expect(event).to.exist;
expect(event.type).to.equal(UUISelectableEvent.SELECTED);
expect(element.selected).to.be.true;
});

it('can be selected with keyboard', async () => {
element.selectable = true;
await elementUpdated(element);
const listener = oneEvent(element, UUISelectableEvent.SELECTED);
element.dispatchEvent(new KeyboardEvent('keypress', { key: 'Enter' }));
const event = await listener;
expect(event).to.exist;
expect(event.type).to.equal(UUISelectableEvent.SELECTED);
expect(element.selected).to.be.true;
});
});

describe('deselect', () => {
it('emits a deselected event when preselected', async () => {
element.selectable = true;
element.selected = true;
await elementUpdated(element);
const listener = oneEvent(
element,
UUISelectableEvent.DESELECTED,
false,
);
const listener = oneEvent(element, UUISelectableEvent.DESELECTED);
element.click();
const event = await listener;
expect(event).to.exist;
Expand Down

0 comments on commit 9634f72

Please sign in to comment.