Skip to content

Commit

Permalink
Remove implicit test-only behavior for LiveRegionController.
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashbot committed Oct 18, 2024
1 parent e70baf5 commit c7f361a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/date-range-picker/__tests__/date-range-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { warnOnce } from '@cloudscape-design/component-toolkit/internal';
import DateRangePicker, { DateRangePickerProps } from '../../../lib/components/date-range-picker';
import FormField from '../../../lib/components/form-field';
import TestI18nProvider from '../../../lib/components/i18n/testing';
import { LiveRegionController } from '../../../lib/components/internal/components/live-region/controller.js';
import { NonCancelableEventHandler } from '../../../lib/components/internal/events';
import createWrapper from '../../../lib/components/test-utils/dom';
import DateRangePickerWrapper from '../../../lib/components/test-utils/dom/date-range-picker';
Expand All @@ -18,6 +19,8 @@ import { isValidRange } from './is-valid-range';

import segmentedStyles from '../../../lib/components/segmented-control/styles.css.js';

LiveRegionController.defaultMinDelay = 0;

jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
warnOnce: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions src/flashbar/__tests__/flashbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { disableMotion } from '@cloudscape-design/global-styles';

import Button from '../../../lib/components/button';
import Flashbar, { FlashbarProps } from '../../../lib/components/flashbar';
import { LiveRegionController } from '../../../lib/components/internal/components/live-region/controller.js';
import createWrapper from '../../../lib/components/test-utils/dom';
import { mockInnerText } from '../../internal/analytics/__tests__/mocks';
import { createFlashbarWrapper, findList, testFlashDismissal } from './common';
Expand All @@ -24,6 +25,8 @@ jest.mock('../../../lib/components/internal/hooks/use-visual-mode', () => {
};
});

LiveRegionController.defaultMinDelay = 0;

mockInnerText();

const noop = () => void 0;
Expand Down
15 changes: 9 additions & 6 deletions src/internal/components/live-region/controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { isTest } from '../../is-development.js';

import styles from './styles.css.js';

const DEFAULT_MIN_DELAY = isTest ? 0 : 2000;

/**
* The controller singleton that manages a single live region container. It has a timer and
* a queue to make sure announcements don't collide and messages are debounced correctly.
Expand All @@ -15,7 +11,14 @@ const DEFAULT_MIN_DELAY = isTest ? 0 : 2000;
*
* @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions
*/
class LiveRegionController {
export class LiveRegionController {
/**
* The default delay for announcements when no delay is explicitly provided.
* During unit testing, you can import this and explicitly set it to 0 to make
* the live region update the DOM without waiting for a timer.
*/
public static defaultMinDelay = 2000;

private _element: HTMLElement | undefined;
private _timeoutId: number | undefined;
private _lastAnnouncement = '';
Expand Down Expand Up @@ -52,7 +55,7 @@ class LiveRegionController {
}
}

announce(message: string, minDelay = DEFAULT_MIN_DELAY) {
announce(message: string, minDelay = LiveRegionController.defaultMinDelay) {
this._nextMessages.add(message);

if (this._nextDelay < minDelay) {
Expand Down
7 changes: 0 additions & 7 deletions src/internal/is-development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@ declare const process: { env: { NODE_ENV?: string } };
* and remove the conditional statement for production bundles.
*/
export const isDevelopment = process.env.NODE_ENV !== 'production';

/**
* Whether the execution environment is unit testing, like Jest or Vitest.
* Use this sparingly - components shouldn't behave radically differently in
* test environments.
*/
export const isTest = process.env.NODE_ENV === 'test';
2 changes: 2 additions & 0 deletions src/s3-resource-selector/s3-modal/__tests__/fetching.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import React from 'react';
import { act, render, screen, waitFor } from '@testing-library/react';

import { LiveRegionController } from '../../../../lib/components/internal/components/live-region/controller.js';
import { S3Modal } from '../../../../lib/components/s3-resource-selector/s3-modal';
import createWrapper, { ElementWrapper } from '../../../../lib/components/test-utils/dom';
import { buckets, i18nStrings, objects, versions, waitForFetch } from '../../__tests__/fixtures';
import { getElementsText, modalDefaultProps, navigateToTableItem } from './utils';

import styles from '../../../../lib/components/s3-resource-selector/s3-modal/styles.css.js';

LiveRegionController.defaultMinDelay = 0;
jest.setTimeout(30_000);

async function renderModal(jsx: React.ReactElement) {
Expand Down
3 changes: 3 additions & 0 deletions src/table/__tests__/body-cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as React from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';

import { LiveRegionController } from '../../../lib/components/internal/components/live-region/controller.js';
import { TableBodyCell, TableBodyCellProps } from '../../../lib/components/table/body-cell';
import { useStickyColumns } from '../../../lib/components/table/sticky-columns';
import wrapper from '../../../lib/components/test-utils/dom';
Expand All @@ -18,6 +19,8 @@ const testItem = {
test: 'testData',
};

LiveRegionController.defaultMinDelay = 0;

const stickyCellRef = jest.fn();

jest.mock('../../../lib/components/table/sticky-columns', () => ({
Expand Down

0 comments on commit c7f361a

Please sign in to comment.