@m3ui-vue/m3ui-vue 1.0.2 → 1.1.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.
Files changed (157) hide show
  1. package/dist/API.md +666 -12
  2. package/dist/MMenuItem-BOkcVtEe.js +809 -0
  3. package/dist/MMenuItem-BOkcVtEe.js.map +1 -0
  4. package/dist/MSlider-BXsBv0ox.js +1402 -0
  5. package/dist/MSlider-BXsBv0ox.js.map +1 -0
  6. package/dist/MTextField-rML9jnXs.js +671 -0
  7. package/dist/MTextField-rML9jnXs.js.map +1 -0
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +1 -1
  10. package/dist/components/MBottomSheet.vue.d.ts +20 -1
  11. package/dist/components/MDialog.vue.d.ts +17 -8
  12. package/dist/components/MDragDropList.vue.d.ts +1 -1
  13. package/dist/components/MIconButton.vue.d.ts +1 -1
  14. package/dist/components/MMap.vue.d.ts +74 -0
  15. package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
  16. package/dist/components/MMapControl.vue.d.ts +40 -0
  17. package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
  18. package/dist/components/MMapFilter.vue.d.ts +19 -0
  19. package/dist/components/MMapGeoJson.vue.d.ts +34 -0
  20. package/dist/components/MMapHover.vue.d.ts +17 -0
  21. package/dist/components/MMapIdentify.vue.d.ts +37 -0
  22. package/dist/components/MMapLayers.vue.d.ts +31 -0
  23. package/dist/components/MMapMarker.vue.d.ts +45 -0
  24. package/dist/components/MMapMeasure.vue.d.ts +43 -0
  25. package/dist/components/MMapPopup.vue.d.ts +45 -0
  26. package/dist/components/MMapSearch.vue.d.ts +28 -0
  27. package/dist/components/MMenuItem.vue.d.ts +2 -2
  28. package/dist/components/MSeekBar.vue.d.ts +122 -0
  29. package/dist/components/MSplitButton.vue.d.ts +2 -2
  30. package/dist/components/_MMapAttribution.vue.d.ts +7 -0
  31. package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
  32. package/dist/composables/_mapGeometry.d.ts +13 -0
  33. package/dist/composables/_mapPin.d.ts +5 -0
  34. package/dist/composables/_popupPosition.d.ts +12 -0
  35. package/dist/composables/_ssrState.d.ts +30 -0
  36. package/dist/composables/_storageKeys.d.ts +3 -0
  37. package/dist/composables/_useCalendarMonth.d.ts +24 -0
  38. package/dist/composables/_useEscapeStack.d.ts +3 -0
  39. package/dist/composables/_useScrollLock.d.ts +8 -0
  40. package/dist/composables/_useSheetMotion.d.ts +16 -1
  41. package/dist/composables/_useThemeTokens.d.ts +7 -0
  42. package/dist/composables/mapAdapters.d.ts +111 -0
  43. package/dist/composables/mapSearch.d.ts +31 -0
  44. package/dist/composables/themeInitScript.d.ts +4 -3
  45. package/dist/composables/useColorPalette.d.ts +173 -7
  46. package/dist/composables/useLocale.d.ts +38 -0
  47. package/dist/composables/useMap.d.ts +47 -0
  48. package/dist/composables/useMapFilter.d.ts +10 -0
  49. package/dist/composables/useMapLayers.d.ts +30 -0
  50. package/dist/composables/useTheme.d.ts +2 -1
  51. package/dist/index.d.ts +4 -1
  52. package/dist/locales/index.js +273 -7
  53. package/dist/locales/index.js.map +1 -1
  54. package/dist/m3ui-vue.css +1 -1
  55. package/dist/m3ui.js +7913 -8625
  56. package/dist/m3ui.js.map +1 -1
  57. package/dist/map.d.ts +24 -0
  58. package/dist/map.js +2067 -0
  59. package/dist/map.js.map +1 -0
  60. package/dist/plugin.d.ts +18 -2
  61. package/dist/rich-text-editor.js +26 -25
  62. package/dist/rich-text-editor.js.map +1 -1
  63. package/dist/styles.css +1 -1
  64. package/dist/{useLocale-CuvWRZ1h.js → useLocale-CqQrhWFl.js} +40 -2
  65. package/dist/useLocale-CqQrhWFl.js.map +1 -0
  66. package/package.json +11 -2
  67. package/src/__tests__/MDialog.test.ts +30 -0
  68. package/src/__tests__/MFab.test.ts +52 -6
  69. package/src/__tests__/MMap.test.ts +422 -0
  70. package/src/__tests__/MMapLayers.test.ts +257 -0
  71. package/src/__tests__/MMapTools.test.ts +233 -0
  72. package/src/__tests__/MSeekBar.test.ts +170 -0
  73. package/src/__tests__/MTooltip.test.ts +110 -0
  74. package/src/__tests__/_fakeMaplibre.ts +161 -0
  75. package/src/__tests__/calendarMonth.test.ts +59 -0
  76. package/src/__tests__/hydration.test.ts +33 -0
  77. package/src/__tests__/pickerModals.test.ts +128 -0
  78. package/src/__tests__/popupPosition.test.ts +224 -0
  79. package/src/__tests__/scrollLock.test.ts +110 -0
  80. package/src/__tests__/sheets.test.ts +158 -0
  81. package/src/__tests__/ssr.test.ts +63 -1
  82. package/src/__tests__/ssrState.test.ts +155 -0
  83. package/src/__tests__/themeInitScript.test.ts +18 -0
  84. package/src/components/MAutocomplete.vue +2 -4
  85. package/src/components/MBottomSheet.vue +211 -13
  86. package/src/components/MCommandPalette.vue +4 -6
  87. package/src/components/MContextMenu.vue +19 -5
  88. package/src/components/MDatePicker.vue +29 -70
  89. package/src/components/MDatePickerModal.vue +116 -172
  90. package/src/components/MDateRangePicker.vue +29 -51
  91. package/src/components/MDateRangePickerModal.vue +84 -162
  92. package/src/components/MDialog.vue +87 -63
  93. package/src/components/MFab.vue +29 -7
  94. package/src/components/MIconButton.vue +1 -1
  95. package/src/components/MLightbox.vue +12 -5
  96. package/src/components/MMap.vue +568 -0
  97. package/src/components/MMapBaseLayers.vue +67 -0
  98. package/src/components/MMapControl.vue +175 -0
  99. package/src/components/MMapFeatureInfo.vue +41 -0
  100. package/src/components/MMapFilter.vue +80 -0
  101. package/src/components/MMapGeoJson.vue +136 -0
  102. package/src/components/MMapHover.vue +131 -0
  103. package/src/components/MMapIdentify.vue +104 -0
  104. package/src/components/MMapLayers.vue +111 -0
  105. package/src/components/MMapMarker.vue +109 -0
  106. package/src/components/MMapMeasure.vue +452 -0
  107. package/src/components/MMapPopup.vue +103 -0
  108. package/src/components/MMapSearch.vue +162 -0
  109. package/src/components/MMenu.vue +31 -12
  110. package/src/components/MMenuItem.vue +21 -4
  111. package/src/components/MMultiAutocomplete.vue +2 -4
  112. package/src/components/MMultiSelect.vue +2 -4
  113. package/src/components/MNavigationDrawer.vue +2 -8
  114. package/src/components/MNavigationRail.vue +2 -3
  115. package/src/components/MSearchBar.vue +2 -5
  116. package/src/components/MSeekBar.vue +593 -0
  117. package/src/components/MSelect.vue +2 -8
  118. package/src/components/MSplitButton.vue +23 -7
  119. package/src/components/MSpotlightSearch.vue +4 -6
  120. package/src/components/MTimePicker.vue +23 -6
  121. package/src/components/MTimePickerModal.vue +213 -226
  122. package/src/components/MTooltip.vue +99 -3
  123. package/src/components/_MMapAttribution.vue +81 -0
  124. package/src/components/_MMapLayerItem.vue +77 -0
  125. package/src/composables/_mapGeometry.ts +75 -0
  126. package/src/composables/_mapPin.ts +25 -0
  127. package/src/composables/_popupPosition.ts +39 -0
  128. package/src/composables/_ssrState.ts +80 -0
  129. package/src/composables/_storageKeys.ts +6 -0
  130. package/src/composables/_useCalendarMonth.ts +74 -0
  131. package/src/composables/_useEscapeStack.ts +33 -0
  132. package/src/composables/_useScrollLock.ts +41 -0
  133. package/src/composables/_useSheetMotion.ts +82 -20
  134. package/src/composables/_useThemeTokens.ts +42 -0
  135. package/src/composables/mapAdapters.ts +208 -0
  136. package/src/composables/mapSearch.ts +56 -0
  137. package/src/composables/themeInitScript.ts +6 -4
  138. package/src/composables/useColorPalette.ts +23 -9
  139. package/src/composables/useLocale.ts +78 -0
  140. package/src/composables/useMap.ts +98 -0
  141. package/src/composables/useMapFilter.ts +45 -0
  142. package/src/composables/useMapLayers.ts +124 -0
  143. package/src/composables/useTheme.ts +8 -3
  144. package/src/composables/useWindowSizeClass.ts +14 -5
  145. package/src/index.ts +4 -1
  146. package/src/locales/de.ts +39 -1
  147. package/src/locales/es.ts +39 -1
  148. package/src/locales/fr.ts +39 -1
  149. package/src/locales/ja.ts +39 -1
  150. package/src/locales/ko.ts +39 -1
  151. package/src/locales/pt.ts +39 -1
  152. package/src/locales/zh.ts +39 -1
  153. package/src/map.ts +32 -0
  154. package/src/plugin.ts +54 -3
  155. package/dist/MTextField-BPbFeI9M.js +0 -1368
  156. package/dist/MTextField-BPbFeI9M.js.map +0 -1
  157. package/dist/useLocale-CuvWRZ1h.js.map +0 -1
