useCollapseHandler.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { mount } from '@vue/test-utils'
  3. import { beforeEach, vi } from 'vitest'
  4. import { nextTick } from 'vue'
  5. import { useCollapseHandler } from '#desktop/components/CollapseButton/composables/useCollapseHandler.ts'
  6. describe('useCollapseHandler', async () => {
  7. const emit = vi.fn()
  8. beforeEach(() => {
  9. localStorage.clear()
  10. })
  11. it('initializes with collapsed state from local storage', async () => {
  12. const TestComponent = {
  13. setup() {
  14. const { isCollapsed } = useCollapseHandler(emit, { storageKey: 'test' })
  15. expect(isCollapsed.value).toBe(false)
  16. },
  17. template: '<div></div>',
  18. }
  19. mount(TestComponent)
  20. })
  21. it('sync local storage state on initial load', async () => {
  22. localStorage.setItem('test', 'true')
  23. const TestComponent = {
  24. setup() {
  25. const { isCollapsed } = useCollapseHandler(emit, { storageKey: 'test' })
  26. expect(isCollapsed.value).toBe(true)
  27. },
  28. template: '<div></div>',
  29. }
  30. mount(TestComponent)
  31. expect(emit).toHaveBeenCalledWith('collapse', true)
  32. })
  33. it('sync local storage state on subsequent mutations', async () => {
  34. localStorage.setItem('test', 'true')
  35. const TestComponent = {
  36. setup() {
  37. const { isCollapsed } = useCollapseHandler(emit, { storageKey: 'test' })
  38. expect(isCollapsed.value).toBe(true)
  39. },
  40. template: '<div></div>',
  41. }
  42. mount(TestComponent)
  43. expect(emit).toHaveBeenCalled()
  44. localStorage.setItem('test', 'false')
  45. expect(emit).toHaveBeenCalled()
  46. })
  47. it('calls expand if collapse state is false', async () => {
  48. const TestComponent = {
  49. setup() {
  50. const { toggleCollapse } = useCollapseHandler(emit, {
  51. storageKey: 'test',
  52. })
  53. toggleCollapse()
  54. },
  55. template: '<div></div>',
  56. }
  57. mount(TestComponent)
  58. await nextTick()
  59. expect(emit).toHaveBeenCalledWith('collapse', true)
  60. })
  61. })