Fix: Glossary and Glossary term page issues. (#4381)

* Fix: Glossary and Glossary term page issues.

* Add unit test
This commit is contained in:
Sachin Chaurasiya 2022-04-23 01:36:42 +05:30 committed by GitHub
parent def8a1624f
commit d84e2c2a8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 25 deletions

View File

@ -48,6 +48,7 @@ const AddGlossary = ({
const [showErrorMsg, setShowErrorMsg] = useState<{ [key: string]: boolean }>({
name: false,
invalidName: false,
description: false,
});
const [name, setName] = useState('');
@ -55,6 +56,10 @@ const AddGlossary = ({
const [showRevieweModal, setShowRevieweModal] = useState(false);
const [reviewer, setReviewer] = useState<Array<FormattedUsersData>>([]);
const getDescription = () => {
return markdownRef.current?.getEditorContent() || undefined;
};
const onReviewerModalCancel = () => {
setShowRevieweModal(false);
};
@ -99,6 +104,7 @@ const AddGlossary = ({
const errMsg = {
name: !name.trim(),
invalidName: UrlEntityCharRegEx.test(name.trim()),
description: !getDescription()?.trim(),
};
setShowErrorMsg(errMsg);
@ -110,7 +116,7 @@ const AddGlossary = ({
const data: CreateGlossary = {
name,
displayName: name,
description: markdownRef.current?.getEditorContent() || undefined,
description: getDescription(),
reviewers: reviewer.map((d) => ({ id: d.id, type: d.type })),
owner: {
id: getCurrentUserId(),
@ -210,7 +216,7 @@ const AddGlossary = ({
<label
className="tw-block tw-form-label tw-mb-0"
htmlFor="description">
Description:
{requiredField('Description:')}
</label>
<RichTextEditor
data-testid="description"
@ -218,6 +224,7 @@ const AddGlossary = ({
readonly={!allowAccess}
ref={markdownRef}
/>
{showErrorMsg.description && errorMsg('Description is required.')}
</Field>
<div>

View File

@ -79,6 +79,9 @@ describe('Test AddGlossary component', () => {
});
it('should be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('description') },
});
const { container } = render(<AddGlossary {...mockProps} />);
const nameInput = getByTestId(container, 'name');
@ -98,4 +101,28 @@ describe('Test AddGlossary component', () => {
expect(mockOnSave).toBeCalled();
});
it('should not be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('') },
});
const { container } = render(<AddGlossary {...mockProps} />);
const nameInput = getByTestId(container, 'name');
const saveButton = getByTestId(container, 'save-glossary');
expect(saveButton).toBeInTheDocument();
fireEvent.change(nameInput, { target: { value: 'Test Glossary' } });
fireEvent.click(
saveButton,
new MouseEvent('click', {
bubbles: true,
cancelable: true,
})
);
expect(mockOnSave).not.toBeCalled();
});
});

View File

@ -63,6 +63,7 @@ const AddGlossaryTerm = ({
name: false,
invalidName: false,
invalidReferences: false,
description: false,
});
const [name, setName] = useState('');
@ -82,6 +83,10 @@ const AddGlossaryTerm = ({
}
}, [glossaryData]);
const getDescription = () => {
return markdownRef.current?.getEditorContent() || undefined;
};
const onRelatedTermsModalCancel = () => {
setShowRelatedTermsModal(false);
};
@ -187,6 +192,7 @@ const AddGlossaryTerm = ({
name: !name.trim(),
invalidName: !isUrlFriendlyName(name.trim()),
invalidReferences: !isValidReferences(refs),
description: !getDescription()?.trim(),
};
setShowErrorMsg(errMsg);
@ -210,7 +216,7 @@ const AddGlossaryTerm = ({
const data: CreateGlossaryTerm = {
name,
displayName: name,
description: markdownRef.current?.getEditorContent() || undefined,
description: getDescription(),
reviewers: reviewer.map((r) => ({
id: r.id,
type: r.type,
@ -323,7 +329,7 @@ const AddGlossaryTerm = ({
<label
className="tw-block tw-form-label tw-mb-0"
htmlFor="description">
Description:
{requiredField('Description:')}
</label>
<RichTextEditor
data-testid="description"
@ -331,6 +337,7 @@ const AddGlossaryTerm = ({
readonly={!allowAccess}
ref={markdownRef}
/>
{showErrorMsg.description && errorMsg('Description is required.')}
</Field>
<Field>

View File

@ -87,6 +87,10 @@ describe('Test AddGlossaryTerm component', () => {
});
it('should be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('description') },
});
const { container } = render(<AddGlossaryTerm {...mockProps} />);
const nameInput = getByTestId(container, 'name');
@ -106,4 +110,29 @@ describe('Test AddGlossaryTerm component', () => {
expect(mockOnSave).toBeCalled();
});
it('should not be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('') },
});
const { container } = render(<AddGlossaryTerm {...mockProps} />);
const nameInput = getByTestId(container, 'name');
const saveButton = getByTestId(container, 'save-glossary-term');
expect(saveButton).toBeInTheDocument();
fireEvent.change(nameInput, { target: { value: 'Test Glossary Term' } });
fireEvent.click(
saveButton,
new MouseEvent('click', {
bubbles: true,
cancelable: true,
})
);
expect(mockOnSave).not.toBeCalled();
});
});

View File

@ -29,7 +29,6 @@ import { GlossaryTerm } from '../../generated/entity/data/glossaryTerm';
import { useAuth } from '../../hooks/authHooks';
import { ModifiedGlossaryData } from '../../pages/GlossaryPage/GlossaryPageV1.component';
import { generateTreeData, getActionsList } from '../../utils/GlossaryUtils';
import { dropdownIcon as DropdownIcon } from '../../utils/svgconstant';
import { Button } from '../buttons/Button/Button';
import ErrorPlaceHolder from '../common/error-with-placeholder/ErrorPlaceHolder';
import NonAdminAction from '../common/non-admin-action/NonAdminAction';
@ -261,26 +260,8 @@ const GlossaryV1 = ({
size="small"
theme="primary"
variant="contained"
onClick={() => {
setShowActions((show) => !show);
}}>
Actions{' '}
{showActions ? (
<DropdownIcon
style={{
transform: 'rotate(180deg)',
marginTop: '2px',
color: '#fff',
}}
/>
) : (
<DropdownIcon
style={{
marginTop: '2px',
color: '#fff',
}}
/>
)}
onClick={handleAddGlossaryTermClick}>
Add term
</Button>
</NonAdminAction>
{showActions && (