Skip to content

Commit 92df203

Browse files
authored
Merge pull request #4395 from dlabrecq/sort
Cost Management metrics list should appear sorted
2 parents b7177f2 + 0d12531 commit 92df203

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/routes/settings/costModels/components/addPriceList.test.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ describe('add-a-new-rate', () => {
126126

127127
await user.type(screen.getByLabelText('Description'), 'regular rate test');
128128

129-
// select first option for metric
129+
// select CPU metric explicitly by name
130130
await user.click(screen.getByLabelText('Select metric'));
131131
options = await screen.findAllByRole('option');
132-
await user.click(options[0]);
132+
await user.click(options.find(o => /"value":"cpu"/i.test(o.textContent)));
133133

134-
// select first option for measurement
134+
// select measurement by name (avoid brittle index)
135135
await user.click(screen.getByLabelText('Select measurement'));
136136
options = await screen.findAllByRole('option');
137-
await user.click(options[3]); // Previous select options are not being removed from page
137+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
138138

139139
// make sure the default cost type is selected
140140
expect(screen.getByLabelText(qr.infraradio)).toHaveProperty('checked', true);
@@ -144,7 +144,7 @@ describe('add-a-new-rate', () => {
144144

145145
await user.click(screen.getByLabelText('Select measurement'));
146146
options = await screen.findAllByRole('option');
147-
await user.click(options[1]);
147+
await user.click(options.find(o => /"value":"request"/i.test(o.textContent)));
148148

149149
expect(screen.getByLabelText(qr.supplradio)).toHaveProperty('checked', true);
150150

@@ -153,13 +153,13 @@ describe('add-a-new-rate', () => {
153153

154154
await user.click(screen.getByLabelText('Select metric'));
155155
options = await screen.findAllByRole('option');
156-
await user.click(options[1]);
156+
await user.click(options.find(o => /"value":"memory"/i.test(o.textContent)));
157157

158158
expect(screen.getByText(regExp(messages.costModelsRequiredField))).not.toBeNull();
159159

160160
await user.click(screen.getByLabelText('Select measurement'));
161161
options = await screen.findAllByRole('option');
162-
await user.click(options[3]); // Previous select options are not being removed from page
162+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
163163

164164
expect(screen.getByLabelText(qr.supplradio)).toHaveProperty('checked', true);
165165
await user.click(screen.getByLabelText(qr.infraradio));
@@ -183,7 +183,7 @@ describe('add-a-new-rate', () => {
183183

184184
// making sure button is enabled
185185
const createButton = screen.getByRole('button', { name: regExp(messages.createRate)} );
186-
expect(createButton).not.toBeDisabled;
186+
expect(createButton).not.toBeDisabled();
187187
await user.click(createButton);
188188
expect(submit).toHaveBeenCalled();
189189
}, 10000);
@@ -200,11 +200,11 @@ describe('add-a-new-rate', () => {
200200

201201
await user.click(screen.getByLabelText('Select metric'));
202202
options = await screen.findAllByRole('option');
203-
await user.click(options[0]);
203+
await user.click(options.find(o => /"value":"cpu"/i.test(o.textContent)));
204204

205205
await user.click(screen.getByLabelText('Select measurement'));
206206
options = await screen.findAllByRole('option');
207-
await user.click(options[3]); // Previous select options are not being removed from page
207+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
208208

209209
await user.click(screen.getByLabelText(regExp(messages.costModelsEnterTagRate)));
210210

@@ -246,7 +246,7 @@ describe('add-a-new-rate', () => {
246246

247247
await user.type(tagRateInput, '0.23');
248248
await user.type(screen.getByPlaceholderText('Enter a tag description'), 'default worker');
249-
expect(createButton).not.toBeDisabled;
249+
expect(createButton).not.toBeDisabled();
250250

251251
// set tag to default
252252
await user.click(screen.getByLabelText('Default'));
@@ -271,11 +271,11 @@ describe('add-a-new-rate', () => {
271271

272272
await user.click(screen.getByLabelText('Select metric'));
273273
options = await screen.findAllByRole('option');
274-
await user.click(options[1]);
274+
await user.click(options.find(o => /"value":"memory"/i.test(o.textContent)));
275275

276276
await user.click(screen.getByLabelText('Select measurement'));
277277
options = await screen.findAllByRole('option');
278-
await user.click(options[4]); // Previous select options are not being removed from page
278+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
279279

280280
await user.click(screen.getByLabelText(regExp(messages.costModelsEnterTagRate)));
281281

@@ -293,13 +293,13 @@ describe('add-a-new-rate', () => {
293293

294294
await user.click(screen.getByLabelText('Select measurement'));
295295
options = await screen.findAllByRole('option');
296-
await user.click(options[1]);
296+
await user.click(options.find(o => /"value":"request"/i.test(o.textContent)));
297297

298298
expect(screen.queryByText(regExp(messages.priceListDuplicate))).toBeNull();
299299

300300
await user.click(screen.getByLabelText('Select measurement'));
301301
options = await screen.findAllByRole('option');
302-
await user.click(options[4]);
302+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
303303

304304
expect(screen.getByText(regExp(messages.priceListDuplicate))).not.toBeNull();
305305
});

src/routes/settings/costModels/components/inputs/selector.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ const SelectorBase: React.FC<SelectorProps> = ({
7676
value: option.value,
7777
};
7878
});
79-
return selectOptions;
79+
return selectOptions.sort((a: any, b: any) => {
80+
if (a.toString() < b.toString()) {
81+
return -1;
82+
}
83+
if (a.toString() > b.toString()) {
84+
return 1;
85+
}
86+
return 0;
87+
});
8088
};
8189

8290
const handleOnSelect = (_evt, sel: SelectWrapperOption) => {

src/routes/settings/costModels/costModel/updateRateModel.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,24 @@ describe('update-rate', () => {
295295
// Popper is setting the menu to aria-hidden after a selection is made the first time"
296296
await user.click(screen.getByLabelText('Select measurement'));
297297
options = await screen.findAllByRole('option', { hidden: true});
298-
await user.click(options[1]);
298+
await user.click(options.find(o => /"value":"request"/i.test(o.textContent)));
299299

300300
expect(saveButton).not.toBeDisabled();
301301

302302
await user.click(screen.getByLabelText('Select measurement'));
303303
options = await screen.findAllByRole('option', { hidden: true});
304-
await user.click(options[0]);
304+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
305305

306306
expect(saveButton).toBeDisabled();
307307

308308
await user.click(screen.getByLabelText('Select metric'));
309309
options = await screen.findAllByRole('option', { hidden: true});
310-
await user.click(options[1]);
310+
await user.click(options.find(o => /"value":"memory"/i.test(o.textContent)));
311+
312+
// After changing metric, measurement is reset and must be re-selected
313+
await user.click(screen.getByLabelText('Select measurement'));
314+
options = await screen.findAllByRole('option', { hidden: true});
315+
await user.click(options.find(o => /"value":"usage"/i.test(o.textContent)));
311316

312317
expect(saveButton).not.toBeDisabled();
313318
}, 10000);
@@ -383,7 +388,7 @@ describe('update-rate', () => {
383388

384389
await user.click(screen.getByLabelText('Select measurement'));
385390
options = await screen.findAllByRole('option');
386-
await user.click(options[1]);
391+
await user.click(options.find(o => /"value":"request"/i.test(o.textContent)));
387392

388393
await user.click(screen.getByLabelText(regExp(messages.costModelsEnterTagRate)));
389394
await act(async () =>

0 commit comments

Comments
 (0)