Skip to content

Commit

Permalink
fix: Fixed the display of the selection of available time zones.
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroAlipov committed Oct 4, 2023
1 parent 03e3b67 commit ea3efe4
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/account-settings/EditableSelectField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,31 @@ const EditableSelectField = (props) => {
value: confirmationValue,
});
};
const selectOptions = options.map(option => (
<option value={option.value} key={`${option.value}-${option.label}`}>{option.label}</option>
));

const selectOptions = options.map((option) => {
if (option.group) {
// If the option has a 'group' property, it represents an element with sub-options.
return (
<optgroup label={option.label} key={option.label}>
{option.group.map((subOption) => (
<option
value={subOption.value}
key={`${subOption.value}-${subOption.label}`}
>
{subOption.label}
</option>
))}
</optgroup>
);
} else {

Check failure on line 117 in src/account-settings/EditableSelectField.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Unnecessary 'else' after 'return'
// If the option doesn't have a 'group' property, it's a regular list element.
return (
<option value={option.value} key={`${option.value}-${option.label}`}>
{option.label}
</option>
);
}
});

return (
<SwitchContent
Expand Down
123 changes: 123 additions & 0 deletions src/account-settings/test/EditableSelectField.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import React from 'react';
import { Router } from 'react-router-dom';
import { Provider } from 'react-redux';
import { render, screen } from '@testing-library/react';
import configureStore from 'redux-mock-store';
import { createMemoryHistory } from 'history';

import * as auth from '@edx/frontend-platform/auth';
import { IntlProvider, injectIntl } from '@edx/frontend-platform/i18n';

import EditableSelectField from '../EditableSelectField';

const mockDispatch = jest.fn();
jest.mock('react-redux', () => ({
...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 => (
<Router history={history}>
<IntlProvider locale="en">
<Provider store={store}>{children}</Provider>
</IntlProvider>
</Router>
);

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(() => ({

Check failure on line 91 in src/account-settings/test/EditableSelectField.test.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

The members of 'auth' are read-only
patch: async () => ({
data: { status: 200 },
catch: () => {},
}),
}));
auth.getAuthenticatedUser = jest.fn(() => ({ userId: 5 }));

Check failure on line 97 in src/account-settings/test/EditableSelectField.test.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

The members of 'auth' are read-only
});

afterEach(() => jest.clearAllMocks());

it("renders EditableSelectField correctly with editing disabled", () => {

Check failure on line 102 in src/account-settings/test/EditableSelectField.test.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Strings must use singlequote
render(reduxWrapper(<IntlEditableSelectField {...props} />));

expect(screen.getByText('Main Label')).toBeInTheDocument();
expect(screen.getByText('Edit')).toBeInTheDocument();
});

it("renders EditableSelectField correctly with editing enabled", () => {

Check failure on line 109 in src/account-settings/test/EditableSelectField.test.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Strings must use singlequote
props = {
...props,
isEditing: true,
}

Check failure on line 113 in src/account-settings/test/EditableSelectField.test.jsx

View workflow job for this annotation

GitHub Actions / tests (lint)

Missing semicolon

render(reduxWrapper(<IntlEditableSelectField {...props} />));

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();
});
});

0 comments on commit ea3efe4

Please sign in to comment.