|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 2 | +import { flushSync, tick } from 'svelte' |
| 3 | +import { SideDocument, initialize } from '../../src/SideDocument' |
| 4 | + |
| 5 | +// QRCode は Canvas が必要なためモック |
| 6 | +vi.mock('qrcode', () => ({ |
| 7 | + default: { |
| 8 | + toDataURL: vi.fn().mockResolvedValue('data:image/png;base64,mock'), |
| 9 | + }, |
| 10 | +})) |
| 11 | + |
| 12 | +/** Shadow DOM 内の要素を取得するヘルパー */ |
| 13 | +function getShadowElement<T extends Element>(selector: string): T | null { |
| 14 | + const host = document.querySelector('#sd-container') |
| 15 | + return (host?.shadowRoot?.querySelector(selector) ?? null) as T | null |
| 16 | +} |
| 17 | + |
| 18 | +describe('SideDocument', () => { |
| 19 | + let sd: SideDocument |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + sd?.destroy() |
| 23 | + localStorage.clear() |
| 24 | + }) |
| 25 | + |
| 26 | + // ───────────────────────────────────────────── |
| 27 | + // 初期化 |
| 28 | + // ───────────────────────────────────────────── |
| 29 | + describe('initialize()', () => { |
| 30 | + it('SideDocument インスタンスを返す', async () => { |
| 31 | + sd = await initialize() |
| 32 | + expect(sd).toBeInstanceOf(SideDocument) |
| 33 | + }) |
| 34 | + |
| 35 | + it('#sd-container が document.body に追加される', async () => { |
| 36 | + sd = await initialize() |
| 37 | + expect(document.querySelector('#sd-container')).not.toBeNull() |
| 38 | + }) |
| 39 | + |
| 40 | + it('initialize() の関数エクスポートでも初期化できる', async () => { |
| 41 | + sd = await initialize({ primaryColor: '#ff0000' }) |
| 42 | + expect(sd).toBeInstanceOf(SideDocument) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + // ───────────────────────────────────────────── |
| 47 | + // destroy() |
| 48 | + // ───────────────────────────────────────────── |
| 49 | + describe('destroy()', () => { |
| 50 | + it('#sd-container が DOM から削除される', async () => { |
| 51 | + sd = await initialize() |
| 52 | + sd.destroy() |
| 53 | + expect(document.querySelector('#sd-container')).toBeNull() |
| 54 | + }) |
| 55 | + |
| 56 | + it('destroy() を複数回呼んでもエラーにならない', async () => { |
| 57 | + sd = await initialize() |
| 58 | + expect(() => { |
| 59 | + sd.destroy() |
| 60 | + sd.destroy() |
| 61 | + }).not.toThrow() |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + // ───────────────────────────────────────────── |
| 66 | + // ドロワー開閉 |
| 67 | + // ───────────────────────────────────────────── |
| 68 | + describe('ドロワーの開閉', () => { |
| 69 | + beforeEach(async () => { |
| 70 | + sd = await initialize() |
| 71 | + }) |
| 72 | + |
| 73 | + it('初期状態では閉じている', () => { |
| 74 | + expect(sd.isOpenedDrawer()).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + it('openDrawer() で開く', async () => { |
| 78 | + sd.openDrawer() |
| 79 | + flushSync() |
| 80 | + expect(sd.isOpenedDrawer()).toBe(true) |
| 81 | + }) |
| 82 | + |
| 83 | + it('closeDrawer() で閉じる', async () => { |
| 84 | + sd.openDrawer() |
| 85 | + flushSync() |
| 86 | + sd.closeDrawer() |
| 87 | + flushSync() |
| 88 | + expect(sd.isOpenedDrawer()).toBe(false) |
| 89 | + }) |
| 90 | + |
| 91 | + it('toggleDrawer() で開閉を切り替える', async () => { |
| 92 | + sd.toggleDrawer() |
| 93 | + flushSync() |
| 94 | + expect(sd.isOpenedDrawer()).toBe(true) |
| 95 | + |
| 96 | + sd.toggleDrawer() |
| 97 | + flushSync() |
| 98 | + expect(sd.isOpenedDrawer()).toBe(false) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + // ───────────────────────────────────────────── |
| 103 | + // コールバック |
| 104 | + // ───────────────────────────────────────────── |
| 105 | + describe('コールバック', () => { |
| 106 | + describe('onOpen', () => { |
| 107 | + it('openDrawer(url) 呼び出し時に url を渡して呼ばれる', async () => { |
| 108 | + const onOpen = vi.fn() |
| 109 | + sd = await initialize({ onOpen }) |
| 110 | + sd.openDrawer('https://example.com') |
| 111 | + expect(onOpen).toHaveBeenCalledOnce() |
| 112 | + expect(onOpen).toHaveBeenCalledWith('https://example.com') |
| 113 | + }) |
| 114 | + |
| 115 | + it('openDrawer() を url なしで呼んだ場合は defaultSrc を渡す', async () => { |
| 116 | + const onOpen = vi.fn() |
| 117 | + sd = await initialize({ defaultSrc: 'https://default.com', onOpen }) |
| 118 | + sd.openDrawer() |
| 119 | + expect(onOpen).toHaveBeenCalledWith('https://default.com') |
| 120 | + }) |
| 121 | + |
| 122 | + it('openDrawer() を url なしかつ defaultSrc なしの場合は空文字を渡す', async () => { |
| 123 | + const onOpen = vi.fn() |
| 124 | + sd = await initialize({ onOpen }) |
| 125 | + sd.openDrawer() |
| 126 | + expect(onOpen).toHaveBeenCalledWith('') |
| 127 | + }) |
| 128 | + |
| 129 | + it('openDrawer() を複数回呼ぶと呼び出し回数が増える', async () => { |
| 130 | + const onOpen = vi.fn() |
| 131 | + sd = await initialize({ onOpen }) |
| 132 | + sd.openDrawer('https://a.com') |
| 133 | + sd.closeDrawer() |
| 134 | + sd.openDrawer('https://b.com') |
| 135 | + expect(onOpen).toHaveBeenCalledTimes(2) |
| 136 | + expect(onOpen).toHaveBeenLastCalledWith('https://b.com') |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe('onClose', () => { |
| 141 | + it('closeDrawer() 呼び出し時に呼ばれる', async () => { |
| 142 | + const onClose = vi.fn() |
| 143 | + sd = await initialize({ onClose }) |
| 144 | + sd.openDrawer() |
| 145 | + sd.closeDrawer() |
| 146 | + expect(onClose).toHaveBeenCalledOnce() |
| 147 | + }) |
| 148 | + |
| 149 | + it('ドロワーが閉じている状態で closeDrawer() を呼んでも呼ばれる', async () => { |
| 150 | + const onClose = vi.fn() |
| 151 | + sd = await initialize({ onClose }) |
| 152 | + sd.closeDrawer() |
| 153 | + expect(onClose).toHaveBeenCalledOnce() |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + describe('onOpen / onClose の組み合わせ', () => { |
| 158 | + it('open → close → open の順で正しく呼ばれる', async () => { |
| 159 | + const onOpen = vi.fn() |
| 160 | + const onClose = vi.fn() |
| 161 | + sd = await initialize({ onOpen, onClose }) |
| 162 | + |
| 163 | + sd.openDrawer('https://a.com') |
| 164 | + sd.closeDrawer() |
| 165 | + sd.openDrawer('https://b.com') |
| 166 | + |
| 167 | + expect(onOpen).toHaveBeenCalledTimes(2) |
| 168 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + describe('onPositionChange', () => { |
| 173 | + it('位置変更ボタンをクリックすると呼ばれる', async () => { |
| 174 | + const onPositionChange = vi.fn() |
| 175 | + sd = await initialize({ |
| 176 | + drawerPosition: 'right', |
| 177 | + onPositionChange, |
| 178 | + }) |
| 179 | + sd.openDrawer() |
| 180 | + await tick() // Svelte が {#if isOpened} ブロックを描画するのを待つ |
| 181 | + |
| 182 | + const btn = getShadowElement<HTMLButtonElement>( |
| 183 | + '[aria-label="表示位置を変更"]', |
| 184 | + ) |
| 185 | + expect(btn).not.toBeNull() |
| 186 | + btn!.click() |
| 187 | + |
| 188 | + expect(onPositionChange).toHaveBeenCalledOnce() |
| 189 | + expect(onPositionChange).toHaveBeenCalledWith( |
| 190 | + expect.stringMatching(/^(left|right)$/), |
| 191 | + ) |
| 192 | + }) |
| 193 | + |
| 194 | + it('right → left → right と交互に切り替わる', async () => { |
| 195 | + const onPositionChange = vi.fn() |
| 196 | + sd = await initialize({ |
| 197 | + drawerPosition: 'right', |
| 198 | + onPositionChange, |
| 199 | + }) |
| 200 | + sd.openDrawer() |
| 201 | + await tick() |
| 202 | + |
| 203 | + const btn = getShadowElement<HTMLButtonElement>( |
| 204 | + '[aria-label="表示位置を変更"]', |
| 205 | + )! |
| 206 | + btn.click() |
| 207 | + expect(onPositionChange).toHaveBeenLastCalledWith('left') |
| 208 | + btn.click() |
| 209 | + expect(onPositionChange).toHaveBeenLastCalledWith('right') |
| 210 | + }) |
| 211 | + }) |
| 212 | + |
| 213 | + describe('onResize', () => { |
| 214 | + it('リサイズバーのドラッグ完了時に新しい幅で呼ばれる', async () => { |
| 215 | + const onResize = vi.fn() |
| 216 | + sd = await initialize({ onResize, drawerWidth: 380 }) |
| 217 | + sd.openDrawer() |
| 218 | + await tick() |
| 219 | + |
| 220 | + const resizeBar = getShadowElement<HTMLSpanElement>('.sd-resize-bar') |
| 221 | + expect(resizeBar).not.toBeNull() |
| 222 | + |
| 223 | + // mousedown でリサイズ開始 |
| 224 | + resizeBar!.dispatchEvent( |
| 225 | + new MouseEvent('mousedown', { bubbles: true, clientX: 500 }), |
| 226 | + ) |
| 227 | + // mousemove でドラッグ(左へ 100px) |
| 228 | + window.dispatchEvent( |
| 229 | + new MouseEvent('mousemove', { bubbles: true, clientX: 400 }), |
| 230 | + ) |
| 231 | + // mouseup でリサイズ完了 |
| 232 | + window.dispatchEvent(new MouseEvent('mouseup', { bubbles: true })) |
| 233 | + |
| 234 | + expect(onResize).toHaveBeenCalledOnce() |
| 235 | + expect(onResize).toHaveBeenCalledWith(expect.any(Number)) |
| 236 | + }) |
| 237 | + }) |
| 238 | + }) |
| 239 | + |
| 240 | + // ───────────────────────────────────────────── |
| 241 | + // persistState / clearPersistedState |
| 242 | + // ───────────────────────────────────────────── |
| 243 | + describe('clearPersistedState()', () => { |
| 244 | + it('localStorage からドロワーの状態を削除する', async () => { |
| 245 | + sd = await initialize({ persistState: true, storageKeyPrefix: 'test' }) |
| 246 | + sd.openDrawer() |
| 247 | + await tick() |
| 248 | + |
| 249 | + // localStorage に何か保存されていることを確認 |
| 250 | + const key = 'test-state' |
| 251 | + // 少し待って save のスロットリングが走るようにする |
| 252 | + await new Promise(resolve => setTimeout(resolve, 600)) |
| 253 | + expect(localStorage.getItem(key)).not.toBeNull() |
| 254 | + |
| 255 | + sd.clearPersistedState() |
| 256 | + expect(localStorage.getItem(key)).toBeNull() |
| 257 | + }) |
| 258 | + }) |
| 259 | + |
| 260 | + // ───────────────────────────────────────────── |
| 261 | + // update() |
| 262 | + // ───────────────────────────────────────────── |
| 263 | + describe('update()', () => { |
| 264 | + it('オプションを動的に更新できる', async () => { |
| 265 | + sd = await initialize({ primaryColor: '#000000' }) |
| 266 | + await expect(sd.update({ primaryColor: '#ff0000' })).resolves.not.toThrow() |
| 267 | + }) |
| 268 | + }) |
| 269 | +}) |
0 commit comments