Skip to content

Commit

Permalink
feat: UILD-406: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s3fs committed Oct 31, 2024
1 parent aff1c78 commit 01a7848
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const ExternalResourceLoader = () => {
const { externalId } = useParams();

return (
<div className="external-resource-loader">
<div className="external-resource-loader" data-testid="external-resource-loader">
<div className="contents">
<FormattedMessage id="ld.fetchingExternalResourceById" values={{ resourceId: externalId }} />
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/test/__mocks__/common/hooks/useRecordControls.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const discardRecord = jest.fn();
export const fetchRecord = jest.fn();
export const clearRecordState = jest.fn();
export const fetchRecordAndSelectEntityValues = jest.fn();
export const fetchExternalRecordForPreview = jest.fn();

jest.mock('@common/hooks/useRecordControls', () => ({
useRecordControls: () => ({
Expand All @@ -13,5 +14,6 @@ jest.mock('@common/hooks/useRecordControls', () => ({
clearRecordState,
saveRecordLocally,
fetchRecordAndSelectEntityValues,
fetchExternalRecordForPreview,
}),
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PreviewExternalResourceControls } from '@components/PreviewExternalResourceControls';
import { fireEvent, screen } from '@testing-library/dom';
import { render } from '@testing-library/react';
import { RecoilRoot } from 'recoil';

const navigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => navigate,
}));

describe('PreviewExternalResourceControls', () => {
beforeEach(() =>
render(
<RecoilRoot>
<PreviewExternalResourceControls />
</RecoilRoot>,
),
);

test('navigates back', () => {
fireEvent.click(screen.getByTestId('close-external-preview-button'));

expect(navigate).toHaveBeenCalled();
});
});
37 changes: 37 additions & 0 deletions src/test/__tests__/components/PreviewExternalResourcePane.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PreviewExternalResourcePane } from '@components/PreviewExternalResourcePane';
import state from '@state';
import { fireEvent, screen } from '@testing-library/dom';
import { render } from '@testing-library/react';
import { RecoilRoot } from 'recoil';

const navigate = jest.fn();
const getRecordTitle = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => navigate,
}));

jest.mock('@common/helpers/record.helper', () => ({
getRecordTitle: () => getRecordTitle(),
}));

describe('PreviewExternalResourcePane', () => {
beforeEach(() =>
render(
<RecoilRoot initializeState={snapshot => snapshot.set(state.inputs.record, {})}>
<PreviewExternalResourcePane />
</RecoilRoot>,
),
);

test('invokes getRecordTitle', () => {
expect(getRecordTitle).toHaveBeenCalled();
});

test('navigates back', () => {
fireEvent.click(screen.getByTestId('nav-close-button'));

expect(navigate).toHaveBeenCalled();
});
});
48 changes: 48 additions & 0 deletions src/test/__tests__/views/ExternalResourcePreview.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { fetchExternalRecordForPreview } from '@src/test/__mocks__/common/hooks/useRecordControls.mock';
import '@src/test/__mocks__/common/hooks/useRecordControls.mock';
import state from '@state';
import { render, screen } from '@testing-library/react';
import { ExternalResourcePreview } from '@views';
import { Fragment, ReactNode } from 'react';
import { RecoilRoot } from 'recoil';

jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: false }));

jest.mock('react-intl', () => ({
FormattedMessage: ({ id, values }: any) => {
return (
<div id={id}>
{Object.entries(values).map(([k, v]) => (
<Fragment key={k}>{v as ReactNode}</Fragment>
))}
</div>
);
},
}));

describe('ExternalResourcePreview', () => {
const renderComponent = (withRecord = false) =>
render(
<RecoilRoot initializeState={snapshot => snapshot.set(state.inputs.record, withRecord ? {} : null)}>
<ExternalResourcePreview />
</RecoilRoot>,
);

test('calls fetchExternalRecordForPreview on externalId change', () => {
renderComponent();

expect(fetchExternalRecordForPreview).toHaveBeenCalled();
});

test('renders ExternalResourceLoader if no record is present', () => {
renderComponent();

expect(screen.getByTestId('external-resource-loader')).toBeInTheDocument();
});

test('renders Preview if record is present', () => {
renderComponent(true);

expect(screen.getByTestId('preview-fields')).toBeInTheDocument();
});
});

0 comments on commit 01a7848

Please sign in to comment.