From f786df1f1575247a85cc3ce302575707da93e737 Mon Sep 17 00:00:00 2001 From: Alessandra Davila Date: Thu, 23 Feb 2023 11:02:23 -0600 Subject: [PATCH] test(TableExpandRow): update tests to RTL (#13197) * test(TableExpandRow): update tests to RTL * Update TableExpandRow-test.js * Update TableExpandRow-test.js --------- Co-authored-by: Andrea N. Cardona --- .../__tests__/TableExpandRow-test.js | 143 +++++++++++------- .../__snapshots__/TableExpandRow-test.js.snap | 136 ++++++----------- 2 files changed, 136 insertions(+), 143 deletions(-) diff --git a/packages/react/src/components/DataTable/__tests__/TableExpandRow-test.js b/packages/react/src/components/DataTable/__tests__/TableExpandRow-test.js index 550cecbb1018..94da9bfe4bba 100644 --- a/packages/react/src/components/DataTable/__tests__/TableExpandRow-test.js +++ b/packages/react/src/components/DataTable/__tests__/TableExpandRow-test.js @@ -6,8 +6,11 @@ */ import React from 'react'; -import { mount } from 'enzyme'; import { Table, TableBody, TableExpandRow, TableExpandedRow } from '../'; +import userEvent from '@testing-library/user-event'; +import { render, screen } from '@testing-library/react'; + +const prefix = 'cds'; describe('DataTable.TableExpandRow', () => { let mockProps; @@ -21,59 +24,95 @@ describe('DataTable.TableExpandRow', () => { }; }); - it('should render', () => { - const wrapper = mount( - - - - - -
- ); - expect(wrapper).toMatchSnapshot(); - }); + describe('renders as expected - Component API', () => { + it('should render', () => { + const { container } = render( + + + + + +
+ ); + + expect(container).toMatchSnapshot(); + }); + + it('should respect ariaLabel prop', () => { + render( + + + + + +
+ ); + + expect(screen.getByRole('button')).toHaveAttribute( + 'aria-label', + mockProps.ariaLabel + ); + }); + + it('should support a custom `className` prop on the outermost element', () => { + render( + + + + + +
+ ); - it('should initially not define `data-previous-value`', () => { - const wrapper = mount( - - - - - -
- ); - expect( - Object.keys(wrapper.find('TableCell').first().props()).indexOf( - 'data-previous-value' - ) !== -1 - ).toBe(true); - expect( - wrapper.find('TableCell').first().prop('data-previous-value') - ).not.toBeDefined(); + expect(screen.getAllByRole('row')[0]).toHaveClass('custom-class'); + }); + + it('should respect expandHeader prop', () => { + render( + + + + + +
+ ); + + expect(screen.getAllByRole('cell')[0]).toHaveAttribute( + 'headers', + 'expand' + ); + }); + + it('should respect isExpanded prop', () => { + render( + + + + + +
+ ); + + expect(screen.getAllByRole('row')[0]).not.toHaveClass( + `${prefix}--expandable-row` + ); + }); }); - it('should expand when the expand button is clicked', () => { - const initialWrapper = mount( - - - - - -
- ); - initialWrapper.find('button').simulate('click'); - expect(mockProps.onExpand).toHaveBeenCalledTimes(1); - - const expandedWrapper = mount( - - - - - -
- ); - expect( - expandedWrapper.find('TableCell').first().prop('data-previous-value') - ).toBe('collapsed'); + describe('behaves as expected', () => { + it('should expand when button is clicked and onExpand to be called', () => { + render( + + + + + +
+ ); + + expect(mockProps.onExpand).toHaveBeenCalledTimes(0); + userEvent.click(screen.getByRole('button')); + expect(mockProps.onExpand).toHaveBeenCalledTimes(1); + }); }); }); diff --git a/packages/react/src/components/DataTable/__tests__/__snapshots__/TableExpandRow-test.js.snap b/packages/react/src/components/DataTable/__tests__/__snapshots__/TableExpandRow-test.js.snap index cc24e49dc456..2646e8e091eb 100644 --- a/packages/react/src/components/DataTable/__tests__/__snapshots__/TableExpandRow-test.js.snap +++ b/packages/react/src/components/DataTable/__tests__/__snapshots__/TableExpandRow-test.js.snap @@ -1,107 +1,61 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`DataTable.TableExpandRow should render 1`] = ` - +exports[`DataTable.TableExpandRow renders as expected - Component API should render 1`] = ` +
- - - - - - - - - + + + + + + - - - - - - - +
+ + +
- -
-
-
-
- + `;