Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fixes flakiness in dropdown width test #2883

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pages/common/flush-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export interface WindowWithFlushResponse extends Window {
__pendingCallbacks: Array<() => void>;
__flushServerResponse: () => void;
}
declare const window: WindowWithFlushResponse;

export function enhanceWindow() {
window.__pendingCallbacks = [];
window.__flushServerResponse = () => {
for (const cb of window.__pendingCallbacks) {
cb();
}
window.__pendingCallbacks = [];
};
}
14 changes: 12 additions & 2 deletions pages/dropdown/width.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import Select, { SelectProps } from '~components/select';
import SpaceBetween from '~components/space-between';

import AppContext, { AppContextType } from '../app/app-context';
import { enhanceWindow, WindowWithFlushResponse } from '../common/flush-response';

declare const window: WindowWithFlushResponse;
enhanceWindow();

type DemoContext = React.Context<
AppContextType<{
Expand All @@ -18,6 +22,7 @@ type DemoContext = React.Context<
virtualScroll: boolean;
expandToViewport: boolean;
containerWidth: string;
manualServerMock: boolean;
}>
>;

Expand Down Expand Up @@ -207,12 +212,17 @@ function CustomSelect({ expandToViewport, loading, onOpen, onClose, virtualScrol

export default function () {
const { urlParams } = useContext(AppContext as DemoContext);
const { asyncLoading, component, triggerWidth, virtualScroll, expandToViewport, containerWidth } = urlParams;
const { asyncLoading, component, triggerWidth, virtualScroll, expandToViewport, containerWidth, manualServerMock } =
urlParams;
const [loading, setLoading] = useState(asyncLoading);
const onOpen = () => {
if (asyncLoading) {
setLoading(true);
setTimeout(() => setLoading(false), 500);
if (manualServerMock) {
window.__pendingCallbacks.push(() => setLoading(false));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be easier to configure a synchronous response or to create an additional page with synchronous data flow instead of introducing these race condition handling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page seems quite complex and I think it is likely that copying it would result in a lot of duplicated code. The manualServerMock only replaces the timeout with an explicit command to simplify integ tests and eliminate flakiness.

} else {
setTimeout(() => setLoading(false), 500);
}
}
};
const onClose = () => setLoading(asyncLoading);
Expand Down
14 changes: 14 additions & 0 deletions src/__integ__/page-objects/async-response-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { BasePageObject } from '@cloudscape-design/browser-test-tools/page-objects';

interface ExtendedWindow extends Window {
__flushServerResponse: () => void;
}
declare const window: ExtendedWindow;

export class AsyncResponsePage extends BasePageObject {
flushResponse() {
return this.browser.execute(() => window.__flushServerResponse());
}
}
11 changes: 5 additions & 6 deletions src/internal/components/dropdown/__integ__/width.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { BasePageObject } from '@cloudscape-design/browser-test-tools/page-objects';
import useBrowser from '@cloudscape-design/browser-test-tools/use-browser';

import createWrapper from '../../../../../lib/components/test-utils/selectors';
import { AsyncResponsePage } from '../../../../__integ__/page-objects/async-response-page';

type ComponentId = 'autosuggest' | 'multiselect' | 'select';

export class DropdownPageObject extends BasePageObject {
export class DropdownPageObject extends AsyncResponsePage {
getWrapperAndTrigger(componentId: ComponentId) {
const wrapper = createWrapper();
let componentWrapper;
Expand Down Expand Up @@ -55,7 +55,7 @@ function setupTest(
) {
return useBrowser({ width: pageWidth, height: 1000 }, async browser => {
await browser.url(
`#/light/dropdown/width?component=${componentId}&expandToViewport=${expandToViewport}&triggerWidth=${triggerWidth}px&asyncLoading=${asyncLoading}`
`#/light/dropdown/width?component=${componentId}&expandToViewport=${expandToViewport}&triggerWidth=${triggerWidth}px&asyncLoading=${asyncLoading}&manualServerMock=${asyncLoading}`
);
const page = new DropdownPageObject(browser);
await page.waitForVisible(page.getWrapperAndTrigger(componentId).wrapper.toSelector());
Expand Down Expand Up @@ -148,9 +148,8 @@ describe('Dropdown width', () => {
});
expect(dropdownBox.left + dropdownBox.width).toBeLessThanOrEqual(pageWidth);
await expect(page.getText(dropdownSelector)).resolves.toContain('Loading');
await page.waitUntil(async () => (await page.getText(dropdownSelector)).includes('A very'), {
timeout: 1000,
});
await page.flushResponse();
await expect(page.getText(dropdownSelector)).resolves.toContain('A very');
const newBox = await page.getBoundingBox(dropdownSelector);
expect(newBox.left + newBox.width).toBeLessThanOrEqual(pageWidth);
}
Expand Down
Loading