Skip to content

Commit ad43d71

Browse files
authored
chore: Add more tests on app layout state management (#3722)
1 parent eb48d66 commit ad43d71

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
});

src/app-layout/__tests__/utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { ComponentWrapper } from '@cloudscape-design/test-utils-core/dom';
1010

1111
import AppLayout, { AppLayoutProps } from '../../../lib/components/app-layout';
1212
import customCssProps from '../../../lib/components/internal/generated/custom-css-properties';
13+
import { forceMobileModeSymbol } from '../../../lib/components/internal/hooks/use-mobile';
1314
import { SplitPanelProps } from '../../../lib/components/split-panel';
1415
import createWrapper, { AppLayoutWrapper, ElementWrapper } from '../../../lib/components/test-utils/dom';
15-
import { forceMobileModeSymbol } from '../../internal/hooks/use-mobile';
1616

1717
import testutilStyles from '../../../lib/components/app-layout/test-classes/styles.css.js';
1818
import visualRefreshStyles from '../../../lib/components/app-layout/visual-refresh/styles.css.js';

0 commit comments

Comments
 (0)