From 441dc05e1d4fba4f784798f6b9e3f8e387c0c4ca Mon Sep 17 00:00:00 2001 From: Yury Saukou Date: Wed, 13 Nov 2024 22:21:38 +0400 Subject: [PATCH] add unit tests --- .../VersionCheckbox/VersionCheckbox.test.js | 39 +++++++++++ .../VersionKeyValue/VersionKeyValue.test.js | 64 +++++++++++++++++++ .../VersionView/VersionView.test.js | 52 +++++++++++++++ lib/hooks/useLineHoldings/useLineHoldings.js | 4 +- .../useOrganization/useOrganization.test.js | 4 +- lib/hooks/useUsersBatch/useUsersBatch.js | 4 +- lib/hooks/useUsersBatch/useUsersBatch.test.js | 3 +- 7 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.test.js create mode 100644 lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.test.js create mode 100644 lib/VersionHistory/components/VersionView/VersionView.test.js diff --git a/lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.test.js b/lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.test.js new file mode 100644 index 00000000..2e3fd021 --- /dev/null +++ b/lib/VersionHistory/components/VersionCheckbox/VersionCheckbox.test.js @@ -0,0 +1,39 @@ +import { + render, + screen, +} from '@testing-library/react'; + +import { VersionViewContext } from '../../VersionViewContext'; +import { VersionCheckbox } from './VersionCheckbox'; + +const defaultProps = { + label: 'Test Label', + name: 'testName', +}; + +const renderVersionCheckbox = (props = {}, contextValue = {}) => { + return render( + + + , + ); +}; + +describe('VersionCheckbox', () => { + it('renders with marked label when name is in context paths', () => { + renderVersionCheckbox({}, { paths: ['testName'] }); + + screen.debug(); + + expect(screen.getByText('Test Label').closest('mark')).toBeInTheDocument(); + }); + + it('renders with normal label when name is not in context paths', () => { + renderVersionCheckbox({}, { paths: ['otherName'] }); + + expect(screen.getByText('Test Label').closest('mark')).not.toBeInTheDocument(); + }); +}); diff --git a/lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.test.js b/lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.test.js new file mode 100644 index 00000000..f20a184a --- /dev/null +++ b/lib/VersionHistory/components/VersionKeyValue/VersionKeyValue.test.js @@ -0,0 +1,64 @@ +import { + render, + screen, +} from '@testing-library/react'; + +import { VersionViewContext } from '../../VersionViewContext'; +import { VersionKeyValue } from './VersionKeyValue'; + +const defaultProps = { + label: 'Test Label', + value: 'Test Value', + name: 'testName', +}; + +const renderComponent = (props = {}, contextValue = {}) => { + return render( + + + , + ); +}; + +describe('VersionKeyValue', () => { + it('should render label and value', () => { + renderComponent(); + + expect(screen.getByText('Test Label')).toBeInTheDocument(); + expect(screen.getByText('Test Value')).toBeInTheDocument(); + }); + + it('should render NoValue when value is not provided', () => { + renderComponent({ value: undefined }); + + expect(screen.getByText('Test Label')).toBeInTheDocument(); + expect(screen.getByText('stripes-components.noValue.noValueSet')).toBeInTheDocument(); + }); + + it('should highlight updated value', () => { + renderComponent({ name: 'testName' }, { paths: ['testName'] }); + + expect(screen.getByText('Test Value').closest('mark')).toBeInTheDocument(); + }); + + it('should not highlight non-updated value', () => { + renderComponent({}, { paths: ['anotherName'] }); + + expect(screen.getByText('Test Value').closest('mark')).not.toBeInTheDocument(); + }); + + it('should highlight updated value for multiple fields', () => { + renderComponent({ multiple: true }, { paths: ['testName[0]'] }); + + expect(screen.getByText('Test Value').closest('mark')).toBeInTheDocument(); + }); + + it('should not highlight non-updated value for multiple fields', () => { + renderComponent({ multiple: true }, { paths: ['anotherName[0]'] }); + + expect(screen.getByText('Test Value').closest('mark')).not.toBeInTheDocument(); + }); +}); diff --git a/lib/VersionHistory/components/VersionView/VersionView.test.js b/lib/VersionHistory/components/VersionView/VersionView.test.js new file mode 100644 index 00000000..422b0596 --- /dev/null +++ b/lib/VersionHistory/components/VersionView/VersionView.test.js @@ -0,0 +1,52 @@ +import { + render, + screen, +} from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import VersionView from './VersionView'; + +const defaultProps = { + children:
Version Content
, + id: 'test-id', + isLoading: false, + onClose: jest.fn(), + tags: [{ id: 'tag1' }, { id: 'tag2' }], + versionId: 'version1', + dismissible: true, +}; + +const renderComponent = (props = {}) => render( + , +); + +describe('VersionView', () => { + it('should render loading pane when isLoading is true', () => { + renderComponent({ isLoading: true }); + + expect(screen.queryByText('Version Content')).not.toBeInTheDocument(); + }); + + it('should render children when version exists and is not loading', () => { + renderComponent(); + + expect(screen.getByText('Version Content')).toBeInTheDocument(); + }); + + it('should render no version message when version does not exist', () => { + renderComponent({ versionId: null }); + + expect(screen.getByText('stripes-acq-components.versionHistory.noVersion')).toBeInTheDocument(); + }); + + it('should call onClose when Pane onClose is triggered', async () => { + renderComponent(); + + await userEvent.click(screen.getByRole('button', { name: 'stripes-components.closeItem' })); + + expect(defaultProps.onClose).toHaveBeenCalled(); + }); +}); diff --git a/lib/hooks/useLineHoldings/useLineHoldings.js b/lib/hooks/useLineHoldings/useLineHoldings.js index cd79b33f..3b01813e 100644 --- a/lib/hooks/useLineHoldings/useLineHoldings.js +++ b/lib/hooks/useLineHoldings/useLineHoldings.js @@ -11,9 +11,9 @@ export const useLineHoldings = (holdingIds) => { const query = useQuery( [namespace, holdingIds], - () => { + ({ signal }) => { return batchRequest( - ({ params: searchParams }) => ky.get(HOLDINGS_API, { searchParams }).json(), + ({ params: searchParams }) => ky.get(HOLDINGS_API, { searchParams, signal }).json(), holdingIds, ); }, diff --git a/lib/hooks/useOrganization/useOrganization.test.js b/lib/hooks/useOrganization/useOrganization.test.js index a3a381f1..5c91bbb7 100644 --- a/lib/hooks/useOrganization/useOrganization.test.js +++ b/lib/hooks/useOrganization/useOrganization.test.js @@ -11,8 +11,6 @@ import { VENDORS_API } from '../../constants'; import { useOrganization } from './useOrganization'; const queryClient = new QueryClient(); - -// eslint-disable-next-line react/prop-types const wrapper = ({ children }) => ( {children} @@ -42,6 +40,6 @@ describe('useOrganization', () => { await waitFor(() => !result.current.isLoading); expect(result.current.organization).toEqual(organization); - expect(mockGet).toHaveBeenCalledWith(`${VENDORS_API}/${organization.id}`); + expect(mockGet).toHaveBeenCalledWith(`${VENDORS_API}/${organization.id}`, expect.any(Object)); }); }); diff --git a/lib/hooks/useUsersBatch/useUsersBatch.js b/lib/hooks/useUsersBatch/useUsersBatch.js index b4650009..256ef2c6 100644 --- a/lib/hooks/useUsersBatch/useUsersBatch.js +++ b/lib/hooks/useUsersBatch/useUsersBatch.js @@ -25,9 +25,9 @@ export const useUsersBatch = (userIds, options = {}) => { isLoading, } = useQuery( [namespace, userIds], - async () => { + async ({ signal }) => { const response = await batchRequest( - ({ params: searchParams }) => ky.get(USERS_API, { searchParams }).json(), + ({ params: searchParams }) => ky.get(USERS_API, { searchParams, signal }).json(), userIds, ); diff --git a/lib/hooks/useUsersBatch/useUsersBatch.test.js b/lib/hooks/useUsersBatch/useUsersBatch.test.js index 97b71787..68eb8e16 100644 --- a/lib/hooks/useUsersBatch/useUsersBatch.test.js +++ b/lib/hooks/useUsersBatch/useUsersBatch.test.js @@ -10,8 +10,6 @@ import { USERS_API } from '../../constants'; import { useUsersBatch } from './useUsersBatch'; const queryClient = new QueryClient(); - -// eslint-disable-next-line react/prop-types const wrapper = ({ children }) => ( {children} @@ -45,6 +43,7 @@ describe('useUsersBatch', () => { searchParams: expect.objectContaining({ query: userIds.map(id => `id==${id}`).join(' or '), }), + signal: expect.any(AbortSignal), }); }); });