@@ -0,0 +1,110 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
+ import { h, nextTick } from 'vue'
3
+ import { mount } from '@vue/test-utils'
4
+ import MTooltip from '../components/MTooltip.vue'
5
+
6
+ const tip = () => document.querySelector('[role="tooltip"]')
7
+ const pointer = (type: string, pointerType: string, x = 10) =>
8
+ new PointerEvent(type, { pointerType, pointerId: 1, clientX: x, clientY: 10, bubbles: true })
9
+
10
+ describe('MTooltip input (Compose BasicTooltip)', () => {
11
+ beforeEach(() => {
12
+ document.body.innerHTML = ''
13
+ vi.useFakeTimers()
14
+ })
15
+ afterEach(() => vi.useRealTimers())
16
+
17
+ const mountTip = (props: Record<string, unknown> = {}) => {
18
+ const onClick = vi.fn()
19
+ const w = mount(MTooltip, {
20
+ props: { text: 'Change type', ...props },
21
+ slots: { default: () => h('button', { class: 'btn', onClick }, 'T') },
22
+ attachTo: document.body,
23
+ })
24
+ return { w, onClick, trigger: w.find('span').element as HTMLElement, btn: w.find('.btn').element as HTMLElement }
25
+ }
26
+
27
+ it('a mouse shows it on hover after the delay and hides it on leave', async () => {
28
+ const { trigger } = mountTip()
29
+ trigger.dispatchEvent(pointer('pointerenter', 'mouse'))
30
+ vi.advanceTimersByTime(600)
31
+ await nextTick()
32
+ expect(tip()?.textContent).toContain('Change type')
33
+ trigger.dispatchEvent(pointer('pointerleave', 'mouse'))
34
+ await nextTick()
35
+ expect(tip()).toBeNull()
36
+ })
37
+
38
+ it('a tap doesn\'t show it, and its click goes through', async () => {
39
+ const { trigger, btn, onClick } = mountTip()
40
+ // A browser emulates mouse events for a tap; the tooltip listens to pointer events
41
+ trigger.dispatchEvent(pointer('pointerenter', 'touch'))
42
+ btn.dispatchEvent(pointer('pointerdown', 'touch'))
43
+ vi.advanceTimersByTime(100)
44
+ window.dispatchEvent(pointer('pointerup', 'touch'))
45
+ // ...and after it the compatibility mouse events (mouseenter first)
46
+ trigger.dispatchEvent(new MouseEvent('mouseenter'))
47
+ btn.click()
48
+ vi.advanceTimersByTime(1000)
49
+ await nextTick()
50
+ expect(tip()).toBeNull()
51
+ expect(onClick).toHaveBeenCalledOnce()
52
+ })
53
+
54
+ it('a long press shows it, swallows its click, and hides it 1.5s after lifting', async () => {
55
+ const { btn, onClick } = mountTip()
56
+ btn.dispatchEvent(pointer('pointerdown', 'touch'))
57
+ vi.advanceTimersByTime(500)
58
+ await nextTick()
59
+ expect(tip()).not.toBeNull()
60
+ window.dispatchEvent(pointer('pointerup', 'touch'))
61
+ btn.click()
62
+ expect(onClick).not.toHaveBeenCalled()
63
+ vi.advanceTimersByTime(1400)
64
+ await nextTick()
65
+ expect(tip()).not.toBeNull()
66
+ vi.advanceTimersByTime(100)
67
+ await nextTick()
68
+ expect(tip()).toBeNull()
69
+ // The next tap clicks again
70
+ btn.dispatchEvent(pointer('pointerdown', 'touch'))
71
+ window.dispatchEvent(pointer('pointerup', 'touch'))
72
+ btn.click()
73
+ expect(onClick).toHaveBeenCalledOnce()
74
+ })
75
+
76
+ it('a press that moves (a scroll) isn\'t a long press', async () => {
77
+ const { btn } = mountTip()
78
+ btn.dispatchEvent(pointer('pointerdown', 'touch'))
79
+ window.dispatchEvent(pointer('pointermove', 'touch', 40))
80
+ vi.advanceTimersByTime(600)
81
+ await nextTick()
82
+ expect(tip()).toBeNull()
83
+ })
84
+
85
+ it('a long-pressed rich tooltip stays until a touch outside it', async () => {
86
+ const { btn } = mountTip({ rich: true })
87
+ btn.dispatchEvent(pointer('pointerdown', 'touch'))
88
+ vi.advanceTimersByTime(500)
89
+ window.dispatchEvent(pointer('pointerup', 'touch'))
90
+ vi.advanceTimersByTime(5000)
91
+ await nextTick()
92
+ expect(tip()).not.toBeNull()
93
+ tip()!.dispatchEvent(pointer('pointerdown', 'touch'))
94
+ await nextTick()
95
+ expect(tip()).not.toBeNull()
96
+ document.body.dispatchEvent(pointer('pointerdown', 'touch'))
97
+ await nextTick()
98
+ expect(tip()).toBeNull()
99
+ })
100
+
101
+ it('Escape hides it', async () => {
102
+ const { trigger, btn } = mountTip()
103
+ trigger.dispatchEvent(pointer('pointerenter', 'mouse'))
104
+ vi.advanceTimersByTime(600)
105
+ await nextTick()
106
+ btn.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
107
+ await nextTick()
108
+ expect(tip()).toBeNull()
109
+ })
110
+ })
@@ -0,0 +1,161 @@
1
+ // MapLibre needs WebGL, which happy-dom lacks: a stand-in that records what the map components ask of it
2
+ type Listener = (e?: unknown) => void
3
+ class Evented {
4
+ listeners = new Map<string, Listener[]>()
5
+ on(type: string, a: unknown, b?: unknown) {
6
+ const key = typeof a === 'string' ? `${type}:${a}` : type
7
+ const fn = (typeof a === 'string' ? b : a) as Listener
8
+ this.listeners.set(key, [...(this.listeners.get(key) ?? []), fn])
9
+ return this
10
+ }
11
+ once(type: string, fn: Listener) { return this.on(type, fn) }
12
+ off(type: string, a: unknown, b?: unknown) {
13
+ const key = typeof a === 'string' ? `${type}:${a}` : type
14
+ const fn = typeof a === 'string' ? b : a
15
+ this.listeners.set(key, (this.listeners.get(key) ?? []).filter((l) => l !== fn))
16
+ return this
17
+ }
18
+ fire(key: string, e?: unknown) { for (const l of this.listeners.get(key) ?? []) l(e) }
19
+ count(key: string) { return (this.listeners.get(key) ?? []).length }
20
+ }
21
+
22
+ export const created: { maps: FakeMap[]; markers: FakeMarker[]; popups: FakePopup[] } = { maps: [], markers: [], popups: [] }
23
+
24
+ export class FakeMap extends Evented {
25
+ options: Record<string, unknown>
26
+ center = { lng: 0, lat: 0 }
27
+ zoom = 1
28
+ bearing = 0
29
+ pitch = 0
30
+ controls: { control: { onAdd?: (m: unknown) => HTMLElement }; position: string }[] = []
31
+ sources = new Map<string, { data: unknown; setData: (d: unknown) => void }>()
32
+ layers: { id: string; [k: string]: unknown }[] = []
33
+ canvas = document.createElement('canvas')
34
+ styles: unknown[] = []
35
+ removed = false
36
+ constructor(options: Record<string, unknown>) {
37
+ super()
38
+ this.options = options
39
+ if (options.center) { const [lng, lat] = options.center as number[]; this.center = { lng: lng!, lat: lat! } }
40
+ if (options.zoom !== undefined) this.zoom = options.zoom as number
41
+ created.maps.push(this)
42
+ }
43
+ getCenter() { return this.center }
44
+ getZoom() { return this.zoom }
45
+ getBearing() { return this.bearing }
46
+ getPitch() { return this.pitch }
47
+ jumpTo(o: { center?: number[]; zoom?: number }) {
48
+ if (o.center) this.center = { lng: o.center[0]!, lat: o.center[1]! }
49
+ if (o.zoom !== undefined) this.zoom = o.zoom
50
+ }
51
+ addControl(control: FakeMap['controls'][number]['control'], position: string) {
52
+ this.controls.push({ control, position })
53
+ const el = control.onAdd?.(this)
54
+ if (el) document.body.appendChild(el)
55
+ return this
56
+ }
57
+ removeControl(control: unknown) { this.controls = this.controls.filter((c) => c.control !== control); return this }
58
+ hasControl(control: unknown) { return this.controls.some((c) => c.control === control) }
59
+ setStyle(style: unknown) {
60
+ this.styles.push(style)
61
+ this.sources.clear()
62
+ this.layers = []
63
+ }
64
+ addSource(id: string, spec: { data: unknown }) {
65
+ const source = { data: spec.data, setData: (d: unknown) => { source.data = d } }
66
+ this.sources.set(id, source)
67
+ }
68
+ getSource(id: string) {
69
+ if (this.removed) throw new TypeError("can't access property \"getSource\", this.style is undefined")
70
+ return this.sources.get(id)
71
+ }
72
+ removeSource(id: string) { this.sources.delete(id) }
73
+ addLayer(layer: { id: string }, before?: string) { this.layers.push({ ...layer, before }) }
74
+ getLayer(id: string) {
75
+ // Like MapLibre: a removed map has no style left
76
+ if (this.removed) throw new TypeError("can't access property \"getLayer\", this.style is undefined")
77
+ return this.layers.find((l) => l.id === id) ?? this.styleLayer(id)
78
+ }
79
+ removeLayer(id: string) { this.layers = this.layers.filter((l) => l.id !== id) }
80
+ getCanvas() { return this.canvas }
81
+ container = document.createElement('div')
82
+ getContainer() { return this.container }
83
+ /** The style getStyle() returns (with `layers` and `metadata`), set by tests */
84
+ style: { name?: string; metadata?: unknown; layers: { id: string; type: string; metadata?: unknown; layout?: Record<string, unknown>; paint?: Record<string, unknown> }[] } = { layers: [] }
85
+ getStyle() { return this.style }
86
+ private styleLayer(id: string) { return this.style.layers.find((l) => l.id === id) }
87
+ getLayoutProperty(id: string, prop: string) { return this.styleLayer(id)?.layout?.[prop] }
88
+ setLayoutProperty(id: string, prop: string, value: unknown) { const l = this.styleLayer(id); if (l) l.layout = { ...l.layout, [prop]: value } }
89
+ getPaintProperty(id: string, prop: string) { return this.styleLayer(id)?.paint?.[prop] }
90
+ setPaintProperty(id: string, prop: string, value: unknown) { const l = this.styleLayer(id); if (l) l.paint = { ...l.paint, [prop]: value } }
91
+ // Screen position: 1px per 0.001° (enough to tell near from far)
92
+ project([lng, lat]: number[]) { return { x: lng! * 1000, y: -lat! * 1000 } }
93
+ doubleClickZoom = { enabled: true, enable() { this.enabled = true }, disable() { this.enabled = false } }
94
+ dragPan = { enabled: true, enable() { this.enabled = true }, disable() { this.enabled = false } }
95
+ filters = new Map<string, unknown>()
96
+ getFilter(id: string) { return this.filters.get(id) ?? (this.styleLayer(id) as { filter?: unknown } | undefined)?.filter }
97
+ setFilter(id: string, filter: unknown) { this.filters.set(id, filter) }
98
+ /** A source's loaded features, set by tests */
99
+ sourceFeatures: { properties: Record<string, unknown> }[] = []
100
+ querySourceFeatures() { return this.sourceFeatures }
101
+ flights: unknown[] = []
102
+ flyTo(o: unknown) { this.flights.push(['flyTo', o]) }
103
+ fitBounds(b: unknown, o?: unknown) { this.flights.push(['fitBounds', b, o]) }
104
+ /** Features "under the pointer", set by tests */
105
+ rendered: { layer: { id: string }; properties: Record<string, unknown> }[] = []
106
+ queryRenderedFeatures(_point: unknown, options?: { layers?: string[] }) { return this.rendered.filter((f) => !options?.layers || options.layers.includes(f.layer.id)) }
107
+ remove() { this.removed = true }
108
+ /** Test helper: the style loaded */
109
+ load() { this.fire('style.load'); this.fire('load') }
110
+ }
111
+
112
+ export class FakeMarker extends Evented {
113
+ options: Record<string, unknown>
114
+ element: HTMLElement
115
+ lngLat = { lng: 0, lat: 0 }
116
+ popup?: FakePopup
117
+ map?: FakeMap
118
+ constructor(options: Record<string, unknown>) {
119
+ super()
120
+ this.options = options
121
+ this.element = (options.element as HTMLElement) ?? document.createElement('div')
122
+ created.markers.push(this)
123
+ }
124
+ setLngLat([lng, lat]: number[]) { this.lngLat = { lng: lng!, lat: lat! }; return this }
125
+ getLngLat() { return this.lngLat }
126
+ addTo(map: FakeMap) { this.map = map; return this }
127
+ getElement() { return this.element }
128
+ setPopup(p: FakePopup) { this.popup = p; return this }
129
+ getPopup() { return this.popup }
130
+ setDraggable() { return this }
131
+ setRotation() { return this }
132
+ setOffset() { return this }
133
+ remove() { this.map = undefined; return this }
134
+ }
135
+
136
+ export class FakePopup extends Evented {
137
+ options: Record<string, unknown>
138
+ content?: HTMLElement
139
+ lngLat?: { lng: number; lat: number }
140
+ opened = false
141
+ constructor(options: Record<string, unknown>) { super(); this.options = options; created.popups.push(this) }
142
+ setDOMContent(el: HTMLElement) { this.content = el; return this }
143
+ setLngLat([lng, lat]: number[]) { this.lngLat = { lng: lng!, lat: lat! }; return this }
144
+ setMaxWidth() { return this }
145
+ addTo() { this.opened = true; return this }
146
+ isOpen() { return this.opened }
147
+ remove() { if (this.opened) { this.opened = false; this.fire('close') } return this }
148
+ setOffset(o: unknown) { this.options.offset = o; return this }
149
+ }
150
+
151
+ class Control { constructor(public options?: unknown) {} }
152
+ export const fakeModule = {
153
+ Map: FakeMap,
154
+ Marker: FakeMarker,
155
+ Popup: FakePopup,
156
+ NavigationControl: class NavigationControl extends Control {},
157
+ GeolocateControl: class GeolocateControl extends Control {},
158
+ FullscreenControl: class FullscreenControl extends Control {},
159
+ ScaleControl: class ScaleControl extends Control {},
160
+ AttributionControl: class AttributionControl extends Control {},
161
+ }
@@ -0,0 +1,59 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+ import { nextTick, ref } from 'vue'
3
+ import { mount } from '@vue/test-utils'
4
+ import { isoDate, useCalendarMonth } from '../composables/_useCalendarMonth'
5
+ import MDatePickerModal from '../components/MDatePickerModal.vue'
6
+
7
+ afterEach(() => {
8
+ document.body.innerHTML = ''
9
+ document.body.style.overflow = ''
10
+ })
11
+
12
+ describe('useCalendarMonth (the date pickers\' shared month grid)', () => {
13
+ const month = (date: string, min?: string, max?: string) =>
14
+ useCalendarMonth(ref(new Date(date + 'T00:00:00')), { locale: () => 'en-US', min: () => min, max: () => max })
15
+
16
+ it('lays out six weeks from Monday, with the neighbors\' days around the month', () => {
17
+ // October 2026 starts on a Thursday
18
+ const { days, monthLabel, weekdays } = month('2026-10-20')
19
+ expect(days.value).toHaveLength(42)
20
+ expect(days.value[0]).toEqual({ date: 28, current: false, iso: '2026-09-28', disabled: false })
21
+ expect(days.value[3]).toEqual({ date: 1, current: true, iso: '2026-10-01', disabled: false })
22
+ expect(days.value.filter((d) => d.current)).toHaveLength(31)
23
+ expect(days.value[41]!.iso).toBe('2026-11-08')
24
+ expect(monthLabel.value).toBe('October 2026')
25
+ expect(weekdays.value).toEqual(['M', 'T', 'W', 'T', 'F', 'S', 'S'])
26
+ })
27
+
28
+ it('disables the days outside min and max', () => {
29
+ const { days } = month('2026-10-20', '2026-10-10', '2026-10-15')
30
+ const disabled = (iso: string) => days.value.find((d) => d.iso === iso)!.disabled
31
+ expect([disabled('2026-10-09'), disabled('2026-10-10'), disabled('2026-10-15'), disabled('2026-10-16')]).toEqual([true, false, false, true])
32
+ })
33
+
34
+ it('moves a month at a time from the 31st without skipping one', () => {
35
+ const view = ref(new Date('2026-03-31T00:00:00'))
36
+ const { nextMonth, prevMonth, monthLabel } = useCalendarMonth(view, { locale: () => 'en-US', min: () => undefined, max: () => undefined })
37
+ nextMonth()
38
+ expect(monthLabel.value).toBe('April 2026')
39
+ prevMonth()
40
+ prevMonth()
41
+ expect(monthLabel.value).toBe('February 2026')
42
+ })
43
+
44
+ it('isoDate rolls months and days over like Date', () => {
45
+ expect(isoDate(2026, -1, 31)).toBe('2025-12-31')
46
+ expect(isoDate(2026, 1, 29)).toBe('2026-03-01')
47
+ })
48
+
49
+ it('the modal date picker opened on the 31st goes to the next month, not the one after', async () => {
50
+ const w = mount(MDatePickerModal, { props: { modelValue: '2026-03-31', show: false, locale: 'en-US' }, attachTo: document.body })
51
+ await w.setProps({ show: true })
52
+ await nextTick()
53
+ const next = document.querySelector<HTMLElement>('[aria-label="Next month"]')!
54
+ next.click()
55
+ await nextTick()
56
+ expect(document.body.textContent).toContain('April 2026')
57
+ w.unmount()
58
+ })
59
+ })
@@ -2,6 +2,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
2
2
  import { createSSRApp, h, nextTick, type Component } from 'vue'
