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: enable React StrictMode for tests and Storybook #7061

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setLanguage } from '@ui5/webcomponents-base/dist/config/Language.js';
import { setTheme } from '@ui5/webcomponents-base/dist/config/Theme.js';
import applyDirection from '@ui5/webcomponents-base/dist/locale/applyDirection.js';
import { ContentDensity, Modals, ThemeProvider } from '@ui5/webcomponents-react';
import { useEffect } from 'react';
import { StrictMode, useEffect } from 'react';
import 'tocbot/dist/tocbot.css';
import '../packages/main/dist/Assets.js';
import languages from './components/languageCodes.json';
Expand Down Expand Up @@ -56,10 +56,12 @@ const preview: Preview = {
}, [theme]);

return (
<ThemeProvider>
{viewMode !== 'docs' && <Modals />}
<Story />
</ThemeProvider>
<StrictMode>
<ThemeProvider>
{viewMode !== 'docs' && <Modals />}
<Story />
</ThemeProvider>
</StrictMode>
);
}
],
Expand Down
6 changes: 4 additions & 2 deletions cypress/support/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ declare global {
namespace Cypress {
interface Chainable {
/**
* Cypress mount with ThemeProvider
* Cypress mount with ThemeProvider.
*
* __Note:__ Per default `options.strict` is enabled
*/
mount: (
jsx: ReactNode,
Expand Down Expand Up @@ -43,7 +45,7 @@ declare global {
* Cypress mount with ThemeProvider
*/
Cypress.Commands.add('mount', (component, { themeProviderProps = {}, ...options } = {}) => {
return mount(<ThemeProvider {...themeProviderProps}>{component}</ThemeProvider>, options);
return mount(<ThemeProvider {...themeProviderProps}>{component}</ThemeProvider>, { strict: true, ...options });
});

// copied from https://github.com/cypress-io/cypress/discussions/21150#discussioncomment-2620947 and edited slightly
Expand Down
27 changes: 17 additions & 10 deletions packages/compat/src/components/Toolbar/Toolbar.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Toolbar', () => {
it('overflow menu', () => {
const onOverflowChange = cy.spy().as('overflowChangeSpy');
cy.viewport(300, 500);
cy.mount(<OverflowTestComponent onOverflowChange={onOverflowChange} />);
cy.mount(<OverflowTestComponent onOverflowChange={onOverflowChange} />, { strict: false });
cy.get('@overflowChangeSpy').should('have.been.calledOnce');
cy.findByTestId('toolbarElements').should('have.text', 2);
cy.findByTestId('overflowElements').should('have.text', 3);
Expand All @@ -159,7 +159,7 @@ describe('Toolbar', () => {

// flaky - remount component instead
// cy.get(`[ui5-toggle-button]`).click();
cy.mount(<OverflowTestComponent onOverflowChange={onOverflowChange} />);
cy.mount(<OverflowTestComponent onOverflowChange={onOverflowChange} />, { strict: false });
cy.get('[ui5-popover]').should('not.have.attr', 'open');

cy.get('@overflowChangeSpy').should('have.callCount', 2);
Expand Down Expand Up @@ -324,7 +324,8 @@ describe('Toolbar', () => {
<Toolbar data-testid="tb" design={design}>
<Text>Item1</Text>
<Text>Item2</Text>
</Toolbar>
</Toolbar>,
{ strict: false }
);
let height = '44px'; //2.75rem
let background = 'rgba(0, 0, 0, 0)'; // transparent
Expand Down Expand Up @@ -371,7 +372,8 @@ describe('Toolbar', () => {
<Text data-testid="tbi" style={{ width: '100px' }}>
Item3
</Text>
</Toolbar>
</Toolbar>,
{ strict: false }
);
cy.wait(200);
cy.findAllByTestId('tbi').each(($el) => {
Expand All @@ -395,7 +397,8 @@ describe('Toolbar', () => {
<Text data-testid="tbi" style={{ width: '100px' }}>
Item3
</Text>
</Toolbar>
</Toolbar>,
{ strict: false }
);
cy.wait(200);
cy.findAllByTestId('tbiV').each(($el) => {
Expand Down Expand Up @@ -529,7 +532,7 @@ describe('Toolbar', () => {
);
};
const overflowChange = cy.spy().as('overflowChange');
cy.mount(<TestComp onOverflowChange={overflowChange} style={{ width: '200px' }} />);
cy.mount(<TestComp onOverflowChange={overflowChange} style={{ width: '200px' }} />, { strict: false });

cy.get('[ui5-toggle-button]').should('not.exist');
cy.findByText('add').click();
Expand Down Expand Up @@ -573,7 +576,8 @@ describe('Toolbar', () => {
<div id="1">Text1</div>
<div>Text2 no id</div>
<Button id="3">Text4</Button>
</Toolbar>
</Toolbar>,
{ strict: false }
);

cy.get('#1').should('have.length', 1);
Expand All @@ -590,7 +594,8 @@ describe('Toolbar', () => {
<div>Text1</div>
<div>Text2</div>
<Button>Text4</Button>
</Toolbar>
</Toolbar>,
{ strict: false }
);
cy.get('section[role="alertdialog"]').should('exist');

Expand All @@ -599,7 +604,8 @@ describe('Toolbar', () => {
<div>Text1</div>
<div>Text2</div>
<Button>Text4</Button>
</Toolbar>
</Toolbar>,
{ strict: false }
);
cy.get('section').should('not.have.attr', 'role');
cy.get('[data-component-name="ToolbarOverflowPopoverContent"]').should('have.attr', 'role', 'menu');
Expand All @@ -609,7 +615,8 @@ describe('Toolbar', () => {
<div>Text1</div>
<div>Text2</div>
<Button>Text4</Button>
</Toolbar>
</Toolbar>,
{ strict: false }
);
cy.get('section').should('not.have.attr', 'role');
cy.get('[data-component-name="ToolbarOverflowPopoverContent"]').should('have.attr', 'role', 'menu');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,15 @@ describe('AnalyticalTable', () => {
const { onRowSelect } = props;
const [relevantPayload, setRelevantPayload] = useState<Record<string, any>>({});
const tableInstance = useRef<Record<string, any>>(null);
// strict mode
const hasRun = useRef(false);

useEffect(() => {
if (tableInstance.current) {
tableInstance.current.setGroupBy(['name']);
if (tableInstance.current && !hasRun.current) {
setTimeout(() => {
tableInstance.current.toggleAllRowsExpanded();
tableInstance.current.toggleAllRowsExpanded(true);
}, 100);
hasRun.current = true;
}
}, []);

Expand Down Expand Up @@ -850,6 +852,7 @@ describe('AnalyticalTable', () => {
onRowSelect(e);
}}
data={groupableData}
reactTableOptions={{ initialState: { groupBy: ['name'] } }}
selectionMode="Multiple"
/>
<div data-testid="selectedFlatRowsLength">
Expand Down
3 changes: 2 additions & 1 deletion packages/main/src/components/ThemeProvider/I18n.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ describe('I18nProvider', () => {
</>
);
};
cy.mount(<TranslationComponent />);
// strict mode disabled, otherwise counter is incremented twice on each render
cy.mount(<TranslationComponent />, { strict: false });
cy.findByTestId('counter').should('have.text', 1);
cy.findByText('Please wait')
.then(() => {
Expand Down
Loading