@operato/layout 10.27.0 → 10.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ import { expect, fixture } from '@open-wc/testing'
2
+ import { html } from 'lit'
3
+ import '../src/index.js'
4
+ import type { OxUrlTabs } from '../src/index.js'
5
+
6
+ /**
7
+ * Tabs whose choice lives in the address (ADR-0060 decision 8 addendum): what is read, what is
8
+ * written, and that the two always go together.
9
+ */
10
+
11
+ const TABS = [
12
+ { key: 'orders', label: 'orders' },
13
+ { key: 'ledger', label: 'ledger' },
14
+ { key: 'journal', label: 'journal' }
15
+ ]
16
+
17
+ const start = location.href
18
+ const at = (search: string, path = location.pathname) => history.replaceState(null, '', path + search)
19
+
20
+ describe('ox-url-tabs', () => {
21
+ afterEach(() => history.replaceState(null, '', start))
22
+
23
+ it('★ opens on the tab the address names, and on the first when it names none', async () => {
24
+ at('?tab=ledger&q=x')
25
+ const named = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
26
+ expect(named.selected).to.equal('ledger')
27
+
28
+ at('?tab=nothing-like-it')
29
+ const unknown = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
30
+ expect(unknown.selected).to.equal('orders')
31
+ })
32
+
33
+ it('★ choosing a tab writes it into the address and keeps the rest of the query', async () => {
34
+ at('?q=x')
35
+ const tabs = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
36
+ const lengthBefore = history.length
37
+ ;(tabs.shadowRoot!.querySelector('[data-key="journal"]') as HTMLButtonElement).click()
38
+
39
+ const query = new URLSearchParams(location.search)
40
+ expect(query.get('tab')).to.equal('journal')
41
+ expect(query.get('q')).to.equal('x')
42
+ expect(history.length).to.equal(lengthBefore)
43
+ })
44
+
45
+ it('the first tab is the default and is not written', async () => {
46
+ at('?tab=ledger')
47
+ const tabs = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
48
+ tabs.select('orders')
49
+ expect(new URLSearchParams(location.search).has('tab')).to.equal(false)
50
+ })
51
+
52
+ it('★ back or forward to another address moves the tab, and the screen hears it', async () => {
53
+ at('?tab=ledger')
54
+ const tabs = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
55
+ let heard = ''
56
+ tabs.addEventListener('tab-change', event => (heard = (event as CustomEvent).detail.key))
57
+
58
+ at('?tab=journal')
59
+ window.dispatchEvent(new PopStateEvent('popstate'))
60
+ expect(tabs.selected).to.equal('journal')
61
+ expect(heard).to.equal('journal')
62
+ })
63
+
64
+ it('arrow keys move between tabs', async () => {
65
+ at('')
66
+ const tabs = await fixture<OxUrlTabs>(html`<ox-url-tabs .tabs=${TABS}></ox-url-tabs>`)
67
+ const first = tabs.shadowRoot!.querySelector('[data-key="orders"]') as HTMLButtonElement
68
+ first.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft', bubbles: true, composed: true }))
69
+ expect(tabs.selected).to.equal('journal')
70
+ })
71
+
72
+ it('transitional path segment: the key after the page is read', async () => {
73
+ at('', '/domain/x/quality-list/disposition')
74
+ const tabs = await fixture<OxUrlTabs>(html`<ox-url-tabs base="quality-list" .tabs=${[
75
+ { key: 'inspection', label: 'i' },
76
+ { key: 'disposition', label: 'd' }
77
+ ]}></ox-url-tabs>`)
78
+ expect(tabs.selected).to.equal('disposition')
79
+ })
80
+ })