Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up copy/pasting of tabular content in EuiDataGrid and EuiBasic/InMemoryTable #8019

Merged
merged 14 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/eui/cypress/support/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'cypress-real-events';

import './a11y/checkAxe';
import './keyboard/repeatRealPress';
import './copy/select_and_copy';
import './setup/mount';
import './setup/realMount';

Expand Down
49 changes: 49 additions & 0 deletions packages/eui/cypress/support/copy/select_and_copy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

const selectAndCopy = (selectorToCopy: string) => {
// Force Chrome devtools to allow reading from the clipboard
cy.wrap(
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
Copy link
Contributor

Choose a reason for hiding this comment

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

🤯

Copy link
Member Author

Choose a reason for hiding this comment

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

I went down so many google/stackoverflow rabbit holes to figure out how to get this working haha 🫠 honestly until the very end I wasn't really sure it was going to be, but very glad to have a regression test for this!

origin: window.location.origin,
},
})
);

// For some annoying reason, mocking selection+copying throws a bunch
// of document errors from the code coverage reporter - just ignore them
Cypress.on('uncaught:exception', (err) => {
console.log(err.message);
if (
err.message.includes(
"Cannot read properties of null (reading 'document')"
)
) {
return false;
}
});

cy.get(selectorToCopy).then(($el) => {
const el = $el[0];
const document = el.ownerDocument;
const range = document.createRange();
range.selectNodeContents(el);
document.getSelection()!.removeAllRanges();
document.getSelection()!.addRange(range);
});

return cy.window().then((window) => {
document.execCommand('copy');
return window.navigator.clipboard.readText();
});
};

Cypress.Commands.add('selectAndCopy', selectAndCopy);
7 changes: 7 additions & 0 deletions packages/eui/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ declare global {
count?: number,
options?: RealPressOptions
): void;

/**
* Select an element's content and copy it to the browser clipboard
* @param selectorToCopy e.g. '.euiDataGrid__content'
* @returns a chainable .then((string) => { doSomethingWith(string); })
*/
selectAndCopy(selectorToCopy: string): Chainable<string>;
}
}
}
49 changes: 49 additions & 0 deletions packages/eui/src/components/basic_table/basic_table.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

/// <reference types="cypress" />
/// <reference types="cypress-real-events" />
/// <reference types="../../../cypress/support" />

import React from 'react';
import { EuiHealth } from '../../components';
import { EuiBasicTable, EuiBasicTableColumn } from './index';

describe('EuiBasicTable', () => {
const columns: Array<EuiBasicTableColumn<any>> = [
{ field: 'a', name: 'A', render: () => 'Hello' },
{ field: 'b', name: 'B', render: () => 'World' },
{
field: 'c',
name: 'Status',
render: () => <EuiHealth color="success">Online</EuiHealth>,
},
];

describe('copying tabular content', () => {
it('renders one newline per-row and renders horizontal tab characters between cells', () => {
cy.realMount(
<EuiBasicTable
tableCaption="Demo of EuiBasicTable"
items={[{}]}
rowHeader="firstName"
columns={columns}
responsiveBreakpoint={false}
/>
);

cy.selectAndCopy('.euiTable').then((copiedText) => {
expect(copiedText).to.eq(
`A\tB\t\Status
Hello\tWorld\tOnline
`
);
});
});
});
});
17 changes: 17 additions & 0 deletions packages/eui/src/components/datagrid/data_grid.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,23 @@ describe('EuiDataGrid', () => {
});
});
});

describe('copying tabular content', () => {
it('renders one newline per-row and renders horizontal tab characters between cells', () => {
cy.realMount(<EuiDataGrid {...baseProps} />);

cy.selectAndCopy('.euiDataGrid__content').then((copiedText) => {
expect(copiedText).to.eq(
`First\tSecond
a, 0\tb, 0
a, 1\tb, 1
a, 2\tb, 2
a, footer\tb, footer
`
);
});
});
});
});

function getGridData() {
Expand Down