From ea3efe44f417a026196b9f23b378bf555d940192 Mon Sep 17 00:00:00 2001 From: alipov_d Date: Wed, 4 Oct 2023 15:45:08 +0300 Subject: [PATCH] fix: Fixed the display of the selection of available time zones. --- src/account-settings/EditableSelectField.jsx | 28 +++- .../test/EditableSelectField.test.jsx | 123 ++++++++++++++++++ 2 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/account-settings/test/EditableSelectField.test.jsx diff --git a/src/account-settings/EditableSelectField.jsx b/src/account-settings/EditableSelectField.jsx index ec2406b0c..473a5d464 100644 --- a/src/account-settings/EditableSelectField.jsx +++ b/src/account-settings/EditableSelectField.jsx @@ -98,9 +98,31 @@ const EditableSelectField = (props) => { value: confirmationValue, }); }; - const selectOptions = options.map(option => ( - - )); + + const selectOptions = options.map((option) => { + if (option.group) { + // If the option has a 'group' property, it represents an element with sub-options. + return ( + + {option.group.map((subOption) => ( + + ))} + + ); + } else { + // If the option doesn't have a 'group' property, it's a regular list element. + return ( + + ); + } + }); return ( ({ + ...jest.requireActual('react-redux'), + useDispatch: () => mockDispatch, +})); + +jest.mock('@edx/frontend-platform/auth'); +jest.mock('../data/selectors', () => jest.fn().mockImplementation(() => ({ certPreferenceSelector: () => ({}) }))); + +const history = createMemoryHistory(); + +const IntlEditableSelectField = injectIntl(EditableSelectField); + +const mockStore = configureStore(); + +describe('EditableSelectField', () => { + let props = {}; + let store = {}; + + const reduxWrapper = children => ( + + + {children} + + + ); + + beforeEach(() => { + store = mockStore(); + props = { + name: 'testField', + label: 'Main Label', + emptyLabel: 'Empty Main Label', + type: 'text', + value: 'Test Field', + userSuppliedValue: '', + options: [ + { + label: 'Default Option', + value: 'defaultOption', + }, + { + label: 'User Options', + group: [ + { + label: 'Suboption 1', + value: 'suboption1', + }, + ], + }, + { + label: 'Other Options', + group: [ + { + label: 'Suboption 2', + value: 'suboption2', + }, + { + label: 'Suboption 3', + value: 'suboption3', + }, + ], + }, + ], + saveState: 'default', + error: '', + confirmationMessageDefinition: { + id: 'confirmationMessageId', + defaultMessage: 'Default Confirmation Message', + description: 'Description of the confirmation message', + }, + confirmationValue: 'Confirmation Value', + helpText: 'Helpful Text', + isEditing: false, + isEditable: true, + isGrayedOut: false, + }; + + auth.getAuthenticatedHttpClient = jest.fn(() => ({ + patch: async () => ({ + data: { status: 200 }, + catch: () => {}, + }), + })); + auth.getAuthenticatedUser = jest.fn(() => ({ userId: 5 })); + }); + + afterEach(() => jest.clearAllMocks()); + + it("renders EditableSelectField correctly with editing disabled", () => { + render(reduxWrapper()); + + expect(screen.getByText('Main Label')).toBeInTheDocument(); + expect(screen.getByText('Edit')).toBeInTheDocument(); + }); + + it("renders EditableSelectField correctly with editing enabled", () => { + props = { + ...props, + isEditing: true, + } + + render(reduxWrapper()); + + expect(screen.getByText('Main Label')).toBeInTheDocument(); + expect(screen.getByText('Suboption 1')).toBeInTheDocument(); + expect(screen.getByText('Default Option')).toBeInTheDocument(); + expect(screen.getByText('Save')).toBeInTheDocument(); + expect(screen.getByText('Cancel')).toBeInTheDocument(); + }); +});