Skip to content

Commit 05d2b74

Browse files
committed
feat(tests): add tests for value change handling and confirmation popup in InContextDiscussionFields
1 parent c842c4a commit 05d2b74

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

src/advanced-settings/setting-card/SettingCard.test.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ describe('<SettingCard />', () => {
105105
expect(input).not.toBeDisabled();
106106
});
107107

108+
it('calls setIsEditableState when value changes and isEditableState is false', async () => {
109+
// Test for line 45: setIsEditableState(true) called when value changes
110+
const { getByLabelText } = render(<RootWrapper isEditableState={false} />);
111+
const input = getByLabelText(/Setting Name/i);
112+
fireEvent.change(input, { target: { value: 'new-different-value' } });
113+
await waitFor(() => {
114+
expect(setIsEditableState).toHaveBeenCalledWith(true);
115+
});
116+
});
117+
108118
it('shows help popup when clicking info button', () => {
109119
render(<RootWrapper />);
110120
const helpButton = screen.getByRole('button', { name: /show help text/i });

src/pages-and-resources/discussions/app-config-form/apps/shared/InContextDiscussionFields.test.jsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// @ts-check
2-
import { render, screen } from '@testing-library/react';
2+
import { render, screen, fireEvent } from '@testing-library/react';
33
import { IntlProvider } from '@edx/frontend-platform/i18n';
44
import { Formik, Form } from 'formik';
55
import InContextDiscussionFields from './InContextDiscussionFields';
66

77
const defaultProps = {
88
onBlur: jest.fn(),
99
onChange: jest.fn(),
10+
setFieldValue: jest.fn(),
1011
values: {
1112
enableGradedUnits: false,
1213
groupAtSubsection: false,
@@ -17,9 +18,11 @@ const renderComponent = (props = {}) =>
1718
render(
1819
<IntlProvider locale="en">
1920
<Formik initialValues={defaultProps.values} onSubmit={jest.fn()}>
20-
<Form>
21-
<InContextDiscussionFields {...defaultProps} {...props} />
22-
</Form>
21+
{() => (
22+
<Form>
23+
<InContextDiscussionFields {...defaultProps} {...props} />
24+
</Form>
25+
)}
2326
</Formik>
2427
</IntlProvider>,
2528
);
@@ -40,12 +43,15 @@ describe('InContextDiscussionFields', () => {
4043
expect(screen.getByText(/Visibility of in-context discussions/)).toBeInTheDocument();
4144
});
4245

43-
it('accepts different values', () => {
46+
it('renders with default disabled (false)', () => {
47+
// Test default param - no disabled prop passed
48+
renderComponent();
49+
expect(screen.getByText(/Visibility of in-context discussions/)).toBeInTheDocument();
50+
});
51+
52+
it('renders with enableGradedUnits true (shows cancel labels)', () => {
4453
renderComponent({
45-
values: {
46-
enableGradedUnits: true,
47-
groupAtSubsection: true,
48-
},
54+
values: { enableGradedUnits: true, groupAtSubsection: false },
4955
});
5056
expect(screen.getByText(/Visibility of in-context discussions/)).toBeInTheDocument();
5157
});
@@ -65,4 +71,28 @@ describe('InContextDiscussionFields', () => {
6571
const switchControl = screen.getByLabelText(/enable discussions/i);
6672
expect(switchControl).toBeDisabled();
6773
});
74+
75+
it('shows confirmation popup and handles confirm', () => {
76+
renderComponent({ disabled: false, setFieldValue: jest.fn() });
77+
// Click the switch to show popup
78+
const switchControl = screen.getByLabelText(/enable discussions/i);
79+
fireEvent.click(switchControl);
80+
// Check popup appears with confirm button
81+
expect(screen.getByText(/Confirm/i)).toBeInTheDocument();
82+
// Click confirm
83+
fireEvent.click(screen.getByText(/Confirm/i));
84+
// Popup should close (no longer visible)
85+
});
86+
87+
it('shows confirmation popup and handles cancel', () => {
88+
renderComponent({ disabled: false, setFieldValue: jest.fn() });
89+
// Click the switch to show popup
90+
const switchControl = screen.getByLabelText(/enable discussions/i);
91+
fireEvent.click(switchControl);
92+
// Check popup appears with cancel button
93+
expect(screen.getByText(/Cancel/i)).toBeInTheDocument();
94+
// Click cancel
95+
fireEvent.click(screen.getByText(/Cancel/i));
96+
// Popup should close (no longer visible)
97+
});
6898
});

0 commit comments

Comments
 (0)