Pragmatically reorder columns (react-testing-library)

2 Answers 1044 Views
Grid
Mo
Top achievements
Rank 1
Iron
Mo asked on 03 Jul 2021, 01:29 AM
Hello, my ultimate goal here is to be able to reorder columns through my testing suite (Jest, react-testing-library). I am not sure that drag and drop is properly supported in react-testing-library (I could be wrong, pointer events are though). Dragging and dropping columns triggers the onColumnReorder method in the browser which is all I am really trying to do, get onColumnReorder to trigger in my testing suite. Can I do this pragmatically or is there a global event I can trigger to have onColumnReorder be called? Thank you!

2 Answers, 1 is accepted

Sort by
0
Accepted
Mo
Top achievements
Rank 1
Iron
answered on 29 Jul 2021, 05:52 AM

Thank you Nikolay. Here is how I ended up figuring it out. Hopefully this helps someone out.

const getTableRows = () => screen.getAllByRole('row'); const getColumnHeaders = () => getTableRows()[0].children; test('Dragging and dropping column should trigger reorder', () => { const count = 3; // Set up component props

const products = generateMockProducts({ count }); const props = generateGridProps({ products }); const authorTxt = 'Author'; const titleTxt = 'Title'; const publisherTxt = 'Publisher';

// TestGrid is a wrapper for the Kendo React Grid component

const { rerender } = render(<TestGrid {...props} />); // column elements

const publisherColumn = screen.getByText(publisherTxt); const authorColumn = screen.getByText(authorTxt); let counter = 0; let headerCols = getColumnHeaders(); // double check that original order is in place before we drag and drop expect(headerCols[1].textContent).toEqual(authorTxt); expect(headerCols[2].textContent).toEqual(titleTxt); expect(headerCols[5].textContent).toEqual(publisherTxt); // mock this function that Kendo React Grid uses to determine focused column

document.elementFromPoint = jest.fn(() => { const el = counter >= 1 ? authorColumn : publisherColumn; counter++; return el; }); act(() => { // Move publisher column before author column fireEvent.mouseEnter(publisherColumn); fireEvent.mouseOver(publisherColumn); fireEvent.mouseMove(publisherColumn); fireEvent.mouseDown(publisherColumn); fireEvent.mouseMove(authorColumn); fireEvent.mouseUp(authorColumn); }); rerender(<TestGrid {...props} />); headerCols = getColumnHeaders(); // verify that publisher was reordered and put before author column expect(headerCols[1].textContent).toEqual(publisherTxt); expect(headerCols[2].textContent).toEqual(authorTxt); expect(headerCols[3].textContent).toEqual(titleTxt); });


Krissy
Telerik team
commented on 29 Jul 2021, 08:53 AM

Hi Mo,

Thank you for sharing your solution! We also hope this will be helpful to others who may encounter the same case.

If there is anything else we can help with, do not hesitate to contact us again.

1
Nikolay
Telerik team
answered on 06 Jul 2021, 11:14 AM

Hello,

The onColumnReorder event has been triggered only when a column is dragged. I think It should be possible to drag columns using react-testing-library. Currently, we use Nightwatch for testing it in the Grid. The reordering looks like this:

const productIdXpathSelector = '//*[text()="ProductID"]';
const productNameXpathSelector = '//*[text()="ProductName"]';

// Reordering 0 to 1
browser
    .useXpath()
    .moveToElement(productIdXpathSelector, 20, 10)
    .mouseButtonDown(productIdXpathSelector)
    .moveToElement(productNameXpathSelector, 20, 10)
    .mouseButtonUp()
    .useCss()
    .elements('css selector', '.k-master-row > td', (elements) => {
        browser.elementIdText(elements.value[0].ELEMENT, (text) => {
            browser.assert.equal(text.value, 'Chai');
        });
        browser.elementIdText(elements.value[1].ELEMENT, (text) => {
            browser.assert.equal(text.value, '1');
        });
        browser.elementIdText(elements.value[2].ELEMENT, (text) => {
            browser.assert.equal(text.value, '18');
        });
    });

Regards,
Nikolay
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Mo
Top achievements
Rank 1
Iron
Answers by
Mo
Top achievements
Rank 1
Iron
Nikolay
Telerik team
Share this question
or