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

fix(GlobalLoader): render in StrictMode #3582

Open
wants to merge 4 commits into
base: next
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Calendar', () => {
}),
);

await waitFor(() => expect(onMonthChange).toHaveReturnedWith({ month: 7, year: 2017 }), { timeout: 5000 });
await waitFor(() => expect(onMonthChange).toHaveReturnedWith({ month: 7, year: 2017 }), { timeout: 8000 });
});

it('onMonthChange returns correct year', async () => {
Expand All @@ -106,7 +106,7 @@ describe('Calendar', () => {
);
});

await waitFor(() => expect(onMonthChange).toHaveLastReturnedWith({ month: 6, year: 2018 }), { timeout: 5000 });
await waitFor(() => expect(onMonthChange).toHaveLastReturnedWith({ month: 6, year: 2018 }), { timeout: 8000 });
});

it('should set langCode', () => {
Expand Down
16 changes: 10 additions & 6 deletions packages/react-ui/components/GlobalLoader/GlobalLoader.tsx
SchwJ marked this conversation as resolved.
Show resolved Hide resolved
SchwJ marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type DefaultProps = Required<
>
>;

let currentGlobalLoader: GlobalLoader;
let currentGlobalLoader: GlobalLoader | null;

/**
* Глобальный лоадер `GlobalLoader` — это универсальный индикатор обмена данными с сервером.
Expand Down Expand Up @@ -132,6 +132,10 @@ export class GlobalLoader extends React.Component<GlobalLoaderProps, GlobalLoade
}
}

componentWillUnmount(): void {
currentGlobalLoader = null;
}

componentDidUpdate(prevProps: Readonly<GlobalLoaderProps>) {
const { expectedResponseTime, rejected, active } = this.getProps();
if (expectedResponseTime !== prevProps.expectedResponseTime) {
Expand Down Expand Up @@ -183,9 +187,9 @@ export class GlobalLoader extends React.Component<GlobalLoaderProps, GlobalLoade
* @public
*/
public static start = (expectedResponseTime?: number) => {
currentGlobalLoader.setActive();
currentGlobalLoader?.setActive();
if (typeof expectedResponseTime === 'number') {
currentGlobalLoader.updateExpectedResponseTime(expectedResponseTime);
currentGlobalLoader?.updateExpectedResponseTime(expectedResponseTime);
}
};

Expand All @@ -196,7 +200,7 @@ export class GlobalLoader extends React.Component<GlobalLoaderProps, GlobalLoade
* @public
*/
public static done = () => {
currentGlobalLoader.setDone();
currentGlobalLoader?.setDone();
};

/**
Expand All @@ -206,7 +210,7 @@ export class GlobalLoader extends React.Component<GlobalLoaderProps, GlobalLoade
* @public
*/
public static reject = () => {
currentGlobalLoader.setReject(true);
currentGlobalLoader?.setReject(true);
};

/**
Expand All @@ -216,7 +220,7 @@ export class GlobalLoader extends React.Component<GlobalLoaderProps, GlobalLoade
* @public
*/
public static accept = () => {
currentGlobalLoader.setReject(false);
currentGlobalLoader?.setReject(false);
};

public setActive = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { StrictMode } from 'react';
import { act, render, screen, waitFor } from '@testing-library/react';

import { GlobalLoader, GlobalLoaderDataTids } from '../GlobalLoader';
Expand All @@ -25,6 +25,40 @@ describe('Global Loader', () => {
expect(refGlobalLoader2.current?.state.dead).toBe(false);
});

describe('in StrictMode', () => {
it('should render', async () => {
render(
<StrictMode>
<GlobalLoader expectedResponseTime={2000} active ref={refGlobalLoader} />
</StrictMode>,
);

await delay(DELAY_BEFORE_GLOBAL_LOADER_SHOW);

expect(screen.getByTestId(GlobalLoaderDataTids.root)).toBeInTheDocument();
expect(refGlobalLoader.current?.state.dead).toBe(false);
});

it('should only render one instance', async () => {
render(
<StrictMode>
<GlobalLoader expectedResponseTime={2000} active ref={refGlobalLoader} />
</StrictMode>,
);
render(
<StrictMode>
<GlobalLoader expectedResponseTime={2000} active ref={refGlobalLoader2} />
</StrictMode>,
);

await delay(DELAY_BEFORE_GLOBAL_LOADER_SHOW);

expect(screen.getByTestId(GlobalLoaderDataTids.root)).toBeInTheDocument();
expect(refGlobalLoader.current?.state.dead).toBe(true);
expect(refGlobalLoader2.current?.state.dead).toBe(false);
});
});

describe('with props', () => {
it('should set active', async () => {
render(
Expand Down