LayoutSidebar.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { fireEvent } from '@testing-library/vue'
  3. import { beforeEach, describe, expect } from 'vitest'
  4. import renderComponent, {
  5. type ExtendedRenderResult,
  6. } from '#tests/support/components/renderComponent.ts'
  7. import LayoutSidebar from '#desktop/components/layout/LayoutSidebar.vue'
  8. describe('LayoutSidebar', () => {
  9. describe('Feature: Collapsible', () => {
  10. let view: ExtendedRenderResult
  11. beforeEach(() => {
  12. view = renderComponent(LayoutSidebar, {
  13. props: {
  14. name: 'testBar-collapsible',
  15. id: 'sidebar-test',
  16. collapsible: true,
  17. resizable: false,
  18. },
  19. })
  20. })
  21. it('shows CollapseButton in expanded state when collapsible is true', async () => {
  22. expect(
  23. view.queryByLabelText('Expand this element'),
  24. ).not.toBeInTheDocument()
  25. // By default, is expanded and aria label shows collapse action
  26. expect(view.queryByLabelText('Collapse this element')).toBeInTheDocument()
  27. })
  28. it('does not show CollapseButton when collapsible is false', async () => {
  29. await view.rerender({ collapsible: false })
  30. expect(
  31. view.queryByLabelText('collapse this item'),
  32. ).not.toBeInTheDocument()
  33. expect(view.queryByLabelText('expand this item')).not.toBeInTheDocument()
  34. })
  35. it('hides action Button when no iconCollapsed is provided', async () => {
  36. expect(view.queryByTestId('action-button')).not.toBeInTheDocument()
  37. })
  38. it('shows an action Button when iconCollapsed is provided and sidebar is collapsed', async () => {
  39. await view.rerender({ iconCollapsed: 'person-gear' })
  40. const collapseButton = await view.findByLabelText('Collapse this element')
  41. await view.events.click(collapseButton)
  42. expect(view.queryByTestId('action-button')).toBeInTheDocument()
  43. })
  44. })
  45. describe('Feature: Resizable', () => {
  46. let view: ExtendedRenderResult
  47. beforeEach(() => {
  48. view = renderComponent(LayoutSidebar, {
  49. props: {
  50. name: 'testBar-resize',
  51. id: 'sidebar-test',
  52. collapsible: false,
  53. resizable: true,
  54. },
  55. })
  56. })
  57. it('shows ResizeHandle when resizable is true', async () => {
  58. expect(view.queryByLabelText('Resize sidebar')).toBeInTheDocument()
  59. })
  60. it('does not show ResizeHandle when resizable is false', async () => {
  61. await view.rerender({ resizable: false })
  62. expect(view.queryByLabelText('Resize sidebar')).not.toBeInTheDocument()
  63. })
  64. it('resizes sidebar when ResizeHandle is clicked and dragged', async () => {
  65. const resizeHandle = await view.findByLabelText('Resize sidebar')
  66. await view.events.click(resizeHandle)
  67. // Emulate mouse down on resize handle and mouse move on document
  68. await fireEvent.mouseDown(resizeHandle, { clientX: 0 })
  69. await fireEvent.mouseMove(document, { clientX: 100 })
  70. await fireEvent.mouseUp(document, { clientX: 100 })
  71. expect(view.emitted('resize-horizontal')).toEqual([[100]])
  72. })
  73. it('resizes sidebar when ResizeHandle is touched and dragged', async () => {
  74. const resizeHandle = await view.findByLabelText('Resize sidebar')
  75. await view.events.click(resizeHandle)
  76. // Touch device
  77. await fireEvent.touchStart(resizeHandle, { pageX: 0 })
  78. await fireEvent.touchMove(document, { pageX: 100 })
  79. await fireEvent.touchEnd(document, { pageX: 100 })
  80. // :TODO check why we can not use toEqual([[100]])
  81. expect(view.emitted('resize-horizontal')).toBeTruthy()
  82. })
  83. it('resets width when ResizeHandle is double clicked', async () => {
  84. const resizeHandle = await view.findByLabelText('Resize sidebar')
  85. await fireEvent.dblClick(resizeHandle)
  86. expect(view.emitted('reset-width')).toBeTruthy()
  87. })
  88. it('hides ResizeHandle when sidebar is collapsed', async () => {
  89. await view.rerender({ collapsible: true })
  90. const collapseButton = await view.findByLabelText('Collapse this element')
  91. await view.events.click(collapseButton)
  92. expect(view.queryByLabelText('Resize sidebar')).not.toBeInTheDocument()
  93. })
  94. })
  95. })