Skip to content

Commit

Permalink
feat: implement copy, cut, paste
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 23, 2024
1 parent 212dac1 commit 9506828
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/browser/context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export interface UserEvent {
* @see {@link https://testing-library.com/docs/user-event/utility#upload} testing-library API
*/
upload: (element: Element | Locator, files: File | File[] | string | string[]) => Promise<void>

// TODO: doc https://testing-library.com/docs/user-event/clipboard/
copy: () => Promise<void>
cut: () => Promise<void>
paste: () => Promise<void>

/**
* Fills an input element with text. This will remove any existing text in the input before typing the new text.
* Uses provider's API under the hood.
Expand Down
33 changes: 32 additions & 1 deletion packages/browser/src/client/tester/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ export function createUserEvent(__tl_user_event_base__?: TestingLibraryUserEvent
unreleased: [] as string[],
}

return {
// https://playwright.dev/docs/api/class-keyboard
// https://webdriver.io/docs/api/browser/keys/
const modifier
= provider === `playwright`
? 'ControlOrMeta'
: provider === 'webdriverio'
? 'Ctrl'
: 'Control'

const userEvent: UserEvent = {
setup(options?: any) {
return createUserEvent(__tl_user_event_base__, options)
},
Expand Down Expand Up @@ -118,7 +127,29 @@ export function createUserEvent(__tl_user_event_base__?: TestingLibraryUserEvent
)
keyboard.unreleased = unreleased
},
async copy() {
if (typeof __tl_user_event__ !== 'undefined') {
await __tl_user_event__.copy()
return
}
await userEvent.keyboard(`{${modifier}>}{c}{/${modifier}}`)
},
async cut() {
if (typeof __tl_user_event__ !== 'undefined') {
await __tl_user_event__.cut()
return
}
await userEvent.keyboard(`{${modifier}>}{x}{/${modifier}}`)
},
async paste() {
if (typeof __tl_user_event__ !== 'undefined') {
await __tl_user_event__.paste()
return
}
await userEvent.keyboard(`{${modifier}>}{v}{/${modifier}}`)
},
}
return userEvent
}

export function cdp() {
Expand Down
24 changes: 6 additions & 18 deletions test/browser/fixtures/user-event/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { page, userEvent, server } from '@vitest/browser/context';
import { page, userEvent } from '@vitest/browser/context';

test('clipboard', async () => {
// make it smaller since webdriverio fails when scaled
Expand All @@ -11,38 +11,26 @@ test('clipboard', async () => {
<input placeholder="third" />
`;

// https://webdriver.io/docs/api/browser/keys/
// https://playwright.dev/docs/api/class-keyboard
const modifier =
server.provider === 'webdriverio'
? 'Ctrl'
: server.provider === 'playwright'
? 'ControlOrMeta'
: 'Control';
const copy = `{${modifier}>}{c}{/${modifier}}`;
const cut = `{${modifier}>}{x}{/${modifier}}`;
const paste = `{${modifier}>}{v}{/${modifier}}`;

// write first "hello" and copy to clipboard
await userEvent.click(page.getByPlaceholder('first'));
await userEvent.keyboard('hello');
await userEvent.keyboard(`{selectall}`);
await userEvent.keyboard(copy);
await userEvent.copy();

// paste twice into second
await userEvent.click(page.getByPlaceholder('second'));
await userEvent.keyboard(paste);
await userEvent.keyboard(paste);
await userEvent.paste();
await userEvent.paste();

// append first "world" and cut
await userEvent.click(page.getByPlaceholder('first'));
await userEvent.keyboard('world');
await userEvent.keyboard(`{selectall}`);
await userEvent.keyboard(cut);
await userEvent.cut();

// paste it to third
await userEvent.click(page.getByPlaceholder('third'));
await userEvent.keyboard(paste);
await userEvent.paste();

expect([
(page.getByPlaceholder('first').element() as any).value,
Expand Down

0 comments on commit 9506828

Please sign in to comment.