diff --git a/UNRELEASED.md b/UNRELEASED.md index a07a253841d..1d46baf2d3c 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -73,6 +73,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f - Modernized tests for Pane, Section, PositionedOverlay, SingleThumb, RangeSlider, and ConnectedFilter components ([#4429](https://github.com/Shopify/polaris-react/pull/4429)) - Modernized tests for ContextualSaveBar and DataTable and its subcomponents ([#4397](https://github.com/Shopify/polaris-react/pull/4397)) - Modernized tests for IndexTable, Indicator, InlineError, KeyboardKey, and KeypressListener components([#4431](https://github.com/Shopify/polaris-react/pull/4431)) +- Modernized tests for Modal ([#4433](https://github.com/Shopify/polaris-react/pull/4433)) - Modernized tests for Navigation and Navigation.Section ([#4440](https://github.com/Shopify/polaris-react/pull/4440)) - Modernized tests for EmptyState component ([#4427](https://github.com/Shopify/polaris-react/pull/4427)) diff --git a/src/components/Modal/tests/Modal.test.tsx b/src/components/Modal/tests/Modal.test.tsx index 3906cf6af58..1d2c8789054 100644 --- a/src/components/Modal/tests/Modal.test.tsx +++ b/src/components/Modal/tests/Modal.test.tsx @@ -1,7 +1,5 @@ import React, {useRef} from 'react'; import {animationFrame} from '@shopify/jest-dom-mocks'; -// eslint-disable-next-line no-restricted-imports -import {mountWithAppProvider, trigger} from 'test-utilities/legacy'; import {mountWithApp} from 'test-utilities'; import {Badge, Button, Spinner, Portal, Scrollable} from 'components'; @@ -40,7 +38,7 @@ describe('', () => { return null; } - const component = mountWithAppProvider( + const component = mountWithApp( {(withinContentContext) => { @@ -52,22 +50,22 @@ describe('', () => { , ); - expect(component.find(TestComponent).prop('withinContentContainer')).toBe( - true, - ); + expect(component).toContainReactComponent(TestComponent, { + withinContentContainer: true, + }); }); it('focuses the dialog node on mount', () => { - const modal = mountWithAppProvider( - , - ); + const modal = mountWithApp(); - expect(document.activeElement).toBe(modal.find(Dialog).getDOMNode()); + expect(document.activeElement).toBe( + modal.find('div', {className: 'Dialog'})?.domNode, + ); }); describe('src', () => { it('renders an iframe if src is provided', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { , ); - const iframe = modal.find('iframe').first(); - expect(iframe.exists()).toBe(true); - expect(iframe.prop('name')).toStrictEqual('Name'); - expect(iframe.prop('src')).toStrictEqual('Source'); - - const scrollable = modal.find(Scrollable).first(); - expect(scrollable.exists()).toBe(false); + const iframe = modal.find('iframe'); + expect(iframe).toHaveReactProps({name: 'Name', src: 'Source'}); + expect(iframe).not.toContainReactComponent(Scrollable); }); it('renders Scrollable if src is not provided', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - const iframe = modal.find('iframe').first(); - expect(iframe.exists()).toBe(false); - - const scrollable = modal.find(Scrollable).first(); - expect(scrollable.exists()).toBe(true); - expect(scrollable.prop('shadow')).toBe(true); + expect(modal).not.toContainReactComponent('iframe'); + expect(modal).toContainReactComponent(Scrollable, {shadow: true}); }); }); describe('onTransitionEnd', () => { it('calls onTransitionEnd after it mounts', () => { const mockOnTransitionEnd = jest.fn(); - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { onTransitionEnd={mockOnTransitionEnd} />, ); - trigger(modal.find(Dialog), 'onEntered'); + modal.find(Dialog)?.trigger('onEntered'); expect(mockOnTransitionEnd).toHaveBeenCalledTimes(1); }); }); @@ -123,7 +113,7 @@ describe('', () => { describe('onIFrameLoad', () => { it('calls onIFrameLoad after it mounts', () => { const mockOnIframeLoad = jest.fn(); - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { src="path/to/place" />, ); - trigger(modal.find('iframe'), 'onLoad', {target: {contentWindow: {}}}); + modal.find('iframe')?.trigger('onLoad', {target: {}}); expect(mockOnIframeLoad).toHaveBeenCalledTimes(1); }); }); describe('instant', () => { it('passes instant to Dialog if true', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('instant')).toBe(true); + expect(modal).toContainReactComponent(Dialog, {instant: true}); }); it('does not pass instant to Dialog be default', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('instant')).toBeUndefined(); + expect(modal).toContainReactComponent(Dialog, {instant: undefined}); }); }); describe('large', () => { it('passes large to Dialog if true', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('large')).toBe(true); + expect(modal).toContainReactComponent(Dialog, {large: true}); }); it('does not pass large to Dialog be default', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('large')).toBeUndefined(); + expect(modal).toContainReactComponent(Dialog, {large: undefined}); }); }); describe('small', () => { it('passes small to Dialog if true', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('small')).toBe(true); + expect(modal).toContainReactComponent(Dialog, {small: true}); }); it('does not pass small to Dialog be default', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('small')).toBeUndefined(); + expect(modal).toContainReactComponent(Dialog, {small: undefined}); }); }); describe('limitHeight', () => { it('passes limitHeight to Dialog if true', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('limitHeight')).toBe(true); + expect(modal).toContainReactComponent(Dialog, {limitHeight: true}); }); it('does not pass limitHeight to Dialog be default', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('limitHeight')).toBeUndefined(); + expect(modal).toContainReactComponent(Dialog, {limitHeight: undefined}); }); }); describe('open', () => { it('renders ', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Portal)).toHaveLength(1); + expect(modal).toContainReactComponentTimes(Portal, 1); }); }); describe('closed', () => { it('does not render children', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Badge)).toHaveLength(0); + expect(modal).not.toContainReactComponent(Badge); }); }); describe('opening / closing', () => { it('renders modal content when open = true', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Badge).exists()).toBe(true); + expect(modal).toContainReactComponent(Badge); }); it('does not render modal content when open = false', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Badge).exists()).toBe(false); + expect(modal).not.toContainReactComponent(Badge); }); }); @@ -295,23 +285,23 @@ describe('', () => { describe('footer', () => { it('does not render footer by default', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Footer).exists()).toBeFalsy(); + expect(modal).not.toContainReactComponent(Footer); }); it('renders if footer are passed in', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Footer).exists()).toBeTruthy(); + expect(modal).toContainReactComponent(Footer); }); it('renders if primaryAction are passed in', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { />, ); - expect(modal.find(Footer).exists()).toBeTruthy(); + expect(modal).toContainReactComponent(Footer); }); it('renders if secondaryActions are passed in', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { />, ); - expect(modal.find(Footer).exists()).toBeTruthy(); + expect(modal).toContainReactComponent(Footer); }); }); describe('body', () => { it('limits dialog height from limitHeight prop', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Dialog).prop('limitHeight')).toBeTruthy(); + expect(modal).toContainReactComponent(Dialog, {limitHeight: true}); }); it('does not render a Scrollable with noScroll prop', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - const scrollable = modal.find(Scrollable).first(); - expect(scrollable.exists()).toBe(false); + expect(modal).not.toContainReactComponent(Scrollable); }); }); describe('loading', () => { it('renders a spinner', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Spinner).exists()).toBe(true); + expect(modal).toContainReactComponent(Spinner); }); it('does not render children', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( , ); - expect(modal.find(Badge).exists()).toBe(false); + expect(modal).not.toContainReactComponent(Badge); }); }); describe('lifecycle', () => { it('unmounts safely', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp(

Child

, @@ -409,7 +398,7 @@ describe('', () => { }); it('renders the element if an element is passed in', () => { - const modal = mountWithAppProvider( + const modal = mountWithApp( ', () => { />, ); - expect(modal.find(Button).exists()).toBe(true); + expect(modal).toContainReactComponent(Button); }); it('does not render the element if a ref object is passed in', () => { @@ -449,9 +438,7 @@ describe('', () => { }); it('does not throw an error when no activator is passed in', () => { - const modal = mountWithAppProvider( - , - ); + const modal = mountWithApp(); expect(() => { modal.setProps({open: false});