layout-store.spec.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @vitest-environment jsdom
  2. /**
  3. * createLayoutStore unit account: init shape, the action write set (clamp
  4. * inside actions), and the persist key round-trip over jsdom localStorage.
  5. * Uses the test-sanctioned path: factory self-call + .create() gives the
  6. * real engine instance (same create path as production).
  7. */
  8. import { beforeEach, describe, expect, it } from 'vitest'
  9. import { createLayoutStore } from '@deepseek-ai/dsh-client-ui-layout/src/client/stores.ts'
  10. import {
  11. DETAILS_DEFAULT, DETAILS_MAX, DETAILS_MIN,
  12. SIDEBAR_DEFAULT, SIDEBAR_MAX, SIDEBAR_MIN,
  13. } from '@deepseek-ai/dsh-client-ui-layout/src/client/columns.ts'
  14. const PERSIST_KEY = 'dsh.layout.panels'
  15. beforeEach(() => { localStorage.clear() })
  16. describe('createLayoutStore', () => {
  17. it('initializes with sidebar open at default and details closed', () => {
  18. const { store } = createLayoutStore().create()
  19. expect(store.getSnapshot()).toEqual({ sidebar: SIDEBAR_DEFAULT, details: 0 })
  20. })
  21. it('each create() is an independent instance (factory is not a singleton)', () => {
  22. const a = createLayoutStore().create()
  23. const b = createLayoutStore().create()
  24. a.actions.setSidebar(400)
  25. expect(b.store.getSnapshot().sidebar).toBe(SIDEBAR_DEFAULT)
  26. })
  27. it('setSidebar/setDetails clamp into the contract ranges', () => {
  28. const { store, actions } = createLayoutStore().create()
  29. actions.setSidebar(1)
  30. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_MIN)
  31. actions.setSidebar(9999)
  32. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_MAX)
  33. actions.setDetails(1)
  34. expect(store.getSnapshot().details).toBe(DETAILS_MIN)
  35. actions.setDetails(9999)
  36. expect(store.getSnapshot().details).toBe(DETAILS_MAX)
  37. })
  38. it('toggleSidebar flips closed <-> contract default (drag width forgotten)', () => {
  39. const { store, actions } = createLayoutStore().create()
  40. actions.setSidebar(400)
  41. actions.toggleSidebar()
  42. expect(store.getSnapshot().sidebar).toBe(0)
  43. actions.toggleSidebar()
  44. expect(store.getSnapshot().sidebar).toBe(SIDEBAR_DEFAULT)
  45. })
  46. it('openDetails is a no-op when already open; closeDetails zeroes', () => {
  47. const { store, actions } = createLayoutStore().create()
  48. actions.openDetails()
  49. expect(store.getSnapshot().details).toBe(DETAILS_DEFAULT)
  50. actions.setDetails(500)
  51. actions.openDetails()
  52. expect(store.getSnapshot().details).toBe(500)
  53. actions.closeDetails()
  54. expect(store.getSnapshot().details).toBe(0)
  55. })
  56. it('persists under dsh.layout.panels and rehydrates on the next create', () => {
  57. const first = createLayoutStore().create()
  58. first.actions.setSidebar(320)
  59. first.actions.openDetails()
  60. expect(JSON.parse(localStorage.getItem(PERSIST_KEY) ?? '{}')).toEqual({ sidebar: 320, details: DETAILS_DEFAULT })
  61. const second = createLayoutStore().create()
  62. expect(second.store.getSnapshot()).toEqual({ sidebar: 320, details: DETAILS_DEFAULT })
  63. })
  64. })