3
3
  import { renderToString } from 'vue/server-renderer'
4
4
 
5
+ // Each test imports the whole library afresh: a cold import can take seconds when the suite runs in parallel
6
+ vi.setConfig({ testTimeout: 30_000 })
7
+
5
8
  /** A fresh copy of the library seeing a window of this width (the window size is read when its modules load). */
6
9
  async function load(width: number) {
7
10
  Object.defineProperty(window, 'innerWidth', { value: width, configurable: true })
@@ -71,6 +74,36 @@ describe('hydrating server-rendered adaptive layouts', () => {
71
74
  expect(container.textContent).toContain('content')
72
75
  })
73
76
 
77
+ it('with a viewport (from a cookie), renders the right layout from the server on, without a jump', async () => {
78
+ const props = { modelValue: 'a', items }
79
+ const slots = { default: () => 'content' }
80
+ const viewport = { width: 1200, height: 800 }
81
+ const server = await load(0)
82
+ // Under happy-dom there is a window, so the flag stands in for the server's (ssr.test.ts renders it without one)
83
+ const html = await renderToString(createSSRApp({ render: () => h(server.MNavigationSuite, props, slots) }).use(server.createM3UI({ initialState: { viewport }, isHydrating: () => true })))
84
+ expect(html).toMatch(/^<div class="[^"]*flex-row/)
85
+
86
+ const lib = await load(1200)
87
+ const container = document.createElement('div')
88
+ container.innerHTML = html
89
+ document.body.appendChild(container)
90
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
91
+ const layouts: string[] = []
92
+ const app = createSSRApp({
93
+ render: () => h(lib.MNavigationSuite, { ...props, onVnodeUpdated: () => layouts.push(container.firstElementChild!.className) }, slots),
94
+ })
95
+ app.use(lib.createM3UI({ initialState: { viewport }, isHydrating: () => true }))
96
+ // happy-dom has no layout: what the browser measures once mounted
97
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(1200)
98
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(800)
99
+ app.mount(container)
100
+ await nextTick()
101
+ expect(warn.mock.calls.map((c) => String(c[0])).filter((m) => m.includes('Hydration'))).toEqual([])
102
+ // Never compact on the client either
103
+ expect(container.firstElementChild!.className).toContain('flex-row')
104
+ expect(layouts.filter((c) => c.includes('flex-col'))).toEqual([])
105
+ })
106
+
74
107
  it('without isHydrating, guesses from the window on the first render (client-only apps)', async () => {
75
108
  const { useWindowSizeClass } = await load(1024)
76
109
  let first = -1
@@ -0,0 +1,128 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+ import { defineComponent, h, nextTick, ref } from 'vue'
3
+ import { mount } from '@vue/test-utils'
4
+ import MDialog from '../components/MDialog.vue'
5
+ import MDatePickerModal from '../components/MDatePickerModal.vue'
6
+ import MDateRangePickerModal from '../components/MDateRangePickerModal.vue'
7
+ import MTimePickerModal from '../components/MTimePickerModal.vue'
8
+ import { M3_LOCALE_KEY } from '../composables/useLocale'
9
+ import { esLocale } from '../locales'
10
+
11
+ afterEach(() => {
12
+ document.body.innerHTML = ''
13
+ document.body.style.overflow = ''
14
+ })
15
+
16
+ const locked = () => document.body.style.overflow === 'hidden'
17
+ const box = () => document.querySelector<HTMLElement>('[role="dialog"]')
18
+
19
+ // The three modal pickers are MDialog's bare box now: its scrim, Escape, scroll lock and dialog semantics
20
+ describe.each([
21
+ ['MDatePickerModal', MDatePickerModal, null, 'Select date'],
22
+ ['MDateRangePickerModal', MDateRangePickerModal, { start: null, end: null }, 'Select range'],
23
+ ['MTimePickerModal', MTimePickerModal, null, 'Select time'],
24
+ ] as const)('%s', (_, Comp, value, title) => {
25
+ const open = () => mount(Comp as any, { props: { modelValue: value, show: true, title }, attachTo: document.body })
26
+
27
+ it('is a modal dialog named by its title, and locks the page', async () => {
28
+ const w = open()
29
+ await nextTick()
30
+ expect(box()?.getAttribute('aria-modal')).toBe('true')
31
+ expect(box()?.getAttribute('aria-label')).toBe(title)
32
+ expect(locked()).toBe(true)
33
+ w.unmount()
34
+ expect(locked()).toBe(false)
35
+ })
36
+
37
+ it('Escape cancels without a value', async () => {
38
+ const w = open()
39
+ await nextTick()
40
+ document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }))
41
+ expect(w.emitted('update:show')?.[0]).toEqual([false])
42
+ expect(w.emitted('update:modelValue')).toBeUndefined()
43
+ w.unmount()
44
+ })
45
+
46
+ it('a click on the scrim cancels', async () => {
47
+ const w = open()
48
+ await nextTick()
49
+ box()!.parentElement!.click()
50
+ expect(w.emitted('update:show')?.[0]).toEqual([false])
51
+ w.unmount()
52
+ })
53
+
54
+ it('opened from a dialog, closing it leaves the page locked under the dialog', async () => {
55
+ const show = ref(true)
56
+ const w = mount(defineComponent({
57
+ setup: () => () => h(MDialog, { modelValue: true, title: 'Booking' }, () =>
58
+ h(Comp as any, { modelValue: value, show: show.value })),
59
+ }), { attachTo: document.body })
60
+ await nextTick()
61
+ show.value = false
62
+ await nextTick()
63
+ expect(locked()).toBe(true)
64
+ w.unmount()
65
+ })
66
+ })
67
+
68
+ describe('the picker dialogs\' text comes from the locale', () => {
69
+ const withLocale = (locale: Record<string, string>) => ({ global: { provide: { [M3_LOCALE_KEY as symbol]: locale } } })
70
+
71
+ it('OK, the input toggle and the date input follow the app\'s language and date order', async () => {
72
+ const w = mount(MDatePickerModal, {
73
+ props: { modelValue: '2026-03-04', show: false },
74
+ attachTo: document.body,
75
+ ...withLocale(esLocale as unknown as Record<string, string>),
76
+ })
77
+ await w.setProps({ show: true })
78
+ await nextTick()
79
+ expect(document.body.textContent).toContain('Aceptar')
80
+ const toggle = document.querySelector<HTMLElement>('[aria-label="Cambiar a entrada de texto"]')!
81
+ toggle.click()
82
+ await nextTick()
83
+ const input = document.querySelector<HTMLInputElement>('input[type="text"]')!
84
+ // Spain: DD/MM/YYYY
85
+ expect(input.value).toBe('04/03/2026')
86
+ expect(input.placeholder).toBe('DD/MM/YYYY')
87
+ expect(document.body.textContent).toContain('Fecha (DD/MM/YYYY)')
88
+ input.value = '31/02/2026'
89
+ input.dispatchEvent(new Event('input'))
90
+ input.dispatchEvent(new Event('blur'))
91
+ await nextTick()
92
+ // Not a date: nothing changes
93
+ expect(document.body.textContent).toContain('4 mar')
94
+ input.value = '9/3/2026'
95
+ input.dispatchEvent(new Event('input'))
96
+ input.dispatchEvent(new Event('blur'))
97
+ await nextTick()
98
+ expect(document.body.textContent).toContain('9 mar')
99
+ w.unmount()
100
+ })
101
+
102
+ it('reads the US order with an English locale', async () => {
103
+ const w = mount(MDatePickerModal, { props: { modelValue: '2026-03-04', show: false, locale: 'en-US' }, attachTo: document.body })
104
+ await w.setProps({ show: true })
105
+ await nextTick()
106
+ document.querySelector<HTMLElement>('[aria-label="Switch to text input"]')!.click()
107
+ await nextTick()
108
+ const input = document.querySelector<HTMLInputElement>('input[type="text"]')!
109
+ expect(input.value).toBe('03/04/2026')
110
+ expect(input.placeholder).toBe('MM/DD/YYYY')
111
+ w.unmount()
112
+ })
113
+
114
+ it('the time picker\'s AM/PM and OK are in the app\'s language', async () => {
115
+ const w = mount(MTimePickerModal, {
116
+ props: { modelValue: '09:30', show: false },
117
+ attachTo: document.body,
118
+ ...withLocale(esLocale as unknown as Record<string, string>),
119
+ })
120
+ await w.setProps({ show: true })
121
+ await nextTick()
122
+ const text = document.body.textContent!
123
+ expect(text).toContain('Aceptar')
124
+ expect(text).not.toMatch(/\bAM\b/)
125
+ expect(text).toMatch(/a\.\s?m\./)
126
+ w.unmount()
127
+ })
128
+ })