|
| 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