Skip to content

Commit

Permalink
Add base test cases for DC context
Browse files Browse the repository at this point in the history
  • Loading branch information
amattu2 committed Nov 7, 2023
1 parent d7b28c5 commit b17c04c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/components/Contexts/DataCommonContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { FC } from 'react';
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { useDataCommonContext, Status as DCStatus, DataCommonProvider } from './DataCommonContext';

const TestChild: FC = () => {
const { status } = useDataCommonContext();

if (status === DCStatus.LOADING) {
return null;
}

return (
<div data-testid="status">{status}</div>
);
};

type Props = {
dc: string;
children?: React.ReactNode;
};

const TestParent: FC<Props> = ({ dc, children } : Props) => (
<DataCommonProvider DataCommon={dc}>
{children ?? <TestChild />}
</DataCommonProvider>
);

describe("DataCommonContext > useDataCommonContext Tests", () => {
it("should throw an exception when used outside of a DataCommonProvider", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
expect(() => render(<TestChild />)).toThrow("useDataCommonContext cannot be used outside of the DataCommonProvider component");
jest.spyOn(console, "error").mockRestore();
});
});

describe("DataCommonContext > DataCommonProvider Tests", () => {
it("should render without crashing", () => {
render(<TestParent dc="XYZ" />);
});
});

0 comments on commit b17c04c

Please sign in to comment.