Skip to content

Commit

Permalink
fix(tags): make the 'delete' event cancelable (#3778)
Browse files Browse the repository at this point in the history
* fix(tags): make the 'delete' event cancelable

* chore(tags): address review feedback

---------

Co-authored-by: Luca Trușcă <ltrusca@adobe.com>
  • Loading branch information
imlutr and Luca Trușcă authored Nov 6, 2023
1 parent 3616199 commit d9afd41
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/tags/src/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class Tag extends SizedMixin(SpectrumElement, {
const applyDefault = this.dispatchEvent(
new Event('delete', {
bubbles: true,
cancelable: true,
composed: true,
})
);
Expand Down
28 changes: 27 additions & 1 deletion packages/tags/test/tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ governing permissions and limitations under the License.

import { elementUpdated, expect, fixture } from '@open-wc/testing';
import { html } from 'lit/static-html.js';
import { spy } from 'sinon';
import { spy, stub } from 'sinon';

import '@spectrum-web-components/tags/sp-tag.js';
import '@spectrum-web-components/tags/sp-tags.js';
Expand Down Expand Up @@ -186,4 +186,30 @@ describe('Tag', () => {
'Does not respond after `focusout`'
).to.equal(expectedEventCount);
});
it('can have delete event prevented', async () => {
const deleteSpy = spy();
const handleDelete = (event: Event): void => {
event.preventDefault();
deleteSpy();
};
const el = await fixture<Tag>(
html`
<sp-tag deletable @delete=${handleDelete}>Tag</sp-tag>
`
);

const removeStub = stub(el, 'remove');

await elementUpdated(el);

expect(deleteSpy.called).to.be.false;

const deleteButton = el.shadowRoot.querySelector(
'sp-clear-button'
) as ClearButton;
deleteButton.click();

expect(deleteSpy.callCount).to.equal(1);
expect(removeStub.called).to.be.false;
});
});

0 comments on commit d9afd41

Please sign in to comment.