Skip to content

Commit d14181d

Browse files
authored
Merge pull request #9520 from GilbertCherrie/fix_start_page
Fix start page not being honoured
2 parents 3f9c023 + 11a9881 commit d14181d

File tree

5 files changed

+93
-17
lines changed

5 files changed

+93
-17
lines changed

app/javascript/components/visual-settings-form/index.jsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,36 @@ import MiqFormRenderer from '@@ddf';
55
import createSchema from './visual-settings-form.schema';
66

77
const VisualSettingsForm = ({ recordId }) => {
8-
const [{ initialValues, timezoneOptions, isLoading }, setState] = useState({ isLoading: true });
8+
const [{
9+
initialValues, shortcuts, timezoneOptions, isLoading,
10+
}, setState] = useState({ isLoading: true });
911

1012
useEffect(() => {
11-
API.get('/api').then(({ timezones }) => {
12-
const timezoneOptions = [];
13-
timezones.forEach((timezone) => {
14-
timezoneOptions.push({ value: timezone.name, label: timezone.description });
13+
API.get('/api/shortcuts?expand=resources&attributes=description,url').then(({ resources }) => {
14+
const shortcuts = [];
15+
16+
resources.forEach((shortcut) => {
17+
shortcuts.push({ value: shortcut.url, label: shortcut.description });
18+
});
19+
20+
return shortcuts;
21+
}).then((shortcuts) => {
22+
API.get('/api').then(({ timezones }) => {
23+
const timezoneOptions = [];
24+
25+
timezones.forEach((timezone) => {
26+
timezoneOptions.push({ value: timezone.name, label: timezone.description });
27+
});
28+
29+
return timezoneOptions;
30+
}).then((timezoneOptions) => {
31+
API.get(`/api/users/${recordId}?attributes=settings`).then(({ settings }) => setState({
32+
initialValues: settings,
33+
shortcuts,
34+
timezoneOptions,
35+
isLoading: false,
36+
}));
1537
});
16-
return timezoneOptions;
17-
}).then((timezoneOptions) => {
18-
API.get(`/api/users/${recordId}?attributes=settings`).then(({ settings }) => setState({
19-
initialValues: settings,
20-
timezoneOptions,
21-
isLoading: false,
22-
}));
2338
});
2439
}, [recordId]);
2540

@@ -42,7 +57,7 @@ const VisualSettingsForm = ({ recordId }) => {
4257
}
4358
return (
4459
<MiqFormRenderer
45-
schema={createSchema(timezoneOptions)}
60+
schema={createSchema(shortcuts, timezoneOptions)}
4661
initialValues={initialValues}
4762
onSubmit={onSubmit}
4863
canReset

app/javascript/components/visual-settings-form/visual-settings-form.schema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { componentTypes } from '@@ddf';
22

3-
const createSchema = (timezoneOptions) => ({
3+
const createSchema = (shortcuts, timezoneOptions) => ({
44
fields: [
55
{
66
component: componentTypes.SUB_FORM,
@@ -98,8 +98,8 @@ const createSchema = (timezoneOptions) => ({
9898
id: 'display.startpage',
9999
label: __('Start Page'),
100100
isSearchable: true,
101-
loadOptions: () => API.get('/api/shortcuts?expand=resources&attributes=description,url')
102-
.then(({ resources }) => resources.map(({ url, description }) => ({ value: url, label: description }))),
101+
simpleValue: true,
102+
options: shortcuts,
103103
},
104104
{
105105
component: componentTypes.SELECT,

cypress/e2e/ui/settings.cy.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
/* eslint-disable no-undef */
2-
describe('GTL', () => {
2+
describe('Settings > My Settings', () => {
33
beforeEach(() => {
44
cy.login();
5+
cy.menu('Settings', 'My Settings');
6+
});
7+
8+
it('Saves the start page setting', () => {
9+
cy.get('#display\\.startpage').then((value) => {
10+
expect(value[0].value).to.be.oneOf(['', 'Overview / Dashboard']);
11+
});
12+
cy.changeSelect('display\\.startpage', 'Overview / Utilization');
13+
cy.intercept('PATCH', '/api/users/*').as('settingsUpdate');
14+
cy.contains('button', 'Save').click();
15+
cy.wait('@settingsUpdate').its('response.statusCode').should('eq', 200);
16+
17+
// Wait for page to load before clicking log out to prevent errors
18+
cy.get('[name="general-subform"] > :nth-child(2) > .bx--select');
19+
cy.get('#menu_item_logout').click();
20+
cy.login();
21+
cy.url().should('include', '/utilization');
22+
23+
cy.menu('Settings', 'My Settings');
24+
cy.get('#display\\.startpage').then((value) => {
25+
expect(value[0].value).to.equal('Overview / Utilization');
26+
});
27+
cy.changeSelect('display\\.startpage', 'Overview / Dashboard');
28+
cy.intercept('PATCH', '/api/users/*').as('settingsUpdate');
29+
cy.contains('button', 'Save').click();
30+
cy.wait('@settingsUpdate').its('response.statusCode').should('eq', 200);
31+
32+
cy.get('[name="general-subform"] > :nth-child(2) > .bx--select');
33+
cy.get('#menu_item_logout').click();
34+
cy.login();
35+
cy.url().should('include', '/dashboard');
536
});
637
});

cypress/support/commands/select.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable no-undef */
2+
3+
// selectId: String of the ID of the select element to interact with.
4+
// optionToSelect: String of the option to select from the dropdown.
5+
Cypress.Commands.add('changeSelect', (selectId, optionToSelect) => {
6+
// First, get the select element and store its text
7+
let selectElementText = '';
8+
cy.get(`#${selectId}`).then((selectElement) => {
9+
// Get the currently displayed text in the select element
10+
selectElementText = selectElement.text().trim();
11+
// Click to open the dropdown
12+
cy.wrap(selectElement).click();
13+
}).then(() => {
14+
// Now find the options and try to select the requested one
15+
cy.get('.bx--list-box__menu-item__option').then((options) => {
16+
const optionArray = Cypress.$.makeArray(options);
17+
const match = optionArray.find((el) => el.innerText.trim() === optionToSelect);
18+
19+
if (match) {
20+
cy.wrap(match).click();
21+
} else {
22+
// Include both the requested option and the select element's text in the error
23+
cy.logAndThrowError(
24+
`Could not find "${optionToSelect}" in select element with text "${selectElementText}"`
25+
);
26+
}
27+
});
28+
});
29+
});

cypress/support/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import './commands/menu.js';
5050
import './commands/stub_notifications.js';
5151
import './commands/throttle_response.js';
5252
import './commands/toolbar.js';
53+
import './commands/select.js';
5354

5455
// Assertions
5556
import './assertions/expect_alerts.js';

0 commit comments

Comments
 (0)