Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions components/button/test/button-copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ describe('d2l-button-copy', () => {
expect(dispatched).to.be.false;
});

it('stops propagation when clicked', async() => {
const el = await fixture(html`<d2l-button-copy></d2l-button-copy>`);
let propagated = false;
el.parentElement.addEventListener('click', () => propagated = true);
const buttonIcon = el.shadowRoot.querySelector('d2l-button-icon');
clickElem(buttonIcon);
await oneEvent(el, 'click');
expect(propagated).to.be.false;
});

});

describe('writeTextToClipboard', () => {
Expand Down Expand Up @@ -62,4 +72,32 @@ describe('d2l-button-copy', () => {

});

describe('icon state', () => {

it('clears previous timeout on multiple clicks', async() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a somewhat fragile way to test that clicking the copy button multiple times extends the amount of time before clearing the copied icon. I wonder if we could do a vdiff with a fake timer (so we're not actually waiting 2s) that accomplishes the same thing?

const writeTextStub = stub(navigator.clipboard, 'writeText').resolves();
const el = await fixture(html`<d2l-button-copy></d2l-button-copy>`);

// First click
clickElem(el);
let event = await oneEvent(el, 'click');
await event.detail.writeTextToClipboard('first');
await el.updateComplete;

const firstTimeoutId = el._iconCheckTimeoutId;

// Second click before timeout expires
clickElem(el);
event = await oneEvent(el, 'click');
await event.detail.writeTextToClipboard('second');
await el.updateComplete;

const secondTimeoutId = el._iconCheckTimeoutId;

expect(firstTimeoutId).to.not.equal(secondTimeoutId);
writeTextStub.restore();
});

});

});
15 changes: 14 additions & 1 deletion components/button/test/button-copy.vdiff.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../button-copy.js';
import { clickElem, expect, fixture, html, oneEvent } from '@brightspace-ui/testing';
import { clickElem, expect, fixture, html, oneEvent, waitUntil } from '@brightspace-ui/testing';
import { stub } from 'sinon';

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

it('icon reverts to copy after timeout', async() => {
writeTextStub = stub(navigator.clipboard, 'writeText').resolves();
const elem = await fixture(template, { viewport: { width: 700, height: 200 } });
await clickAction(elem);

await waitUntil(() => {
const buttonIcon = elem.shadowRoot.querySelector('d2l-button-icon');
return buttonIcon.icon === 'tier1:copy';
}, 'icon did not revert to copy', { timeout: 3000 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of actually waiting 3s, could we use Sinon's fake time to manually advance the clock?


await expect(elem).to.be.golden();
});

});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading