@m3ui-vue/m3ui-vue 1.1.2 → 1.1.4
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.
- package/README.md +1 -1
- package/dist/API.md +202 -10
- package/dist/{MIcon-DLOkQduI.js → MIcon-Bxh9j135.js} +15 -8
- package/dist/MIcon-Bxh9j135.js.map +1 -0
- package/dist/{MMenuItem-DVxKEv1W.js → MMenuItem-BfjqwcYP.js} +85 -64
- package/dist/MMenuItem-BfjqwcYP.js.map +1 -0
- package/dist/MSlider-JflHJ70q.js +1525 -0
- package/dist/MSlider-JflHJ70q.js.map +1 -0
- package/dist/{MTextField-rML9jnXs.js → MTextField-NaIB6RbM.js} +68 -66
- package/dist/MTextField-NaIB6RbM.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/code-editor.js +2 -2
- package/dist/components/MAnimatedNumber.vue.d.ts +29 -0
- package/dist/components/MButton.vue.d.ts +2 -0
- package/dist/components/MContainerTransform.vue.d.ts +59 -0
- package/dist/components/MDialog.vue.d.ts +8 -5
- package/dist/components/MFlex.vue.d.ts +1 -1
- package/dist/components/MIcon.vue.d.ts +2 -0
- package/dist/components/MIconButton.vue.d.ts +2 -0
- package/dist/components/MListGroup.vue.d.ts +23 -0
- package/dist/components/MListItem.vue.d.ts +5 -5
- package/dist/components/MNavigationRail.vue.d.ts +1 -1
- package/dist/components/MScheduler.vue.d.ts +2 -2
- package/dist/components/MScheme.vue.d.ts +60 -0
- package/dist/components/MSparkline.vue.d.ts +41 -0
- package/dist/components/MSplitter.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MToolbar.vue.d.ts +32 -5
- package/dist/components/MWindow.vue.d.ts +2 -2
- package/dist/components/_MDrawerItemList.vue.d.ts +2 -0
- package/dist/components/_MToolbarFab.vue.d.ts +16 -0
- package/dist/composables/_containerTransform.d.ts +48 -0
- package/dist/composables/_useSegmentRuns.d.ts +8 -0
- package/dist/composables/dynamicColor.d.ts +11 -0
- package/dist/composables/useColorPalette.d.ts +3 -1
- package/dist/composables/useConfetti.d.ts +32 -0
- package/dist/composables/useContainerTransform.d.ts +30 -0
- package/dist/composables/useLocale.d.ts +9 -0
- package/dist/index.d.ts +10 -1
- package/dist/locales/index.js +35 -0
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +16844 -15530
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +55 -55
- package/dist/{material-color-utilities-Cktu8DBU.js → material-color-utilities-C_iikgRm.js} +928 -603
- package/dist/material-color-utilities-C_iikgRm.js.map +1 -0
- package/dist/rich-text-editor.js +11 -11
- package/dist/styles.css +1 -1
- package/dist/terminal.js +1 -1
- package/dist/{useLocale-CqQrhWFl.js → useLocale-CXibICQA.js} +6 -1
- package/dist/useLocale-CXibICQA.js.map +1 -0
- package/package.json +2 -1
- package/src/__tests__/MAnimatedNumber.test.ts +128 -0
- package/src/__tests__/MContainerTransform.test.ts +279 -0
- package/src/__tests__/MDialog.test.ts +27 -0
- package/src/__tests__/MIcon.test.ts +19 -0
- package/src/__tests__/MListItemExpandable.test.ts +48 -0
- package/src/__tests__/MListItemShape.test.ts +42 -5
- package/src/__tests__/MListKeyboard.test.ts +90 -0
- package/src/__tests__/MMapTools.test.ts +2 -1
- package/src/__tests__/MNavigationRail.test.ts +33 -0
- package/src/__tests__/MScheme.test.ts +107 -0
- package/src/__tests__/MSlider.test.ts +161 -0
- package/src/__tests__/MSparkline.test.ts +96 -0
- package/src/__tests__/MToolbar.test.ts +157 -0
- package/src/__tests__/attrsFallthrough.test.ts +6 -0
- package/src/__tests__/hydration.test.ts +25 -0
- package/src/__tests__/iconButtonNames.test.ts +124 -0
- package/src/__tests__/ssr.test.ts +1 -0
- package/src/__tests__/useConfetti.test.ts +81 -0
- package/src/components/MAnimatedNumber.vue +327 -0
- package/src/components/MAutocomplete.vue +34 -4
- package/src/components/MBottomSheet.vue +1 -0
- package/src/components/MButton.vue +4 -2
- package/src/components/MContainerTransform.vue +321 -0
- package/src/components/MDataTable.vue +1 -1
- package/src/components/MDialog.vue +31 -6
- package/src/components/MFab.vue +10 -5
- package/src/components/MIcon.vue +11 -4
- package/src/components/MIconButton.vue +3 -1
- package/src/components/MList.vue +33 -31
- package/src/components/MListGroup.vue +66 -0
- package/src/components/MListItem.vue +138 -84
- package/src/components/MMaskField.vue +3 -0
- package/src/components/MMultiAutocomplete.vue +5 -1
- package/src/components/MMultiSelect.vue +19 -9
- package/src/components/MNavigationRail.vue +40 -10
- package/src/components/MScheme.vue +162 -0
- package/src/components/MSearchBar.vue +2 -0
- package/src/components/MSelect.vue +44 -10
- package/src/components/MSideSheet.vue +3 -0
- package/src/components/MSimpleCarousel.vue +6 -0
- package/src/components/MSlider.vue +161 -42
- package/src/components/MSparkline.vue +250 -0
- package/src/components/MSplitButton.vue +3 -0
- package/src/components/MSpotlightSearch.vue +1 -0
- package/src/components/MTable.vue +1 -0
- package/src/components/MTagInput.vue +4 -0
- package/src/components/MTextField.vue +3 -0
- package/src/components/MToolbar.vue +256 -15
- package/src/components/MTour.vue +1 -0
- package/src/components/MTreeTable.vue +1 -0
- package/src/components/_MToolbarFab.vue +14 -0
- package/src/components/_MTreeNode.vue +3 -0
- package/src/composables/_containerTransform.ts +179 -0
- package/src/composables/_useEscapeStack.ts +2 -0
- package/src/composables/_useSegmentRuns.ts +39 -0
- package/src/composables/dynamicColor.ts +66 -0
- package/src/composables/useColorPalette.ts +2 -2
- package/src/composables/useConfetti.ts +204 -0
- package/src/composables/useContainerTransform.ts +59 -0
- package/src/composables/useLocale.ts +14 -0
- package/src/index.ts +10 -1
- package/src/locales/de.ts +5 -0
- package/src/locales/es.ts +5 -0
- package/src/locales/fr.ts +5 -0
- package/src/locales/ja.ts +5 -0
- package/src/locales/ko.ts +5 -0
- package/src/locales/pt.ts +5 -0
- package/src/locales/zh.ts +5 -0
- package/dist/MIcon-DLOkQduI.js.map +0 -1
- package/dist/MMenuItem-DVxKEv1W.js.map +0 -1
- package/dist/MSlider-BXsBv0ox.js +0 -1402
- package/dist/MSlider-BXsBv0ox.js.map +0 -1
- package/dist/MTextField-rML9jnXs.js.map +0 -1
- package/dist/material-color-utilities-Cktu8DBU.js.map +0 -1
- package/dist/useLocale-CqQrhWFl.js.map +0 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from 'vitest'
|
|
2
|
+
import { mount, type VueWrapper } from '@vue/test-utils'
|
|
3
|
+
import { h, nextTick } from 'vue'
|
|
4
|
+
import MScheme from '../components/MScheme.vue'
|
|
5
|
+
import { generateScheme, seedFromPixels } from '../composables/dynamicColor'
|
|
6
|
+
import { palettes } from '../composables/useColorPalette'
|
|
7
|
+
|
|
8
|
+
const pixels = (colors: [number, number, number, number][]) => colors.flatMap(([r, g, b, n]) => Array.from({ length: n }, () => [r, g, b, 255]).flat())
|
|
9
|
+
|
|
10
|
+
describe('seedFromPixels', () => {
|
|
11
|
+
it('returns the color of a single-color image', async () => {
|
|
12
|
+
expect(await seedFromPixels(pixels([[200, 50, 40, 400]]))).toBe('#c83228')
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('picks the dominant colorful color and skips transparent pixels', async () => {
|
|
16
|
+
const rgba = [...pixels([[200, 50, 40, 700], [40, 160, 70, 300]]), ...Array.from({ length: 2000 }, (_, i) => (i % 4 === 3 ? 0 : 30))]
|
|
17
|
+
expect(await seedFromPixels(rgba)).toBe('#c83228')
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
// QuantizerWsmeans seeds its clusters with Math.random(); Android's Java version uses a fixed seed. Two reads of
|
|
21
|
+
// the same photo gave different seeds (#236493, then #497caa in Chrome).
|
|
22
|
+
it('gives the same seed every time for the same pixels', async () => {
|
|
23
|
+
let a = 7
|
|
24
|
+
const rand = () => ((a = (a * 1103515245 + 12345) % 2147483648) / 2147483648)
|
|
25
|
+
const rgba: number[] = []
|
|
26
|
+
for (let i = 0; i < 4000; i++) {
|
|
27
|
+
const base = i % 3 === 0 ? [40, 90, 150] : i % 3 === 1 ? [110, 150, 200] : [200, 120, 60]
|
|
28
|
+
rgba.push(...base.map((c) => Math.max(0, Math.min(255, Math.round(c + (rand() - 0.5) * 120)))), 255)
|
|
29
|
+
}
|
|
30
|
+
const seeds = new Set<string>()
|
|
31
|
+
for (let i = 0; i < 8; i++) seeds.add(await seedFromPixels(rgba))
|
|
32
|
+
expect(seeds.size).toBe(1)
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
describe('MScheme', () => {
|
|
37
|
+
let wrapper: VueWrapper | undefined
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
wrapper?.unmount()
|
|
40
|
+
wrapper = undefined
|
|
41
|
+
document.documentElement.classList.remove('dark')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('sets the seed\'s scheme on itself, light or dark like the app', async () => {
|
|
45
|
+
const expected = await generateScheme('#c83228', { variant: 'content' })
|
|
46
|
+
wrapper = mount(MScheme, { props: { seed: '#c83228', variant: 'content' }, slots: { default: '<p>hi</p>' } })
|
|
47
|
+
const el = wrapper.element as HTMLElement
|
|
48
|
+
await vi.waitFor(() => expect(el.style.getPropertyValue('--color-primary')).toBe(expected.light.primary))
|
|
49
|
+
expect(el.classList.contains('m3-scheme')).toBe(true)
|
|
50
|
+
|
|
51
|
+
document.documentElement.classList.add('dark')
|
|
52
|
+
await vi.waitFor(() => expect(el.style.getPropertyValue('--color-primary')).toBe(expected.dark.primary))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('tells its slot the seed and when the scheme is applied, and drops it when the seed goes', async () => {
|
|
56
|
+
const seen: { seed?: string; ready: boolean }[] = []
|
|
57
|
+
wrapper = mount(MScheme, {
|
|
58
|
+
props: { seed: '#3b4ca0' },
|
|
59
|
+
slots: { default: (scope: { seed?: string; ready: boolean }) => (seen.push({ ...scope }), h('span')) },
|
|
60
|
+
})
|
|
61
|
+
expect(seen[0]).toEqual({ seed: '#3b4ca0', ready: false })
|
|
62
|
+
await vi.waitFor(() => expect(seen.at(-1)).toEqual({ seed: '#3b4ca0', ready: true }))
|
|
63
|
+
|
|
64
|
+
await wrapper.setProps({ seed: undefined })
|
|
65
|
+
await nextTick()
|
|
66
|
+
expect((wrapper.element as HTMLElement).style.getPropertyValue('--color-primary')).toBe('')
|
|
67
|
+
expect(seen.at(-1)).toEqual({ seed: undefined, ready: false })
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('applies a palette with CSS through its attribute, dark included', async () => {
|
|
71
|
+
const css = document.createElement('style')
|
|
72
|
+
css.textContent = "[data-palette='teal'] { --color-primary: #00696b; }"
|
|
73
|
+
document.head.appendChild(css)
|
|
74
|
+
try {
|
|
75
|
+
wrapper = mount(MScheme, { props: { palette: 'teal' } })
|
|
76
|
+
const el = wrapper.element as HTMLElement
|
|
77
|
+
expect(el.getAttribute('data-palette')).toBe('teal')
|
|
78
|
+
expect(el.style.getPropertyValue('--color-primary')).toBe('')
|
|
79
|
+
document.documentElement.classList.add('dark')
|
|
80
|
+
await vi.waitFor(() => expect(el.classList.contains('dark')).toBe(true))
|
|
81
|
+
await wrapper.setProps({ seed: '#c83228' })
|
|
82
|
+
expect(el.hasAttribute('data-palette')).toBe(false)
|
|
83
|
+
} finally {
|
|
84
|
+
css.remove()
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('builds purple (the :root baseline, no [data-palette] block) from its seed', async () => {
|
|
89
|
+
const expected = await generateScheme('#6750A4')
|
|
90
|
+
wrapper = mount(MScheme, { props: { palette: 'purple' } })
|
|
91
|
+
const el = wrapper.element as HTMLElement
|
|
92
|
+
expect(el.hasAttribute('data-palette')).toBe(false)
|
|
93
|
+
await vi.waitFor(() => expect(el.style.getPropertyValue('--color-primary')).toBe(expected.light.primary))
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('takes custom palettes registered with createM3UI: generated from their seed when they have no CSS', async () => {
|
|
97
|
+
palettes.push({ id: 'brand', label: 'Brand', seed: '#ff5722' })
|
|
98
|
+
try {
|
|
99
|
+
const expected = await generateScheme('#ff5722')
|
|
100
|
+
wrapper = mount(MScheme, { props: { palette: 'brand' } })
|
|
101
|
+
const el = wrapper.element as HTMLElement
|
|
102
|
+
await vi.waitFor(() => expect(el.style.getPropertyValue('--color-primary')).toBe(expected.light.primary))
|
|
103
|
+
} finally {
|
|
104
|
+
palettes.splice(palettes.findIndex((p) => p.id === 'brand'), 1)
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
import { mount, type VueWrapper } from '@vue/test-utils'
|
|
3
|
+
import { nextTick } from 'vue'
|
|
4
|
+
import MSlider from '../components/MSlider.vue'
|
|
5
|
+
|
|
6
|
+
let wrapper: VueWrapper | undefined
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
wrapper?.unmount()
|
|
9
|
+
wrapper = undefined
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
// happy-dom has no layout: the slider's track is 880px long (the docs' Volume slider in the bug report)
|
|
13
|
+
const offsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth')!
|
|
14
|
+
beforeAll(() => {
|
|
15
|
+
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
|
|
16
|
+
configurable: true,
|
|
17
|
+
get(this: HTMLElement) { return this.classList.contains('touch-none') ? 880 : 0 },
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
afterAll(() => Object.defineProperty(HTMLElement.prototype, 'offsetWidth', offsetWidth))
|
|
21
|
+
|
|
22
|
+
// The track's pieces (inactive/active) and its end dots, in template order
|
|
23
|
+
const track = (w: VueWrapper) => w.find('.touch-none')
|
|
24
|
+
const bar = (w: VueWrapper) => track(w).element.firstElementChild as HTMLElement
|
|
25
|
+
const pieces = (w: VueWrapper) => [...bar(w).children].filter((c) => !c.classList.contains('rounded-full')) as HTMLElement[]
|
|
26
|
+
const endDot = (w: VueWrapper) => [...bar(w).children].filter((c) => c.classList.contains('rounded-full')).at(-1) as HTMLElement
|
|
27
|
+
const startDot = (w: VueWrapper) => [...bar(w).children].filter((c) => c.classList.contains('rounded-full'))[0] as HTMLElement
|
|
28
|
+
|
|
29
|
+
describe('MSlider', () => {
|
|
30
|
+
it('shows the end dot while there is room for it in the inactive piece', async () => {
|
|
31
|
+
wrapper = mount(MSlider, { props: { modelValue: 90 } })
|
|
32
|
+
await nextTick()
|
|
33
|
+
expect(endDot(wrapper).style.opacity).toBe('1')
|
|
34
|
+
expect(pieces(wrapper)[1]!.style.width).not.toBe('0px')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('hides the end dot once it no longer fits in the inactive piece, instead of leaving it in the thumb gap', async () => {
|
|
38
|
+
// 98 of 880px: an 11.6px piece, shorter than the dot's 12px inset + its radius
|
|
39
|
+
wrapper = mount(MSlider, { props: { modelValue: 98 } })
|
|
40
|
+
await nextTick()
|
|
41
|
+
expect(endDot(wrapper).style.opacity).toBe('0')
|
|
42
|
+
// The piece itself is still longer than its corner, so it stays (Compose draws it too)
|
|
43
|
+
expect(pieces(wrapper)[1]!.style.width).not.toBe('0px')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('drops the inactive piece once it is shorter than its corner, like Compose', async () => {
|
|
47
|
+
// 99: a 3.2px piece
|
|
48
|
+
wrapper = mount(MSlider, { props: { modelValue: 99 } })
|
|
49
|
+
await nextTick()
|
|
50
|
+
expect(pieces(wrapper)[1]!.style.width).toBe('0px')
|
|
51
|
+
expect(endDot(wrapper).style.opacity).toBe('0')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('applies the same to both ends of a range slider', async () => {
|
|
55
|
+
wrapper = mount(MSlider, { props: { variant: 'range', modelValue: [1, 99] } })
|
|
56
|
+
await nextTick()
|
|
57
|
+
const [before, , after] = pieces(wrapper)
|
|
58
|
+
expect(before!.style.width).toBe('0px')
|
|
59
|
+
expect(after!.style.width).toBe('0px')
|
|
60
|
+
expect(startDot(wrapper).style.opacity).toBe('0')
|
|
61
|
+
expect(endDot(wrapper).style.opacity).toBe('0')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('keeps both dots of a range slider away from the ends', async () => {
|
|
65
|
+
wrapper = mount(MSlider, { props: { variant: 'range', modelValue: [20, 80] } })
|
|
66
|
+
await nextTick()
|
|
67
|
+
expect(startDot(wrapper).style.opacity).toBe('1')
|
|
68
|
+
expect(endDot(wrapper).style.opacity).toBe('1')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe('range', () => {
|
|
72
|
+
// The track is 100px long from 0 for the pointer
|
|
73
|
+
const press = async (w: VueWrapper, at: number, moves: number[] = []) => {
|
|
74
|
+
const el = track(w).element as HTMLElement
|
|
75
|
+
el.getBoundingClientRect = () => ({ left: 0, top: 0, width: 100, height: 46, right: 100, bottom: 46, x: 0, y: 0, toJSON() {} })
|
|
76
|
+
el.dispatchEvent(new PointerEvent('pointerdown', { clientX: at, bubbles: true }))
|
|
77
|
+
await nextTick()
|
|
78
|
+
for (const x of moves) {
|
|
79
|
+
window.dispatchEvent(new PointerEvent('pointermove', { clientX: x }))
|
|
80
|
+
await nextTick()
|
|
81
|
+
}
|
|
82
|
+
window.dispatchEvent(new PointerEvent('pointerup', { clientX: moves.at(-1) ?? at }))
|
|
83
|
+
}
|
|
84
|
+
// A v-model the component updates, as a parent would
|
|
85
|
+
const mountRange = (value: [number, number]) => {
|
|
86
|
+
const w = mount(MSlider, {
|
|
87
|
+
props: { variant: 'range', modelValue: value, 'onUpdate:modelValue': (v: unknown) => w.setProps({ modelValue: v as [number, number] }) },
|
|
88
|
+
attachTo: document.body,
|
|
89
|
+
})
|
|
90
|
+
return w
|
|
91
|
+
}
|
|
92
|
+
const last = (w: VueWrapper) => (w.emitted('update:modelValue') as unknown[][]).at(-1)![0]
|
|
93
|
+
|
|
94
|
+
it('moves the end thumb for a press past two overlapping thumbs, keeping the pair ordered', async () => {
|
|
95
|
+
wrapper = mountRange([50, 50])
|
|
96
|
+
await press(wrapper, 70)
|
|
97
|
+
expect(last(wrapper)).toEqual([50, 70])
|
|
98
|
+
await press(wrapper, 30)
|
|
99
|
+
expect(last(wrapper)).toEqual([30, 70])
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('picks the thumb by the drag direction on two overlapping thumbs', async () => {
|
|
103
|
+
wrapper = mountRange([50, 50])
|
|
104
|
+
await press(wrapper, 50, [40, 30])
|
|
105
|
+
expect(last(wrapper)).toEqual([30, 50])
|
|
106
|
+
await wrapper.setProps({ modelValue: [50, 50] })
|
|
107
|
+
await press(wrapper, 50, [60, 75])
|
|
108
|
+
expect(last(wrapper)).toEqual([50, 75])
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('never lets a dragged thumb pass the other one', async () => {
|
|
112
|
+
wrapper = mountRange([20, 60])
|
|
113
|
+
await press(wrapper, 22, [40, 90])
|
|
114
|
+
expect(last(wrapper)).toEqual([60, 60])
|
|
115
|
+
await wrapper.setProps({ modelValue: [20, 60] })
|
|
116
|
+
await press(wrapper, 58, [10])
|
|
117
|
+
expect(last(wrapper)).toEqual([20, 20])
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('makes each thumb a slider of its own, moved by its keys within the other', async () => {
|
|
121
|
+
wrapper = mountRange([20, 60])
|
|
122
|
+
const thumbs = wrapper.findAll('[role=slider]')
|
|
123
|
+
expect(thumbs).toHaveLength(2)
|
|
124
|
+
expect(thumbs.map((t) => t.attributes('aria-label'))).toEqual(['Range start', 'Range end'])
|
|
125
|
+
expect(thumbs[0]!.attributes('aria-valuemax')).toBe('60')
|
|
126
|
+
expect(thumbs[1]!.attributes('aria-valuemin')).toBe('20')
|
|
127
|
+
await thumbs[1]!.trigger('keydown', { key: 'ArrowRight' })
|
|
128
|
+
expect(last(wrapper)).toEqual([20, 61])
|
|
129
|
+
await thumbs[1]!.trigger('keydown', { key: 'Home' })
|
|
130
|
+
expect(last(wrapper)).toEqual([20, 20])
|
|
131
|
+
await thumbs[0]!.trigger('keydown', { key: 'ArrowRight' })
|
|
132
|
+
expect(last(wrapper)).toEqual([20, 20])
|
|
133
|
+
await thumbs[0]!.trigger('keydown', { key: 'Home' })
|
|
134
|
+
expect(last(wrapper)).toEqual([0, 20])
|
|
135
|
+
expect(wrapper.find('[role=group]').exists()).toBe(true)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('rings a thumb focused from the keyboard, not one a press focuses', async () => {
|
|
139
|
+
wrapper = mountRange([20, 60])
|
|
140
|
+
await press(wrapper, 70)
|
|
141
|
+
await nextTick()
|
|
142
|
+
const thumbs = wrapper.findAll('[role=slider]')
|
|
143
|
+
expect(document.activeElement).toBe(thumbs[1]!.element)
|
|
144
|
+
expect(thumbs[1]!.classes()).not.toContain('ring-2')
|
|
145
|
+
// The keys after a press bring the ring back, as browsers do
|
|
146
|
+
await thumbs[1]!.trigger('keydown', { key: 'ArrowLeft' })
|
|
147
|
+
expect(thumbs[1]!.classes()).toContain('ring-2')
|
|
148
|
+
// Tabbing to the other thumb
|
|
149
|
+
;(thumbs[0]!.element as HTMLElement).focus()
|
|
150
|
+
await nextTick()
|
|
151
|
+
expect(thumbs[0]!.classes()).toContain('ring-2')
|
|
152
|
+
expect(thumbs[1]!.classes()).not.toContain('ring-2')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('rings a thumb in the slider\'s own color', () => {
|
|
156
|
+
wrapper = mount(MSlider, { props: { variant: 'range', modelValue: [20, 60], color: 'tertiary' } })
|
|
157
|
+
const ring = (wrapper.find('[role=slider]').element as HTMLElement).style.getPropertyValue('--tw-ring-color')
|
|
158
|
+
expect(ring).toBe('var(--color-tertiary)')
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
import { mount, type VueWrapper } from '@vue/test-utils'
|
|
3
|
+
import MSparkline from '../components/MSparkline.vue'
|
|
4
|
+
|
|
5
|
+
// happy-dom has no layout: give every element a 100×30 box
|
|
6
|
+
const size = { clientWidth: 100, clientHeight: 30 }
|
|
7
|
+
const saved: Record<string, PropertyDescriptor | undefined> = {}
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
for (const [key, value] of Object.entries(size)) {
|
|
10
|
+
saved[key] = Object.getOwnPropertyDescriptor(HTMLElement.prototype, key)
|
|
11
|
+
Object.defineProperty(HTMLElement.prototype, key, { configurable: true, get: () => value })
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
afterAll(() => {
|
|
15
|
+
for (const key of Object.keys(size)) if (saved[key]) Object.defineProperty(HTMLElement.prototype, key, saved[key]!)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
let wrapper: VueWrapper | undefined
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
wrapper?.unmount()
|
|
21
|
+
wrapper = undefined
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const numbers = (d: string) => (d.match(/-?\d+(\.\d+)?/g) ?? []).map(Number)
|
|
25
|
+
const settle = () => new Promise((r) => setTimeout(r, 900))
|
|
26
|
+
|
|
27
|
+
describe('MSparkline', () => {
|
|
28
|
+
it('is an image named by a summary of the data, in the app\'s language', () => {
|
|
29
|
+
wrapper = mount(MSparkline, { props: { data: [12, 9, 21, 18] } })
|
|
30
|
+
expect(wrapper.attributes('role')).toBe('img')
|
|
31
|
+
expect(wrapper.attributes('aria-label')).toBe('From 12 to 18, low 9, high 21')
|
|
32
|
+
expect(wrapper.find('svg').attributes('aria-hidden')).toBe('true')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('takes a label instead of the summary', () => {
|
|
36
|
+
wrapper = mount(MSparkline, { props: { data: [1, 2], label: 'Visits this week' } })
|
|
37
|
+
expect(wrapper.attributes('aria-label')).toBe('Visits this week')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('draws the line through every point, at the box\'s real size', async () => {
|
|
41
|
+
wrapper = mount(MSparkline, { props: { data: [1, 3, 2, 5], smooth: false, showLast: false }, attachTo: document.body })
|
|
42
|
+
await wrapper.vm.$nextTick()
|
|
43
|
+
expect(wrapper.find('svg').attributes('viewBox')).toBe('0 0 100 30')
|
|
44
|
+
const d = wrapper.find('path').attributes('d')!
|
|
45
|
+
expect(d.match(/[ML]/g)).toHaveLength(4)
|
|
46
|
+
const ys = numbers(d).filter((_, i) => i % 2 === 1)
|
|
47
|
+
// Highest value on top (smallest y), lowest at the bottom
|
|
48
|
+
expect(Math.min(...ys)).toBe(ys[3])
|
|
49
|
+
expect(Math.max(...ys)).toBe(ys[0])
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('smooths without overshooting the points (monotone)', async () => {
|
|
53
|
+
wrapper = mount(MSparkline, { props: { data: [0, 10, 10, 0], showLast: false }, attachTo: document.body })
|
|
54
|
+
await wrapper.vm.$nextTick()
|
|
55
|
+
const ys = numbers(wrapper.find('path').attributes('d')!).filter((_, i) => i % 2 === 1)
|
|
56
|
+
const top = Math.min(...ys)
|
|
57
|
+
const bottom = Math.max(...ys)
|
|
58
|
+
// Every control point stays between the highest and lowest points
|
|
59
|
+
expect(ys.every((v) => v >= top - 0.01 && v <= bottom + 0.01)).toBe(true)
|
|
60
|
+
expect(top).toBeCloseTo(2, 0)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('draws bars from zero, a dashed baseline, and an area fill', async () => {
|
|
64
|
+
wrapper = mount(MSparkline, { props: { data: [3, -2, 5], type: 'bar', baseline: 4 }, attachTo: document.body })
|
|
65
|
+
await wrapper.vm.$nextTick()
|
|
66
|
+
const rects = wrapper.findAll('rect')
|
|
67
|
+
expect(rects).toHaveLength(3)
|
|
68
|
+
expect(rects.every((r) => Number(r.attributes('height')) > 0)).toBe(true)
|
|
69
|
+
expect(wrapper.find('line').attributes('stroke-dasharray')).toBe('3 3')
|
|
70
|
+
await wrapper.setProps({ type: 'area', baseline: undefined })
|
|
71
|
+
expect(wrapper.find('linearGradient').exists()).toBe(true)
|
|
72
|
+
expect(wrapper.findAll('path')).toHaveLength(2)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('springs to new data and settles exactly where it would have jumped', async () => {
|
|
76
|
+
const jumped = mount(MSparkline, { props: { data: [5, 1, 4], animate: false }, attachTo: document.body })
|
|
77
|
+
await jumped.vm.$nextTick()
|
|
78
|
+
const target = jumped.find('path').attributes('d')
|
|
79
|
+
jumped.unmount()
|
|
80
|
+
wrapper = mount(MSparkline, { props: { data: [1, 4, 2] }, attachTo: document.body })
|
|
81
|
+
await wrapper.setProps({ data: [5, 1, 4] })
|
|
82
|
+
await new Promise((r) => setTimeout(r, 60))
|
|
83
|
+
expect(wrapper.find('path').attributes('d')).not.toBe(target)
|
|
84
|
+
await settle()
|
|
85
|
+
expect(wrapper.find('path').attributes('d')).toBe(target)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('follows the pointer with interactive', async () => {
|
|
89
|
+
wrapper = mount(MSparkline, { props: { data: [1, 2, 3, 4, 5], interactive: true }, attachTo: document.body })
|
|
90
|
+
;(wrapper.element as HTMLElement).getBoundingClientRect = () => ({ left: 0, top: 0, width: 100, height: 30 }) as DOMRect
|
|
91
|
+
await wrapper.trigger('pointermove', { clientX: 99 })
|
|
92
|
+
await wrapper.trigger('pointermove', { clientX: 1 })
|
|
93
|
+
await wrapper.trigger('pointerleave')
|
|
94
|
+
expect(wrapper.emitted('hover')).toEqual([[4], [0], [null]])
|
|
95
|
+
})
|
|
96
|
+
})
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from 'vitest'
|
|
2
|
+
import { mount, type VueWrapper } from '@vue/test-utils'
|
|
3
|
+
import { h, nextTick } from 'vue'
|
|
4
|
+
import MToolbar from '../components/MToolbar.vue'
|
|
5
|
+
import MFab from '../components/MFab.vue'
|
|
6
|
+
|
|
7
|
+
let wrapper: VueWrapper | undefined
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
wrapper?.unmount()
|
|
10
|
+
wrapper = undefined
|
|
11
|
+
vi.restoreAllMocks()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
// happy-dom has no layout: give the unclipped content natural widths
|
|
15
|
+
function sizes(w: VueWrapper, widths: Record<string, number>) {
|
|
16
|
+
for (const [selector, width] of Object.entries(widths)) {
|
|
17
|
+
const el = w.find(selector).element
|
|
18
|
+
Object.defineProperty(el, 'offsetWidth', { configurable: true, get: () => width })
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const reduceMotion = () =>
|
|
22
|
+
vi.spyOn(window, 'matchMedia').mockImplementation((q: string) => ({ matches: q.includes('reduce'), media: q, addEventListener() {}, removeEventListener() {} }) as unknown as MediaQueryList)
|
|
23
|
+
const button = (text: string) => () => h('button', text)
|
|
24
|
+
|
|
25
|
+
describe('MToolbar', () => {
|
|
26
|
+
it('collapses a floating toolbar without a FAB to its center, closing #leading and #trailing', async () => {
|
|
27
|
+
reduceMotion()
|
|
28
|
+
wrapper = mount(MToolbar, {
|
|
29
|
+
props: { variant: 'floating' },
|
|
30
|
+
slots: { leading: button('back'), default: button('add'), trailing: button('more') },
|
|
31
|
+
})
|
|
32
|
+
const [lead, , trail] = wrapper.findAll('.overflow-hidden, .gap-1.flex-row:not(.shrink-0)').map((x) => x.element as HTMLElement)
|
|
33
|
+
expect(wrapper.text()).toBe('backaddmore')
|
|
34
|
+
expect(lead!.style.width).toBe('')
|
|
35
|
+
await wrapper.setProps({ expanded: false })
|
|
36
|
+
expect(lead!.style.width).toBe('0px')
|
|
37
|
+
expect(trail!.style.width).toBe('0px')
|
|
38
|
+
expect(lead!.hasAttribute('inert')).toBe(true)
|
|
39
|
+
expect(trail!.hasAttribute('inert')).toBe(true)
|
|
40
|
+
// The center stays reachable
|
|
41
|
+
expect(wrapper.findAll('button').filter((b) => !b.element.closest('[inert]')).map((b) => b.text())).toEqual(['add'])
|
|
42
|
+
await wrapper.setProps({ expanded: true })
|
|
43
|
+
expect(lead!.style.width).toBe('')
|
|
44
|
+
expect(lead!.hasAttribute('inert')).toBe(false)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('collapses into its FAB, keeping its footprint, while the FAB grows from 56 to 80px', async () => {
|
|
48
|
+
reduceMotion()
|
|
49
|
+
wrapper = mount(MToolbar, {
|
|
50
|
+
props: { variant: 'floating', fabPosition: 'end' },
|
|
51
|
+
slots: { default: button('add'), fab: () => h(MFab, { icon: 'edit', label: 'Edit' }) },
|
|
52
|
+
})
|
|
53
|
+
sizes(wrapper, { '.w-max': 200 })
|
|
54
|
+
const fabBox = wrapper.find('.absolute').element as HTMLElement
|
|
55
|
+
const body = wrapper.find('.rounded-full.overflow-hidden').element as HTMLElement
|
|
56
|
+
const region = body.parentElement!
|
|
57
|
+
expect(fabBox.style.width).toBe('56px')
|
|
58
|
+
await wrapper.setProps({ expanded: false })
|
|
59
|
+
expect(body.style.width).toBe('0px')
|
|
60
|
+
expect(region.style.width).toBe('200px')
|
|
61
|
+
expect(fabBox.style.width).toBe('80px')
|
|
62
|
+
expect(fabBox.style.height).toBe('80px')
|
|
63
|
+
expect(body.hasAttribute('inert')).toBe(true)
|
|
64
|
+
// The toolbar sizes the FAB, square even with a label (which names it), in the standard toolbar FAB's color
|
|
65
|
+
const fab = wrapper.findComponent(MFab).find('button')
|
|
66
|
+
expect(fab.classes()).toContain('w-full')
|
|
67
|
+
expect(fab.attributes('aria-label')).toBe('Edit')
|
|
68
|
+
expect(fab.text()).toBe('edit')
|
|
69
|
+
expect(fab.classes()).toContain('bg-primary-container')
|
|
70
|
+
await wrapper.setProps({ expanded: true })
|
|
71
|
+
expect(body.style.width).toBe('')
|
|
72
|
+
expect(region.style.width).toBe('')
|
|
73
|
+
expect(fabBox.style.width).toBe('56px')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('gives the FAB of a colored toolbar the vibrant FAB color', () => {
|
|
77
|
+
wrapper = mount(MToolbar, {
|
|
78
|
+
props: { variant: 'floating', fabPosition: 'end', color: 'primary' },
|
|
79
|
+
slots: { default: button('add'), fab: () => h(MFab, { icon: 'edit', label: 'Edit' }) },
|
|
80
|
+
})
|
|
81
|
+
expect(wrapper.findComponent(MFab).find('button').classes()).toContain('bg-tertiary-container')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('springs closed and settles', async () => {
|
|
85
|
+
wrapper = mount(MToolbar, {
|
|
86
|
+
props: { variant: 'floating' },
|
|
87
|
+
slots: { leading: button('back'), default: button('add') },
|
|
88
|
+
})
|
|
89
|
+
sizes(wrapper, { '.pe-1': 96 })
|
|
90
|
+
const lead = wrapper.find('.overflow-hidden').element as HTMLElement
|
|
91
|
+
await wrapper.setProps({ expanded: false })
|
|
92
|
+
await new Promise((r) => setTimeout(r, 120))
|
|
93
|
+
const mid = parseFloat(lead.style.width)
|
|
94
|
+
expect(mid).toBeGreaterThan(-1)
|
|
95
|
+
expect(mid).toBeLessThan(96)
|
|
96
|
+
await new Promise((r) => setTimeout(r, 900))
|
|
97
|
+
expect(lead.style.width).toBe('0px')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('starts collapsed when rendered with expanded false', () => {
|
|
101
|
+
wrapper = mount(MToolbar, {
|
|
102
|
+
props: { variant: 'floating', expanded: false },
|
|
103
|
+
slots: { leading: button('back'), default: button('add') },
|
|
104
|
+
})
|
|
105
|
+
expect((wrapper.find('.overflow-hidden').element as HTMLElement).style.width).toBe('0px')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('collapses on scroll down and expands on scroll up, 40px each way, when asked', async () => {
|
|
109
|
+
reduceMotion()
|
|
110
|
+
const list = document.createElement('div')
|
|
111
|
+
document.body.appendChild(list)
|
|
112
|
+
wrapper = mount(MToolbar, {
|
|
113
|
+
props: { variant: 'floating', collapseOnScroll: list, 'onUpdate:expanded': (v: boolean) => wrapper!.setProps({ expanded: v }) },
|
|
114
|
+
slots: { leading: button('back'), default: button('add') },
|
|
115
|
+
})
|
|
116
|
+
const scrollTo = async (top: number) => {
|
|
117
|
+
list.scrollTop = top
|
|
118
|
+
list.dispatchEvent(new Event('scroll'))
|
|
119
|
+
await nextTick()
|
|
120
|
+
}
|
|
121
|
+
await scrollTo(30)
|
|
122
|
+
expect(wrapper.props('expanded')).toBe(true)
|
|
123
|
+
await scrollTo(50)
|
|
124
|
+
expect(wrapper.props('expanded')).toBe(false)
|
|
125
|
+
// Back up less than 40px: stays collapsed
|
|
126
|
+
await scrollTo(20)
|
|
127
|
+
expect(wrapper.props('expanded')).toBe(false)
|
|
128
|
+
await scrollTo(5)
|
|
129
|
+
expect(wrapper.props('expanded')).toBe(true)
|
|
130
|
+
expect(wrapper.emitted('update:expanded')).toEqual([[false], [true]])
|
|
131
|
+
list.remove()
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('does not collapse on scroll unless asked', async () => {
|
|
135
|
+
wrapper = mount(MToolbar, { props: { variant: 'floating' }, slots: { leading: button('back'), default: button('add') } })
|
|
136
|
+
Object.defineProperty(window, 'scrollY', { configurable: true, value: 300 })
|
|
137
|
+
window.dispatchEvent(new Event('scroll'))
|
|
138
|
+
await nextTick()
|
|
139
|
+
expect(wrapper.emitted('update:expanded')).toBeUndefined()
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('never collapses a docked toolbar', async () => {
|
|
143
|
+
reduceMotion()
|
|
144
|
+
wrapper = mount(MToolbar, {
|
|
145
|
+
props: { variant: 'docked', expanded: false },
|
|
146
|
+
slots: { leading: button('back'), default: button('add'), trailing: button('more') },
|
|
147
|
+
})
|
|
148
|
+
const lead = wrapper.find('.overflow-hidden').element as HTMLElement
|
|
149
|
+
expect(lead.style.width).toBe('')
|
|
150
|
+
expect(lead.hasAttribute('inert')).toBe(false)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('renders the items directly in the body without #leading or #trailing, as before', () => {
|
|
154
|
+
wrapper = mount(MToolbar, { slots: { default: () => [h('button', 'a'), h('button', 'b')] } })
|
|
155
|
+
expect(wrapper.find('.h-16').element.children).toHaveLength(2)
|
|
156
|
+
})
|
|
157
|
+
})
|
|
@@ -15,6 +15,8 @@ import MSkeleton from '../components/MSkeleton.vue'
|
|
|
15
15
|
import MStepper from '../components/MStepper.vue'
|
|
16
16
|
import MTabs from '../components/MTabs.vue'
|
|
17
17
|
import MTransferList from '../components/MTransferList.vue'
|
|
18
|
+
import MContainerTransform from '../components/MContainerTransform.vue'
|
|
19
|
+
import MDialog from '../components/MDialog.vue'
|
|
18
20
|
|
|
19
21
|
// These components render more than one root node (an element plus a Teleport, a v-if/v-show
|
|
20
22
|
// pair, a comment before the root...), which makes Vue drop a consumer's class/attrs silently.
|
|
@@ -41,6 +43,10 @@ const cases: [name: string, component: Component, props: Record<string, unknown>
|
|
|
41
43
|
['MTabs', MTabs, { modelValue: 'a', tabs: [{ value: 'a', label: 'A' }] }, 'DIV'],
|
|
42
44
|
['MTabs (secondary)', MTabs, { modelValue: 'a', tabs: [{ value: 'a', label: 'A' }], variant: 'secondary' }, 'DIV'],
|
|
43
45
|
['MTransferList', MTransferList, { modelValue: [], items: [] }, 'DIV'],
|
|
46
|
+
['MContainerTransform (the expanded view)', MContainerTransform, { open: true }, 'DIV'],
|
|
47
|
+
// A Teleport root: the attrs go on the dialog's surface
|
|
48
|
+
['MDialog', MDialog, { modelValue: true, title: 'T' }, 'DIV'],
|
|
49
|
+
['MDialog (fullscreen)', MDialog, { modelValue: true, title: 'T', fullscreen: true }, 'DIV'],
|
|
44
50
|
]
|
|
45
51
|
|
|
46
52
|
describe('consumer attrs reach multi-root components', () => {
|
|
@@ -117,3 +117,28 @@ describe('hydrating server-rendered adaptive layouts', () => {
|
|
|
117
117
|
expect(first).toBe(1024)
|
|
118
118
|
})
|
|
119
119
|
})
|
|
120
|
+
|
|
121
|
+
describe('hydrating MContainerTransform', () => {
|
|
122
|
+
it('renders the element on the server and the expanded view only in the browser, open from the start', async () => {
|
|
123
|
+
vi.resetModules()
|
|
124
|
+
const MContainerTransform = (await import('../components/MContainerTransform.vue')).default
|
|
125
|
+
const slots = { default: () => h('button', { class: 'card' }, 'Card'), expanded: () => h('p', 'Detail') }
|
|
126
|
+
const render = () => h(MContainerTransform, { open: true }, slots)
|
|
127
|
+
const ctx: { teleports?: Record<string, string> } = {}
|
|
128
|
+
const html = await renderToString(createSSRApp({ render }), ctx)
|
|
129
|
+
expect(html).toContain('Card')
|
|
130
|
+
expect(html + JSON.stringify(ctx.teleports)).not.toContain('Detail')
|
|
131
|
+
|
|
132
|
+
// As Nuxt does: the page, then what was teleported to <body> at its end
|
|
133
|
+
const container = document.createElement('div')
|
|
134
|
+
container.innerHTML = html
|
|
135
|
+
document.body.appendChild(container)
|
|
136
|
+
document.body.insertAdjacentHTML('beforeend', ctx.teleports?.body ?? '')
|
|
137
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
138
|
+
createSSRApp({ render }).mount(container)
|
|
139
|
+
await nextTick()
|
|
140
|
+
await nextTick()
|
|
141
|
+
expect(warn.mock.calls.map((c) => String(c[0])).filter((m) => m.includes('Hydration'))).toEqual([])
|
|
142
|
+
expect(document.body.querySelector('[role=dialog]')?.textContent).toBe('Detail')
|
|
143
|
+
})
|
|
144
|
+
})
|