Skip to content

Commit 733d6f3

Browse files
committed
Enhance Tests: button-copy
1 parent 0ac5e8e commit 733d6f3

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

components/button/test/button-copy.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ describe('d2l-button-copy', () => {
2828
expect(dispatched).to.be.false;
2929
});
3030

31+
it('stops propagation when clicked', async() => {
32+
const el = await fixture(html`<d2l-button-copy></d2l-button-copy>`);
33+
let propagated = false;
34+
el.parentElement.addEventListener('click', () => propagated = true);
35+
const buttonIcon = el.shadowRoot.querySelector('d2l-button-icon');
36+
clickElem(buttonIcon);
37+
await oneEvent(el, 'click');
38+
expect(propagated).to.be.false;
39+
});
40+
3141
});
3242

3343
describe('writeTextToClipboard', () => {
@@ -62,4 +72,32 @@ describe('d2l-button-copy', () => {
6272

6373
});
6474

75+
describe('icon state', () => {
76+
77+
it('clears previous timeout on multiple clicks', async() => {
78+
const writeTextStub = stub(navigator.clipboard, 'writeText').resolves();
79+
const el = await fixture(html`<d2l-button-copy></d2l-button-copy>`);
80+
81+
// First click
82+
clickElem(el);
83+
let event = await oneEvent(el, 'click');
84+
await event.detail.writeTextToClipboard('first');
85+
await el.updateComplete;
86+
87+
const firstTimeoutId = el._iconCheckTimeoutId;
88+
89+
// Second click before timeout expires
90+
clickElem(el);
91+
event = await oneEvent(el, 'click');
92+
await event.detail.writeTextToClipboard('second');
93+
await el.updateComplete;
94+
95+
const secondTimeoutId = el._iconCheckTimeoutId;
96+
97+
expect(firstTimeoutId).to.not.equal(secondTimeoutId);
98+
writeTextStub.restore();
99+
});
100+
101+
});
102+
65103
});

components/button/test/button-copy.vdiff.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '../button-copy.js';
2-
import { clickElem, expect, fixture, html, oneEvent } from '@brightspace-ui/testing';
2+
import { clickElem, expect, fixture, html, oneEvent, waitUntil } from '@brightspace-ui/testing';
33
import { stub } from 'sinon';
44

55
const clickAction = async(elem) => {
@@ -35,4 +35,17 @@ describe('button-copy', () => {
3535
await expect(document).to.be.golden();
3636
});
3737

38+
it('icon reverts to copy after timeout', async() => {
39+
writeTextStub = stub(navigator.clipboard, 'writeText').resolves();
40+
const elem = await fixture(template, { viewport: { width: 700, height: 200 } });
41+
await clickAction(elem);
42+
43+
await waitUntil(() => {
44+
const buttonIcon = elem.shadowRoot.querySelector('d2l-button-icon');
45+
return buttonIcon.icon === 'tier1:copy';
46+
}, 'icon did not revert to copy', { timeout: 3000 });
47+
48+
await expect(elem).to.be.golden();
49+
});
50+
3851
});

0 commit comments

Comments
 (0)