Skip to content

Commit

Permalink
lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
the-r3aper7 committed Dec 8, 2023
1 parent a14df3c commit fadf150
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
74 changes: 49 additions & 25 deletions client/src/controllers/ClipboardController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CloneController } from './CloneController';

describe('ClipboardController', () => {
let application;

beforeAll(() => {
Object.defineProperty(window.navigator, 'clipboard', {
writable: true,
Expand Down Expand Up @@ -38,43 +38,64 @@ describe('ClipboardController', () => {
});

afterEach(() => {
application.stop()
})

describe("#copy", () => {
application.stop();
});

describe('#copy', () => {
it('initializes the clipboard copies successfully', async () => {
const clipboardButton = document.querySelector('[data-action="w-clipboard#copy"]');
const tooltipContainer = document.querySelector('[data-w-clone-target="container"]');
const clipboardButton = document.querySelector(
'[data-action="w-clipboard#copy"]',
);
const tooltipContainer = document.querySelector(
'[data-w-clone-target="container"]',
);
const textarea = document.getElementById('result-url');
textarea.value = 'Wagtail URL generator copy button';

clipboardButton.click();
await new Promise(resolve => { setTimeout(resolve, 1000) });

expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith('Wagtail URL generator copy button');
expect(tooltipContainer.firstChild.textContent).toEqual('Copied to clipboard');
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});

expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith(
'Wagtail URL generator copy button',
);
expect(tooltipContainer.firstChild.textContent).toEqual(
'Copied to clipboard',
);
expect(tooltipContainer.childElementCount).toBe(1);
});


it('copy button is clicked many times', async () => {
const clipboardButton = document.querySelector('[data-action="w-clipboard#copy"]');
const tooltipContainer = document.querySelector('[data-w-clone-target="container"]');
const clipboardButton = document.querySelector(
'[data-action="w-clipboard#copy"]',
);
const tooltipContainer = document.querySelector(
'[data-w-clone-target="container"]',
);
const textarea = document.getElementById('result-url');
textarea.value = 'Wagtail URL generator copy button';

clipboardButton.click()
await new Promise((resolve) => {setTimeout(resolve, 1000)})
expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith("Wagtail URL generator copy button");
expect(tooltipContainer.firstChild.textContent).toEqual('Copied to clipboard');
clipboardButton.click();
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith(
'Wagtail URL generator copy button',
);
expect(tooltipContainer.firstChild.textContent).toEqual(
'Copied to clipboard',
);
expect(tooltipContainer.childElementCount).toBe(1);
});


it('clipboard is not defined', async () => {
const clipboardButton = document.querySelector('[data-action="w-clipboard#copy"]');
const tooltipContainer = document.querySelector('[data-w-clone-target="container"]');
const clipboardButton = document.querySelector(
'[data-action="w-clipboard#copy"]',
);
const tooltipContainer = document.querySelector(
'[data-w-clone-target="container"]',
);
const textarea = document.getElementById('result-url');
textarea.value = 'Wagtail URL generator copy button';

Expand All @@ -86,12 +107,15 @@ describe('ClipboardController', () => {
});

clipboardButton.click();
await new Promise(resolve => {setTimeout(resolve, 1000)});
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});

expect(window.navigator.clipboard.writeText).toBe(undefined);
expect(tooltipContainer.firstChild.textContent).toEqual('Copying to clipboard failed');
expect(tooltipContainer.firstChild.textContent).toEqual(
'Copying to clipboard failed',
);
expect(tooltipContainer.childElementCount).toBe(1);
});

})
});
});
1 change: 0 additions & 1 deletion client/src/controllers/ClipboardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class ClipboardController extends Controller<HTMLElement> {
declare tooltipTarget: HTMLDivElement;

copy() {

const value = this.valueTarget.value;
if (!value) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

{% trans "URL" as heading %}
{% panel id="url" heading=heading %}
<div class="w-relative" data-controller="w-clipboard w-clone" data-w-clone-clear-delay-value="1_250" data-action="w-clipboard:copy->w-clone#clear w-clipboard:copied->w-clone#add w-clipboard:error->w-clone#add">
<div class="w-relative" data-controller="w-clipboard w-clone" data-w-clone-clear-delay-value="12_250" data-action="w-clipboard:copy->w-clone#clear w-clipboard:copied->w-clone#add w-clipboard:error->w-clone#add">
<label class="w-sr-only" for="result-url">{% trans "Image URL" %}</label>
<textarea class="w-image-url-generator__url" id="result-url" rows="1" name="result-url" data-controller="w-action" data-action="focus->w-action#select" data-w-clipboard-target="value">
</textarea>
Expand Down

0 comments on commit fadf150

Please sign in to comment.