useCollapseHandler.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. await nextTick()
  32. expect(emit).toHaveBeenCalledWith('collapse', true)
  33. })
  34. it('calls expand if collapse state is false', async () => {
  35. const TestComponent = {
  36. setup() {
  37. const { toggleCollapse } = useCollapseHandler(emit, {
  38. storageKey: 'test',
  39. })
  40. toggleCollapse()
  41. },
  42. template: '<div></div>',
  43. }
  44. mount(TestComponent)
  45. await nextTick()
  46. expect(emit).toHaveBeenCalledWith('collapse', true)
  47. })
  48. })