Skip to content

Commit

Permalink
(fix) Correct Typo in Openmrs Component Decorator For Strict Mode (#1213
Browse files Browse the repository at this point in the history
)

* correct typo

* correct typo

* correct typo
  • Loading branch information
kb019 authored Nov 25, 2024
1 parent ada9c49 commit 5ca5cf1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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

0 comments on commit 5ca5cf1

Please sign in to comment.