'use strict' import { describe, expect, test, beforeAll, beforeEach } from 'vitest' import Store, { Optgroup, Option } from './store' describe('store module', () => { describe('constructor', () => { test('constructor without data', () => { let store = new Store('single', []) expect(store).toBeInstanceOf(Store) }) test('constructor with single option', () => { const store = new Store('single', [ { text: 'test' } ]) const data = store.getData() // Make sure data has one item and that it has the correct text expect(data).toHaveLength(1) expect(data[0]).toBeInstanceOf(Option) const option = data[0] as Option expect(option.text).toBe('test') expect(option.id).toBeDefined() expect(option.value).toBeDefined() expect(option.text).toBeDefined() expect(option.html).toBeDefined() expect(option.selected).toBeDefined() expect(option.display).toBeDefined() expect(option.disabled).toBeDefined() expect(option.placeholder).toBeDefined() expect(option.class).toBeDefined() expect(option.style).toBeDefined() expect(option.data).toBeDefined() expect(option.mandatory).toBeDefined() }) test('constructor with optgroup', () => { const store = new Store('single', [ { label: 'opt group', options: [ { text: 'test' } ] } ]) const data = store.getData() expect(data).toHaveLength(1) expect(data[0]).toBeInstanceOf(Optgroup) const optGroup = data[0] as Optgroup expect(optGroup.label).toBe('opt group') expect(optGroup.options).toHaveLength(1) expect(optGroup.options?.[0].text).toBe('test') }) test('constructor with optgroup and option', () => { const store = new Store('single', [ { label: 'opt group', options: [ { text: 'opt group option' } ] }, { text: 'option' } ]) const data = store.getData() expect(data).toHaveLength(2) expect(data[0]).toBeInstanceOf(Optgroup) const optGroup = data[0] as Optgroup expect(optGroup.label).toBe('opt group') expect(optGroup.options).toHaveLength(1) expect(optGroup.options?.[0].text).toBe('opt group option') expect(data[1]).toBeInstanceOf(Option) expect((data[1] as Option).text).toBe('option') }) test('constructor with multiple selected on single select only registers first selected', () => { const store = new Store('single', [ { text: 'test1' }, { text: 'test2', value: 'test2', selected: true }, { text: 'test3', selected: true } ]) // cast to an option array here, so we don't need casts in the comparisons const data = store.getData() as Array