|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import React from 'react'; |
| 4 | +import { act } from '@testing-library/react'; |
| 5 | + |
| 6 | +import AppLayout from '../../../lib/components/app-layout'; |
| 7 | +import { forceMobileModeSymbol } from '../../../lib/components/internal/hooks/use-mobile'; |
| 8 | +import { describeEachAppLayout, renderComponent } from './utils'; |
| 9 | + |
| 10 | +describeEachAppLayout({ themes: ['refresh-toolbar'] }, () => { |
| 11 | + test('content dom reference is preserved when changing state', () => { |
| 12 | + const refSpy = jest.fn(); |
| 13 | + const { rerender, wrapper } = renderComponent( |
| 14 | + <AppLayout navigation="testing" content={<div ref={refSpy}>content</div>} /> |
| 15 | + ); |
| 16 | + |
| 17 | + expect(refSpy).toHaveBeenCalledTimes(1); |
| 18 | + expect(refSpy).toHaveBeenCalledWith(expect.any(HTMLElement)); |
| 19 | + expect(wrapper.findContentRegion().getElement()).toHaveTextContent('content'); |
| 20 | + |
| 21 | + refSpy.mockClear(); |
| 22 | + rerender(<AppLayout navigationHide={true} navigation="testing" content={<div ref={refSpy}>content</div>} />); |
| 23 | + expect(refSpy).toHaveBeenCalledTimes(0); |
| 24 | + expect(wrapper.findContentRegion().getElement()).toHaveTextContent('content'); |
| 25 | + |
| 26 | + rerender(<></>); |
| 27 | + expect(refSpy).toHaveBeenCalledTimes(1); |
| 28 | + expect(refSpy).toHaveBeenCalledWith(null); |
| 29 | + }); |
| 30 | + |
| 31 | + // does not work without this fix: https://github.com/cloudscape-design/component-toolkit/pull/151 |
| 32 | + test.skip('content dom reference is preserved when changing switching between desktop and mobile', () => { |
| 33 | + const refSpy = jest.fn(); |
| 34 | + renderComponent(<AppLayout content={<div ref={refSpy}>content</div>} />); |
| 35 | + |
| 36 | + expect(refSpy).toHaveBeenCalledTimes(1); |
| 37 | + expect(refSpy).toHaveBeenCalledWith(expect.any(HTMLElement)); |
| 38 | + |
| 39 | + refSpy.mockClear(); |
| 40 | + const globalWithFlags = globalThis as any; |
| 41 | + globalWithFlags[forceMobileModeSymbol] = !globalWithFlags[forceMobileModeSymbol]; |
| 42 | + act(() => { |
| 43 | + window.dispatchEvent(new CustomEvent('resize')); |
| 44 | + }); |
| 45 | + expect(refSpy).toHaveBeenCalledTimes(0); |
| 46 | + |
| 47 | + globalWithFlags[forceMobileModeSymbol] = !globalWithFlags[forceMobileModeSymbol]; |
| 48 | + act(() => { |
| 49 | + window.dispatchEvent(new CustomEvent('resize')); |
| 50 | + }); |
| 51 | + expect(refSpy).toHaveBeenCalledTimes(0); |
| 52 | + }); |
| 53 | +}); |
0 commit comments