Skip to content

Commit 2e9b5b7

Browse files
test: replacing snapshot tests with RTL tests part 3 (#2134)
* test: replacing snapshot tests with rtl tests part 3 * test: addint alt text to icon buttons and test refactor
1 parent 8a423eb commit 2e9b5b7

File tree

18 files changed

+333
-2742
lines changed

18 files changed

+333
-2742
lines changed

src/editors/containers/EditorContainer/components/TitleHeader/EditConfirmationButtons.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const EditConfirmationButtons = ({
1717
<IconButtonWithTooltip
1818
tooltipPlacement="left"
1919
tooltipContent={intl.formatMessage(messages.saveTitleEdit)}
20+
alt={intl.formatMessage(messages.saveAltText)}
2021
src={Check}
2122
iconAs={Icon}
2223
onClick={updateTitle}
2324
/>
2425
<IconButtonWithTooltip
2526
tooltipPlacement="right"
2627
tooltipContent={intl.formatMessage(messages.cancelTitleEdit)}
28+
alt={intl.formatMessage(messages.cancelAltText)}
2729
src={Close}
2830
iconAs={Icon}
2931
onClick={cancelEdit}

src/editors/containers/EditorContainer/components/TitleHeader/EditConfirmationButtons.test.jsx

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import {
3+
render, screen, fireEvent, initializeMocks,
4+
} from '../../../../../testUtils';
5+
import EditConfirmationButtons from './EditConfirmationButtons';
6+
7+
describe('EditConfirmationButtons', () => {
8+
beforeEach(() => {
9+
initializeMocks();
10+
});
11+
12+
it('renders two IconButtonWithTooltip buttons', () => {
13+
render(
14+
<EditConfirmationButtons updateTitle={jest.fn()} cancelEdit={jest.fn()} />,
15+
);
16+
const cancelButton = screen.getByRole('button', { name: /cancel/i });
17+
expect(cancelButton).toBeInTheDocument();
18+
const saveButton = screen.getByRole('button', { name: /save/i });
19+
expect(saveButton).toBeInTheDocument();
20+
});
21+
22+
it('calls updateTitle when save button is clicked', () => {
23+
const updateTitleMock = jest.fn();
24+
render(
25+
<EditConfirmationButtons updateTitle={updateTitleMock} cancelEdit={jest.fn()} />,
26+
);
27+
fireEvent.click(screen.getByRole('button', { name: /save/i }));
28+
expect(updateTitleMock).toHaveBeenCalled();
29+
});
30+
31+
it('calls cancelEdit when cancel button is clicked', () => {
32+
const cancelEditMock = jest.fn();
33+
render(
34+
<EditConfirmationButtons updateTitle={jest.fn()} cancelEdit={cancelEditMock} />,
35+
);
36+
fireEvent.click(screen.getByRole('button', { name: /cancel/i }));
37+
expect(cancelEditMock).toHaveBeenCalled();
38+
});
39+
});

src/editors/containers/EditorContainer/components/TitleHeader/__snapshots__/EditConfirmationButtons.test.jsx.snap

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/editors/containers/EditorContainer/components/TitleHeader/messages.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ const messages = defineMessages({
2727
defaultMessage: 'Cancel',
2828
description: 'Screen reader label title for icon button to edit the xblock title',
2929
},
30+
cancelAltText: {
31+
id: 'authoring.texteditor.header.cancelAltText',
32+
defaultMessage: 'Cancel',
33+
description: 'Alt text for icon button to cancel edit the xblock title',
34+
},
3035
saveTitleEdit: {
3136
id: 'authoring.texteditor.header.saveTitleEdit',
3237
defaultMessage: 'Save',
3338
description: 'Screen reader label title for icon button to edit the xblock title',
3439
},
40+
saveAltText: {
41+
id: 'authoring.texteditor.header.saveAltText',
42+
defaultMessage: 'Save',
43+
description: 'Alt text for icon button to save edit the xblock title',
44+
},
3545
});
3646

3747
export default messages;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { FormattedMessage, intlShape } from '@edx/frontend-platform/i18n';
3+
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
44
import { Form } from '@openedx/paragon';
55

66
import { answerOptionProps } from '../../../../../../../data/services/cms/types';
@@ -13,46 +13,47 @@ const FeedbackControl = ({
1313
labelMessage,
1414
labelMessageBoldUnderline,
1515
answer,
16-
intl,
1716
type,
1817
images,
1918
isLibrary,
2019
learningContextId,
21-
}) => (
22-
<Form.Group>
23-
<Form.Label className="mb-3">
24-
<FormattedMessage
25-
{...labelMessage}
26-
values={{
27-
answerId: answer.id,
28-
boldunderline: <b><u><FormattedMessage {...labelMessageBoldUnderline} /></u></b>,
20+
}) => {
21+
const intl = useIntl();
22+
return (
23+
<Form.Group>
24+
<Form.Label className="mb-3">
25+
<FormattedMessage
26+
{...labelMessage}
27+
values={{
28+
answerId: answer.id,
29+
boldunderline: <b><u><FormattedMessage {...labelMessageBoldUnderline} /></u></b>,
30+
}}
31+
/>
32+
</Form.Label>
33+
<ExpandableTextArea
34+
id={`${type}Feedback-${answer.id}`}
35+
value={feedback}
36+
setContent={onChange}
37+
placeholder={intl.formatMessage(messages.feedbackPlaceholder)}
38+
{...{
39+
images,
40+
isLibrary,
41+
learningContextId,
2942
}}
3043
/>
31-
</Form.Label>
32-
<ExpandableTextArea
33-
id={`${type}Feedback-${answer.id}`}
34-
value={feedback}
35-
setContent={onChange}
36-
placeholder={intl.formatMessage(messages.feedbackPlaceholder)}
37-
{...{
38-
images,
39-
isLibrary,
40-
learningContextId,
41-
}}
42-
/>
43-
</Form.Group>
44-
);
44+
</Form.Group>
45+
);
46+
};
4547
FeedbackControl.propTypes = {
4648
feedback: PropTypes.string.isRequired,
4749
onChange: PropTypes.func.isRequired,
48-
labelMessage: PropTypes.string.isRequired,
49-
labelMessageBoldUnderline: PropTypes.string.isRequired,
50+
labelMessage: PropTypes.shape.isRequired,
51+
labelMessageBoldUnderline: PropTypes.shape.isRequired,
5052
answer: answerOptionProps.isRequired,
5153
type: PropTypes.string.isRequired,
5254
images: PropTypes.shape({}).isRequired,
5355
learningContextId: PropTypes.string.isRequired,
5456
isLibrary: PropTypes.bool.isRequired,
55-
intl: intlShape.isRequired,
5657
};
5758

5859
export default FeedbackControl;

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/components/Feedback/FeedbackControl.test.jsx

Lines changed: 0 additions & 30 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import {
3+
render, screen, fireEvent, initializeMocks,
4+
} from '../../../../../../../../testUtils';
5+
import FeedbackControl from './FeedbackControl';
6+
7+
jest.mock('../../../../../../../sharedComponents/ExpandableTextArea', () => jest.fn(({
8+
id, value, setContent, placeholder,
9+
}) => (
10+
<textarea id={id} value={value} placeholder={placeholder} onChange={e => setContent(e.target.value)} />
11+
)));
12+
13+
const defaultProps = {
14+
feedback: 'Initial feedback',
15+
onChange: jest.fn(),
16+
labelMessage: { id: 'label.id', defaultMessage: 'Feedback for answer {answerId} {boldunderline}' },
17+
labelMessageBoldUnderline: { id: 'bold.id', defaultMessage: 'Important' },
18+
answer: { id: 'a1', text: 'Answer 1' },
19+
type: 'correct',
20+
images: {},
21+
isLibrary: false,
22+
learningContextId: 'lc1',
23+
};
24+
25+
describe('FeedbackControl', () => {
26+
beforeEach(() => {
27+
initializeMocks();
28+
});
29+
30+
it('renders Form.Label with FormattedMessage and ExpandableTextArea', () => {
31+
render(<FeedbackControl {...defaultProps} />);
32+
expect(screen.getByText(/Feedback for answer/)).toBeInTheDocument();
33+
expect(screen.getByRole('textbox', { name: '' })).toBeInTheDocument();
34+
});
35+
36+
it('passes correct props to ExpandableTextArea', () => {
37+
render(<FeedbackControl {...defaultProps} />);
38+
const textarea = screen.getByRole('textbox', { name: '' });
39+
expect(textarea).toHaveAttribute('id', 'correctFeedback-a1');
40+
expect(textarea).toHaveValue('Initial feedback');
41+
expect(textarea).toHaveAttribute('placeholder', 'Feedback message');
42+
});
43+
44+
it('calls onChange when textarea value changes', () => {
45+
const onChange = jest.fn();
46+
render(<FeedbackControl {...defaultProps} onChange={onChange} />);
47+
const textarea = screen.getByRole('textbox', { name: '' });
48+
fireEvent.change(textarea, { target: { value: 'Updated feedback' } });
49+
expect(onChange).toHaveBeenCalledWith('Updated feedback');
50+
});
51+
52+
it('renders with different isLibrary, images, and learningContextId', () => {
53+
render(<FeedbackControl
54+
{...defaultProps}
55+
isLibrary
56+
images={{ img1: 'url' }}
57+
learningContextId="lc2"
58+
/>);
59+
expect(screen.getByRole('textbox', { name: '' })).toBeInTheDocument();
60+
});
61+
62+
it('renders boldunderline content inside FormattedMessage', () => {
63+
render(<FeedbackControl {...defaultProps} />);
64+
expect(screen.getByText('Important')).toBeInTheDocument();
65+
});
66+
});

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/components/Feedback/__snapshots__/FeedbackControl.test.jsx.snap

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)