Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Use Combobox automation hooks with clear
Browse files Browse the repository at this point in the history
  • Loading branch information
xeger committed Feb 24, 2023
1 parent 24cf2e0 commit 04502aa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
54 changes: 52 additions & 2 deletions cypress/component/commands/clear.cy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('cy.clear', () => {
cy.get('[data-testid=combobox-menu]').should('not.be.visible');
});

it.only('cooperates with fill', () => {
it('cooperates with fill', () => {
let selected = 'alpha';

cy.mount(
Expand All @@ -111,10 +111,60 @@ describe('cy.clear', () => {
cy.component(comp.Combobox, 'some label').clear();
eventually(() => selected === undefined);
});

context('with test-automation hooks', () => {
function FutureTestbed({ clearable = true, onClear, onToggle }) {
return (
<FormLabelGroup label="some label">
<div data-testid="combobox-dropdown" class="combobox dropdown">
<div aria-haspopup="true" aria-expanded="false">
<div class="input-group">
<input data-testid="combobox-input" class="form-control" placeholder="Select..." />
<button disabled={!clearable} type="button" onClick={onClear} data-testid="combobox-clear" title="Clear value" class="bg-transparent border-0 font-weight-bold px-0 btn btn-secondary disabled" style={{ opacity: 0 }}>×</button>
<button type="button" onClick={onToggle} data-testid="combobox-caret" class="bg-transparent border-0 pl-0 pr-2 btn btn-secondary" aria-label="Toggle options menu"><i aria-hidden="true" class="fa fa-caret-down fa-fw"></i></button>
</div>
</div>
</div>
</FormLabelGroup >
);
}

it('uses the clear button', () => {
let cleared = false;

cy.mount(
<FutureTestbed
onClear={() => {
cleared = true;
}}
/>
);

cy.component(comp.Combobox, 'some label').clear();
eventually(() => cleared === true);
})

it('tolerates disabled clear button', () => {
let cleared = false;

cy.mount(
<FutureTestbed
clearable={false}
onClear={() => {
cleared = true;
}}
/>
);

cy.component(comp.Combobox, 'some label').clear();
cy.wait(1000);
expect(cleared).to.equal(false);
})
})
});

context('Select component', () => {
it.only('clears values', () => {
it('clears values', () => {
const options = ['alpha', 'bravo', 'charlie'].map((o) => ({
label: o,
value: o,
Expand Down
20 changes: 14 additions & 6 deletions src/commands/actions/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export function clear(
});

if (isGearsCombobox) {
return cy
.wrap(prevSubject, QUIET)
.find('[data-testid=combobox-input]', QUIET)
.focus(QUIET)
.type('{backspace}{backspace}', QUIET)
.then(blurIfNecessary);
const btn = prevSubject.find('[data-testid=combobox-clear]');
if (btn.length === 1 && !btn.prop('disabled')) {
// testing hook is present; use it for maximum stability
return cy
.wrap(btn, QUIET).click().wrap(prevSubject, QUIET).then(blurIfNecessary);
} else {
// fall back to UI gesture that should work in most cases
return cy
.wrap(prevSubject, QUIET)
.find('[data-testid=combobox-input]', QUIET)
.focus(QUIET)
.type('{backspace}{backspace}', QUIET)
.then(blurIfNecessary);
}
}

if (isGearsSelect) {
Expand Down

0 comments on commit 04502aa

Please sign in to comment.