-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIQM-574 Added tests for Source File selection functionality
- Loading branch information
1 parent
50db59a
commit 81c8fa3
Showing
11 changed files
with
276 additions
and
30 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { render } from '@folio/jest-config-stripes/testing-library/react'; | ||
import { Form } from 'react-final-form'; | ||
import arrayMutators from 'final-form-arrays'; | ||
|
||
import { ControlNumberField } from './ControlNumberField'; | ||
import { MARC_TYPES } from '../../../common/constants'; | ||
import { QUICK_MARC_ACTIONS } from '../../constants'; | ||
|
||
jest.mock('../../SourceFileLookup', () => ({ | ||
SourceFileLookup: ({ onSourceFileSelect }) => ( | ||
<button type="button" onClick={() => onSourceFileSelect({ target: { value: 'test-source-file-id' } })}>Select file</button> | ||
), | ||
})); | ||
|
||
const getControlNumberField = (props = {}, initialValues) => ( | ||
<Form | ||
onSubmit={jest.fn()} | ||
mutators={{ ...arrayMutators }} | ||
initialValues={{ | ||
controlNumber: 'n 50000331', | ||
}} | ||
{...initialValues} | ||
render={() => ( | ||
<ControlNumberField | ||
id="id-1" | ||
name="controlNumber" | ||
marcType={MARC_TYPES.AUTHORITY} | ||
action={QUICK_MARC_ACTIONS.CREATE} | ||
{...props} | ||
/> | ||
)} | ||
/> | ||
); | ||
|
||
const renderControlNumberField = (props = {}) => render(getControlNumberField(props)); | ||
|
||
describe('Given ControlNumberField', () => { | ||
it('should render content field', () => { | ||
const { | ||
getByRole, | ||
getByText, | ||
} = renderControlNumberField(); | ||
|
||
expect(getByRole('textbox')).toBeDefined(); | ||
expect(getByText('ui-quick-marc.sourceFileLookup')).toBeDefined(); | ||
}); | ||
|
||
describe('when marc type is not AUTHORITY', () => { | ||
it('should not render source file lookup', () => { | ||
const { queryByText } = renderControlNumberField({ | ||
marcType: MARC_TYPES.BIB, | ||
}); | ||
|
||
expect(queryByText('ui-quick-marc.sourceFileLookup')).not.toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('when action is not CREATE', () => { | ||
it('should not render source file lookup', () => { | ||
const { queryByText } = renderControlNumberField({ | ||
action: QUICK_MARC_ACTIONS.EDIT, | ||
}); | ||
|
||
expect(queryByText('ui-quick-marc.sourceFileLookup')).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/QuickMarcEditor/SourceFileLookup/SourceFileLookup.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
render, | ||
fireEvent, | ||
waitFor, | ||
} from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { SourceFileLookup } from './SourceFileLookup'; | ||
import { useAuthoritySourceFiles } from '../../queries'; | ||
import Harness from '../../../test/jest/helpers/harness'; | ||
|
||
jest.mock('./SourceFileLookupModal', () => ({ | ||
SourceFileLookupModal: ({ onConfirm }) => ( | ||
<> | ||
SourceFileLookupModal | ||
<button type="button" onClick={() => onConfirm('source-file-id')}>Confirm</button> | ||
</> | ||
), | ||
})); | ||
|
||
jest.mock('../../queries', () => ({ | ||
...jest.requireActual('../../queries'), | ||
useAuthoritySourceFiles: jest.fn(), | ||
})); | ||
|
||
const mockOnSourceFileSelect = jest.fn(); | ||
|
||
const renderSourceFileLookup = (props = {}) => render( | ||
<SourceFileLookup | ||
onSourceFileSelect={mockOnSourceFileSelect} | ||
{...props} | ||
/>, | ||
{ wrapper: Harness }, | ||
); | ||
|
||
const sourceFiles = [{ | ||
id: 'source-file-id', | ||
name: 'Test source file', | ||
}]; | ||
|
||
describe('Given SourceFileLookup', () => { | ||
beforeAll(() => { | ||
useAuthoritySourceFiles.mockReturnValue({ | ||
sourceFiles, | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render the modal trigger button', async () => { | ||
const { | ||
getByRole, | ||
getByText, | ||
} = renderSourceFileLookup(); | ||
|
||
fireEvent.click(getByRole('button', { name: 'ui-quick-marc.sourceFileLookup' })); | ||
|
||
await waitFor(() => expect(getByText('SourceFileLookupModal')).toBeDefined()); | ||
}); | ||
|
||
describe('when confirming source file selection in modal', () => { | ||
it('should call onSourceFileSelect callback with correct source file', async () => { | ||
const { | ||
getByRole, | ||
getByText, | ||
} = renderSourceFileLookup(); | ||
|
||
fireEvent.click(getByRole('button', { name: 'ui-quick-marc.sourceFileLookup' })); | ||
fireEvent.click(getByText('Confirm')); | ||
|
||
expect(mockOnSourceFileSelect).toHaveBeenCalledWith(sourceFiles[0]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/QuickMarcEditor/SourceFileLookup/SourceFileLookupModal.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
render, | ||
fireEvent, | ||
} from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { SourceFileLookupModal } from './SourceFileLookupModal'; | ||
|
||
const sourceFileOptions = [{ | ||
label: 'Source File A', | ||
value: 'sfa', | ||
}, { | ||
label: 'Source File B', | ||
value: 'sfb', | ||
}]; | ||
|
||
const mockOnConfirm = jest.fn(); | ||
const mockOnCancel = jest.fn(); | ||
|
||
const renderSourceFileLookupModal = (props = {}) => render( | ||
<SourceFileLookupModal | ||
open | ||
sourceFileOptions={sourceFileOptions} | ||
onConfirm={mockOnConfirm} | ||
onCancel={mockOnCancel} | ||
{...props} | ||
/>, | ||
); | ||
|
||
describe('Given SourceFileLookupModal', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render modal', () => { | ||
const { getByText } = renderSourceFileLookupModal(); | ||
|
||
expect(getByText('ui-quick-marc.sourceFileLookupModal')).toBeDefined(); | ||
}); | ||
|
||
describe('when no value is selected', () => { | ||
it('should have empty default value selected', () => { | ||
const { getByLabelText } = renderSourceFileLookupModal(); | ||
|
||
const select = getByLabelText('ui-quick-marc.sourceFileLookupModal.fieldLabel'); | ||
|
||
expect(select.value).toEqual(''); | ||
}); | ||
|
||
it('should disable Save & close button', () => { | ||
const { getByRole } = renderSourceFileLookupModal(); | ||
|
||
const button = getByRole('button', { name: 'stripes-components.saveAndClose' }); | ||
|
||
expect(button).toBeDisabled(); | ||
}); | ||
}); | ||
|
||
describe('when some source file value is selected', () => { | ||
it('should enable Save & close button', async () => { | ||
const { | ||
getByRole, | ||
getByLabelText, | ||
} = renderSourceFileLookupModal(); | ||
|
||
const select = getByLabelText('ui-quick-marc.sourceFileLookupModal.fieldLabel'); | ||
|
||
fireEvent.change(select, { target: { value: sourceFileOptions[0].value } }); | ||
|
||
const button = getByRole('button', { name: 'stripes-components.saveAndClose' }); | ||
|
||
expect(button).toBeEnabled(); | ||
}); | ||
}); | ||
|
||
describe('when confirming Source File selection', () => { | ||
it('should call onConfirm with correct source file id', async () => { | ||
const { | ||
getByRole, | ||
getByLabelText, | ||
} = renderSourceFileLookupModal(); | ||
|
||
const select = getByLabelText('ui-quick-marc.sourceFileLookupModal.fieldLabel'); | ||
|
||
fireEvent.change(select, { target: { value: sourceFileOptions[0].value } }); | ||
|
||
const button = getByRole('button', { name: 'stripes-components.saveAndClose' }); | ||
|
||
fireEvent.click(button); | ||
|
||
expect(mockOnConfirm).toHaveBeenCalledWith(sourceFileOptions[0].value); | ||
}); | ||
}); | ||
|
||
describe('when closing the modal', () => { | ||
it('should call onCancel', async () => { | ||
const { getByRole } = renderSourceFileLookupModal(); | ||
|
||
const button = getByRole('button', { name: 'stripes-components.cancel' }); | ||
|
||
fireEvent.click(button); | ||
|
||
expect(mockOnCancel).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './SourceFileLookup'; |
Oops, something went wrong.