generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 196
chore: Updates test pages and integ tests for compatibility with React 18 (1) #3832
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React, { useContext, useMemo, useRef, useState } from 'react'; | ||
import { orderBy, range } from 'lodash'; | ||
|
||
|
@@ -9,7 +10,6 @@ import { | |
AppLayout, | ||
Button, | ||
ButtonDropdown, | ||
Checkbox, | ||
CollectionPreferences, | ||
ColumnLayout, | ||
Container, | ||
|
@@ -26,7 +26,6 @@ import { | |
SpaceBetween, | ||
StatusIndicator, | ||
} from '~components'; | ||
import { useEffectOnUpdate } from '~components/internal/hooks/use-effect-on-update'; | ||
import { | ||
getTableCellRoleProps, | ||
getTableColHeaderRoleProps, | ||
|
@@ -41,7 +40,8 @@ import { | |
import AppContext, { AppContextType } from '../app/app-context'; | ||
import appLayoutLabels from '../app-layout/utils/labels'; | ||
import { contentDisplayPreferenceI18nStrings } from '../common/i18n-strings'; | ||
import { generateItems, id as generateId, Instance } from '../table/generate-data'; | ||
import { id as generateId, Instance } from '../table/generate-data'; | ||
import { allInstances50 } from '../table/instances-data'; | ||
import { stateToStatusIndicator } from '../table/shared-configs'; | ||
|
||
import styles from './styles.scss'; | ||
|
@@ -56,7 +56,6 @@ type PageContext = React.Context< | |
pageSize: number; | ||
tableRole: TableRole; | ||
actionsMode: ActionsMode; | ||
autoRefresh: boolean; | ||
visibleColumns: string; | ||
}> | ||
>; | ||
|
@@ -70,30 +69,28 @@ const actionsModeOptions = [ | |
{ value: 'inline', label: 'Inline (anti-pattern)' }, | ||
]; | ||
|
||
const allItems = allInstances50.slice(0, 25); | ||
|
||
let refreshIndex = 26; | ||
|
||
export default function Page() { | ||
const [toolsOpen, setToolsOpen] = useState(false); | ||
const { urlParams, setUrlParams } = useContext(AppContext as PageContext); | ||
const { | ||
pageSize = 10, | ||
tableRole = 'grid', | ||
actionsMode = 'dropdown', | ||
autoRefresh = false, | ||
visibleColumns: | ||
visibleColumnsString = 'id,actions,state,imageId,dnsName,type,inline-select,inline-radio,inline-input', | ||
} = urlParams; | ||
const visibleColumns = visibleColumnsString.split(',').filter(Boolean); | ||
|
||
const [items, setItems] = useState(generateItems(25)); | ||
const [refreshCounter, setRefreshCounter] = useState(0); | ||
window.refreshItems = () => setRefreshCounter(prev => prev + 1); | ||
|
||
useEffectOnUpdate(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The auto-refresh feature was not used in integration tests, but it was causing issues: in React 18 with strict mode the useEffectOnUpdate callback is triggered even when the dependencies did not change. As result, the table's state changed, causing some tests to fail. |
||
setItems(prev => [...prev.slice(1), ...generateItems(1)]); | ||
if (autoRefresh) { | ||
const timeoutId = setTimeout(() => setRefreshCounter(prev => prev + 1), 10000); | ||
return () => clearTimeout(timeoutId); | ||
} | ||
}, [autoRefresh, refreshCounter]); | ||
const [items, setItems] = useState(allItems); | ||
const refreshItems = () => | ||
setItems(prev => | ||
refreshIndex < 50 ? [...prev.slice(1), { ...allInstances50[refreshIndex++], id: generateId() }] : prev | ||
); | ||
window.refreshItems = refreshItems; | ||
|
||
const columnDefinitions = [ | ||
{ | ||
|
@@ -210,17 +207,6 @@ export default function Page() { | |
title="Preferences" | ||
confirmLabel="Confirm" | ||
cancelLabel="Cancel" | ||
customPreference={( | ||
customValue: undefined | { autoRefresh: boolean }, | ||
setCustomValue: (value: { autoRefresh: boolean }) => void | ||
) => ( | ||
<Checkbox | ||
checked={!!customValue?.autoRefresh} | ||
onChange={event => setCustomValue({ autoRefresh: event.detail.checked })} | ||
> | ||
Auto-refresh every 10 seconds | ||
</Checkbox> | ||
)} | ||
preferences={{ | ||
contentDisplay: columnDefinitions.map(column => ({ | ||
id: column.key, | ||
|
@@ -233,7 +219,6 @@ export default function Page() { | |
.filter(column => column.visible) | ||
.map(column => column.id) | ||
.join(','), | ||
autoRefresh: detail.custom.autoRefresh, | ||
}) | ||
} | ||
contentDisplayPreference={{ | ||
|
@@ -248,7 +233,7 @@ export default function Page() { | |
}} | ||
/> | ||
|
||
<Button onClick={() => setRefreshCounter(prev => prev + 1)} iconName="refresh" ariaLabel="Refresh"> | ||
<Button onClick={refreshItems} iconName="refresh" ariaLabel="Refresh"> | ||
Refresh | ||
</Button> | ||
</SpaceBetween> | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This onScroll is not reached in React 18 + strict-mode. The nested onScroll do get triggered, so I moved the related code to those handlers instead.