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) Correct Typo in Openmrs Component Decorator For Strict Mode #1213

Merged
merged 3 commits into from
Nov 25, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import React, { Component } from 'react';
import { render, screen, waitFor} from '@testing-library/react';
import { openmrsComponentDecorator } from './openmrsComponentDecorator';
import { ComponentContext } from './ComponentContext';

Expand Down Expand Up @@ -35,6 +35,22 @@ describe('openmrs-component-decorator', () => {
const DecoratedComp = openmrsComponentDecorator(opts)(CompWithConfig);
render(<DecoratedComp />);
});

it("rendering a unsafe component in strict mode should log error in console",()=>{
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
const UnsafeDecoratedCompnent = openmrsComponentDecorator(opts)(UnsafeComponent);
render(<UnsafeDecoratedCompnent/>);
expect(consoleError.mock.calls[0][0]).toContain('Warning: Using UNSAFE_componentWillMount');
consoleError.mockRestore();
})

it("rendering an unsafe component without strict mode should not log an error in console",()=>{
const spy = jest.spyOn(console, "error");
const unsafeComponentOptions=Object.assign(opts,{strictMode:false})
const UnsafeDecoratedCompnent = openmrsComponentDecorator(unsafeComponentOptions)(UnsafeComponent);
render(<UnsafeDecoratedCompnent/>);
expect(spy).not.toHaveBeenCalled();
})
});

function CompThatWorks() {
Expand All @@ -49,3 +65,14 @@ function CompWithConfig() {
const { moduleName } = React.useContext(ComponentContext);
return <div>{moduleName}</div>;
}


class UnsafeComponent extends Component<any,any> {

UNSAFE_componentWillMount() {
}

render() {
return <h1>This is Unsafe Component</h1>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export function openmrsComponentDecorator<T>(userOpts: ComponentDecoratorOptions
</SWRConfig>
</Suspense>
);

if (opts.strictMode || !React.StrictMode) {
if (!opts.strictMode || !React.StrictMode) {
return content;
} else {
return <React.StrictMode>{content}</React.StrictMode>;
Expand Down
Loading