From 84d1a954f58e60010924fefaef9bddd89831a0c9 Mon Sep 17 00:00:00 2001 From: Alessandra Davila Date: Thu, 9 Feb 2023 10:25:43 -0600 Subject: [PATCH] test(InnerClickListener): update to RTL (#13117) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../__tests__/InnerClickListener-test.js | 111 ++++++++++-------- .../InnerClickListener-test.js.snap | 35 +++--- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/packages/react/src/internal/__tests__/InnerClickListener-test.js b/packages/react/src/internal/__tests__/InnerClickListener-test.js index 28aa5692eb79..bb7119f3e875 100644 --- a/packages/react/src/internal/__tests__/InnerClickListener-test.js +++ b/packages/react/src/internal/__tests__/InnerClickListener-test.js @@ -7,7 +7,8 @@ import PropTypes from 'prop-types'; import React from 'react'; -import { mount } from 'enzyme'; +import userEvent from '@testing-library/user-event'; +import { render, screen } from '@testing-library/react'; import InnerClickListener from '../InnerClickListener'; describe('InnerClickListener', () => { @@ -42,68 +43,74 @@ describe('InnerClickListener', () => { handleRefSpy.mockRestore(); }); - it('should render', () => { - const wrapper = mount( - - - - ); - expect(wrapper).toMatchSnapshot(); - }); + describe('renders as expected - Component API', () => { + it('should render', () => { + const { container } = render( + + + + ); - it('should call `handleRef` when mounting', () => { - mount( - - - - ); - expect(handleRefSpy).toHaveBeenCalledTimes(1); + expect(container).toMatchSnapshot(); + }); }); - it('should call `onClickOutside` when clicked outside the node that has the ref', () => { - const rootNode = document.createElement('div'); - rootNode.setAttribute('id', 'root'); + describe('behaves as expected', () => { + it('should call `handleRef` when render', () => { + render( + + + + ); - document.body.appendChild(rootNode); - - mount( - - - , - { - attachTo: rootNode, - } - ); + expect(handleRefSpy).toHaveBeenCalledTimes(1); + }); - document.getElementById('2').click(); - expect(onClickOutside).not.toHaveBeenCalled(); + it('should call `onClickOutside` when clicked outside the node that has the ref', () => { + const rootNode = document.createElement('div'); + rootNode.setAttribute('id', 'root'); - document.getElementById('1').click(); - expect(onClickOutside).toHaveBeenCalledTimes(1); + document.body.appendChild(rootNode); - document.dispatchEvent(new MouseEvent('click')); - expect(onClickOutside).toHaveBeenCalledTimes(2); - }); + render( + + + , + { + attachTo: rootNode, + } + ); - it('should not call `onClickOutside` if click target disappears', () => { - const rootNode2 = document.createElement('div'); - rootNode2.setAttribute('id', 'root2'); + userEvent.click(screen.getByText('2')); + expect(onClickOutside).not.toHaveBeenCalled(); - document.body.appendChild(rootNode2); + userEvent.click(screen.getByText('1')); + expect(onClickOutside).toHaveBeenCalledTimes(1); - mount( - - - , - { - attachTo: rootNode2, - } - ); + userEvent.click(screen.getByText('1')); + expect(onClickOutside).toHaveBeenCalledTimes(2); + }); - document.getElementById('1').addEventListener('click', function () { - this.parentNode.removeChild(this); + it('should not call `onClickOutside` if click target disappears', () => { + const rootNode2 = document.createElement('div'); + rootNode2.setAttribute('id', 'root2'); + + document.body.appendChild(rootNode2); + + render( + + + , + { + attachTo: rootNode2, + } + ); + + screen.getByText('1').addEventListener('click', function () { + this.parentNode.removeChild(this); + }); + userEvent.click(screen.getByText('1')); + expect(onClickOutside).not.toHaveBeenCalled(); }); - document.getElementById('1').click(); - expect(onClickOutside).not.toHaveBeenCalled(); }); }); diff --git a/packages/react/src/internal/__tests__/__snapshots__/InnerClickListener-test.js.snap b/packages/react/src/internal/__tests__/__snapshots__/InnerClickListener-test.js.snap index d00bfb3eba24..fda60afec718 100644 --- a/packages/react/src/internal/__tests__/__snapshots__/InnerClickListener-test.js.snap +++ b/packages/react/src/internal/__tests__/__snapshots__/InnerClickListener-test.js.snap @@ -1,25 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`InnerClickListener should render 1`] = ` - - -
-
- 1 -
-
- 2 -
+exports[`InnerClickListener renders as expected - Component API should render 1`] = ` +
+
+
+ 1
- - +
+ 2 +
+
+
`;