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

View File

@ -79,6 +79,9 @@ describe('Test AddGlossary component', () => {
}); });
it('should be able to save', () => { it('should be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('description') },
});
const { container } = render(<AddGlossary {...mockProps} />); const { container } = render(<AddGlossary {...mockProps} />);
const nameInput = getByTestId(container, 'name'); const nameInput = getByTestId(container, 'name');
@ -98,4 +101,28 @@ describe('Test AddGlossary component', () => {
expect(mockOnSave).toBeCalled(); 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, name: false,
invalidName: false, invalidName: false,
invalidReferences: false, invalidReferences: false,
description: false,
}); });
const [name, setName] = useState(''); const [name, setName] = useState('');
@ -82,6 +83,10 @@ const AddGlossaryTerm = ({
} }
}, [glossaryData]); }, [glossaryData]);
const getDescription = () => {
return markdownRef.current?.getEditorContent() || undefined;
};
const onRelatedTermsModalCancel = () => { const onRelatedTermsModalCancel = () => {
setShowRelatedTermsModal(false); setShowRelatedTermsModal(false);
}; };
@ -187,6 +192,7 @@ const AddGlossaryTerm = ({
name: !name.trim(), name: !name.trim(),
invalidName: !isUrlFriendlyName(name.trim()), invalidName: !isUrlFriendlyName(name.trim()),
invalidReferences: !isValidReferences(refs), invalidReferences: !isValidReferences(refs),
description: !getDescription()?.trim(),
}; };
setShowErrorMsg(errMsg); setShowErrorMsg(errMsg);
@ -210,7 +216,7 @@ const AddGlossaryTerm = ({
const data: CreateGlossaryTerm = { const data: CreateGlossaryTerm = {
name, name,
displayName: name, displayName: name,
description: markdownRef.current?.getEditorContent() || undefined, description: getDescription(),
reviewers: reviewer.map((r) => ({ reviewers: reviewer.map((r) => ({
id: r.id, id: r.id,
type: r.type, type: r.type,
@ -323,7 +329,7 @@ const AddGlossaryTerm = ({
<label <label
className="tw-block tw-form-label tw-mb-0" className="tw-block tw-form-label tw-mb-0"
htmlFor="description"> htmlFor="description">
Description: {requiredField('Description:')}
</label> </label>
<RichTextEditor <RichTextEditor
data-testid="description" data-testid="description"
@ -331,6 +337,7 @@ const AddGlossaryTerm = ({
readonly={!allowAccess} readonly={!allowAccess}
ref={markdownRef} ref={markdownRef}
/> />
{showErrorMsg.description && errorMsg('Description is required.')}
</Field> </Field>
<Field> <Field>

View File

@ -87,6 +87,10 @@ describe('Test AddGlossaryTerm component', () => {
}); });
it('should be able to save', () => { it('should be able to save', () => {
jest.spyOn(React, 'useRef').mockReturnValue({
current: { getEditorContent: jest.fn().mockReturnValue('description') },
});
const { container } = render(<AddGlossaryTerm {...mockProps} />); const { container } = render(<AddGlossaryTerm {...mockProps} />);
const nameInput = getByTestId(container, 'name'); const nameInput = getByTestId(container, 'name');
@ -106,4 +110,29 @@ describe('Test AddGlossaryTerm component', () => {
expect(mockOnSave).toBeCalled(); 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 { useAuth } from '../../hooks/authHooks';
import { ModifiedGlossaryData } from '../../pages/GlossaryPage/GlossaryPageV1.component'; import { ModifiedGlossaryData } from '../../pages/GlossaryPage/GlossaryPageV1.component';
import { generateTreeData, getActionsList } from '../../utils/GlossaryUtils'; import { generateTreeData, getActionsList } from '../../utils/GlossaryUtils';
import { dropdownIcon as DropdownIcon } from '../../utils/svgconstant';
import { Button } from '../buttons/Button/Button'; import { Button } from '../buttons/Button/Button';
import ErrorPlaceHolder from '../common/error-with-placeholder/ErrorPlaceHolder'; import ErrorPlaceHolder from '../common/error-with-placeholder/ErrorPlaceHolder';
import NonAdminAction from '../common/non-admin-action/NonAdminAction'; import NonAdminAction from '../common/non-admin-action/NonAdminAction';
@ -261,26 +260,8 @@ const GlossaryV1 = ({
size="small" size="small"
theme="primary" theme="primary"
variant="contained" variant="contained"
onClick={() => { onClick={handleAddGlossaryTermClick}>
setShowActions((show) => !show); Add term
}}>
Actions{' '}
{showActions ? (
<DropdownIcon
style={{
transform: 'rotate(180deg)',
marginTop: '2px',
color: '#fff',
}}
/>
) : (
<DropdownIcon
style={{
marginTop: '2px',
color: '#fff',
}}
/>
)}
</Button> </Button>
</NonAdminAction> </NonAdminAction>
{showActions && ( {showActions && (