Skip to content

Commit 7d1e770

Browse files
committed
test: 💍 fix test by stubbing writeText
The hds copy button uses the browser clipboard api. The writeText api can fail if the window is not active. This does not happen in practice because the window has to be active to be clicked by the user. But in tests with synthetic triggers this can fail. Stubbing the writeText method is also how hds tests their component: https://github.com/hashicorp/design-system/blob/990981b5741f9a6bf28d100c330b7865ba74e506/showcase/tests/integration/components/hds/copy/button/index-test.gts#L20
1 parent 3e29a3b commit 7d1e770

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

addons/rose/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"prettier": "^3.3.3",
8787
"qunit": "^2.22.0",
8888
"qunit-dom": "^3.2.1",
89+
"sinon": "^19.0.2",
8990
"stylelint": "^15.11.0",
9091
"stylelint-config-prettier-scss": "^0.0.1",
9192
"stylelint-config-standard-scss": "^11.0.0",

addons/rose/tests/integration/components/rose/code-editor/toolbar-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { module, test } from 'qunit';
77
import { setupRenderingTest } from 'ember-qunit';
88
import { click, render } from '@ember/test-helpers';
99
import { hbs } from 'ember-cli-htmlbars';
10+
import sinon from 'sinon';
1011

1112
module('Integration | Component | rose/code-editor/toolbar', function (hooks) {
1213
setupRenderingTest(hooks);
@@ -43,6 +44,9 @@ module('Integration | Component | rose/code-editor/toolbar', function (hooks) {
4344
});
4445

4546
test('it calls onCopy callback', async function (assert) {
47+
// `writeText` could fail if the document is not in focus which is common during test runs
48+
sinon.stub(window.navigator.clipboard, 'writeText').resolves();
49+
4650
const onCopy = () => {
4751
this.set('called', true);
4852
};
@@ -53,8 +57,9 @@ module('Integration | Component | rose/code-editor/toolbar', function (hooks) {
5357
assert.dom(toolbarSelector).isVisible();
5458
assert.dom(copyButtonSelector).isVisible();
5559
assert.dom(menuDividerSelector).doesNotExist();
56-
5760
await click(copyButtonSelector);
5861
assert.true(this.called);
62+
63+
sinon.restore();
5964
});
6065
});

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)