Skip to content

Commit

Permalink
tests: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ynotdraw committed Jul 6, 2023
1 parent ba4687e commit 3b708a0
Show file tree
Hide file tree
Showing 3 changed files with 680 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{this.styles}}
{{this.className}}
"
data-active={{if @isActive "true" "false"}}
id="{{@popoverId}}-{{@index}}"
role="option"
{{on "click" this.onClick}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ToucanFormComboboxControlComponentSignature {
/**
* The function called when a user types into the combobox textbox, typically used to write custom filtering logic.
*/
onFilter?: (input: string) => Promise<unknown[]>;
onFilter?: (input: string) => unknown[];

/**
* `@options` forms the content of this component.
Expand Down Expand Up @@ -135,6 +135,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
}),
];

/**
* This is required for accessibility so that we can announce to the screenreader the highlighted option as the user uses the arrow keys.
*/
get activeDescendant() {
// For the case where nothing is selected on render
if (this.activeIndex === null) {
Expand All @@ -144,6 +147,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
return `${this.popoverId}-${this.activeIndex}`;
}

/**
* This state is used to determine if we should add event handlers to the input element or not.
*/
get isDisabledOrReadOnlyOrWithoutOptions() {
return (
this.args.isDisabled ||
Expand All @@ -152,6 +158,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
);
}

/**
* We apply different styles to the input tag based on our current state.
*/
get styles() {
if (this.args.isDisabled) {
return 'shadow-focusable-outline bg-overlay-1 text-disabled pointer-events-none placeholder:text-disabled';
Expand All @@ -168,10 +177,16 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
return 'shadow-focusable-outline focus:shadow-focus-outline bg-overlay-1 text-titles-and-attributes';
}

/**
* The options to render inside of the popover list.
*/
get options() {
return this.filteredOptions || this.args?.options;
}

/**
* The internal currently selected item.
*/
get selected(): string | undefined {
let { optionKey, selected } = this.args;

Expand All @@ -184,6 +199,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
) as string;
}

/**
* Attempts to scroll the active or newly highlighted item into view for the user.
*/
#scrollActiveOptionIntoView(alignToTop?: boolean) {
assert('`this.activeIndex` cannot be `null`', this.activeIndex !== null);

Expand All @@ -207,9 +225,11 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
@action
noop() {
// eslint-disable @typescript-eslint/no-empty-function
// PR: This rather than mess around with Glint and `ember-composable-functions`. Is there a better way?
}

/**
* Action called when a new item is selected. Ultimately calls the provided `@onChange` with the newly selected item.
*/
@action
onChange() {
assert('`this.activeIndex` cannot be `null`', this.activeIndex !== null);
Expand Down Expand Up @@ -259,6 +279,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
return emberIsEqual(one, two);
}

/**
* Handle keyboard events to operate like a combobox as defined at https://www.w3.org/WAI/ARIA/apg/patterns/combobox/.
*/
@action
onKeydown(event: KeyboardEvent) {
if (event.key === 'Tab') {
Expand Down Expand Up @@ -371,6 +394,9 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
}
}

/**
* Handles filtering when a user types into the input element.
*/
@action
async onInput(event: Event | InputEvent) {
assert(
Expand Down Expand Up @@ -432,8 +458,7 @@ export default class ToucanFormComboboxControlComponent extends Component<Toucan
}

/**
* Action that resets the input on blur to the selected option, if one was
* chosen.
* Action that resets the input on blur to the selected option, if one was chosen.
*/
@action
resetValue(event: Event) {
Expand Down
Loading

0 comments on commit 3b708a0

Please sign in to comment.