Skip to content

Commit ac1e217

Browse files
authored
Merge pull request #1 from nasa/feature/GUUI-3594-sort-data-rods-variables
Variable Combobox: Sort variables alphabetically
2 parents 66c239f + e90179d commit ac1e217

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

src/components/variable-combobox/variable-combobox.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ export class FetchController {
4141
response: { docs },
4242
} = await response.json()
4343

44-
return cherryPickDocInfo(docs)
44+
return cherryPickDocInfo(docs).sort(
45+
(a, b) =>
46+
// sort by collection short name, then by long name if variables are in the same collection
47+
a.collectionShortName.localeCompare(b.collectionShortName) ||
48+
a.longName.localeCompare(b.longName)
49+
)
4550
},
4651
args: (): any => [],
4752
})
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import '../../../dist/terra-ui-components.js'
2+
import { expect, fixture, html, waitUntil } from '@open-wc/testing'
3+
import sinon from 'sinon'
4+
5+
function okJson(body: unknown) {
6+
return Promise.resolve(
7+
new Response(JSON.stringify(body), {
8+
status: 200,
9+
headers: { 'Content-Type': 'application/json' },
10+
})
11+
)
12+
}
13+
14+
describe('<terra-variable-combobox>', () => {
15+
beforeEach(() => {
16+
sinon.stub(globalThis, 'fetch').callsFake(() =>
17+
okJson({
18+
response: {
19+
docs: [
20+
{
21+
'Collection.ShortName': 'B',
22+
'Collection.Version': '01',
23+
'Variable.LongName': 'B',
24+
'Variable.Name': 'B',
25+
'Variable.Id': 'B_id',
26+
'Variable.Units': 'u',
27+
},
28+
{
29+
'Collection.ShortName': 'C',
30+
'Collection.Version': '01',
31+
'Variable.LongName': 'D',
32+
'Variable.Name': 'D',
33+
'Variable.Id': 'D_id',
34+
'Variable.Units': 'u',
35+
},
36+
{
37+
'Collection.ShortName': 'A',
38+
'Collection.Version': '01',
39+
'Variable.LongName': 'A',
40+
'Variable.Name': 'A',
41+
'Variable.Id': 'A_id',
42+
'Variable.Units': 'u',
43+
},
44+
{
45+
'Collection.ShortName': 'C',
46+
'Collection.Version': '01',
47+
'Variable.LongName': 'C',
48+
'Variable.Name': 'C',
49+
'Variable.Id': 'C_id',
50+
'Variable.Units': 'u',
51+
},
52+
],
53+
},
54+
})
55+
)
56+
})
57+
58+
afterEach(() => {
59+
sinon.restore()
60+
})
61+
62+
it('renders', async () => {
63+
const el = await fixture(
64+
html`<terra-variable-combobox></terra-variable-combobox>`
65+
)
66+
expect(el).to.exist
67+
})
68+
69+
it('should sort variables alphabetically by short name then long name', async () => {
70+
const el: any = await fixture(
71+
html`<terra-variable-combobox></terra-variable-combobox>`
72+
)
73+
74+
await waitUntil(
75+
() =>
76+
(el.shadowRoot?.querySelectorAll('.listbox-option')?.length ?? 0) > 0,
77+
'variables did not load in time',
78+
{ timeout: 3000 }
79+
)
80+
81+
const options = [...el.shadowRoot.querySelectorAll('.listbox-option')]
82+
83+
expect(options.map(o => o.dataset.longName)).to.deep.equal([
84+
'A',
85+
'B',
86+
'C',
87+
'D',
88+
])
89+
})
90+
})

0 commit comments

Comments
 (0)