CommonFlyout.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { describe, expect } from 'vitest'
  3. import { nextTick } from 'vue'
  4. import renderComponent from '#tests/support/components/renderComponent.ts'
  5. import CommonFlyout from '#desktop/components/CommonFlyout/CommonFlyout.vue'
  6. const html = String.raw
  7. describe('CommonFlyout', () => {
  8. describe('standalone component', () => {
  9. let flyout: ReturnType<typeof renderComponent>
  10. beforeEach(() => {
  11. flyout = renderComponent(CommonFlyout, {
  12. props: {
  13. name: 'test-identifier',
  14. headerTitle: 'Test Title',
  15. headerIcon: 'buildings',
  16. showBackdrop: false,
  17. },
  18. })
  19. })
  20. it('renders the correct title', async () => {
  21. expect(flyout.getByText('Test Title')).toBeInTheDocument()
  22. })
  23. it('renders the correct icon', async () => {
  24. expect(flyout.queryByIconName('buildings')).toBeInTheDocument()
  25. })
  26. it('renders a default submit label', async () => {
  27. expect(flyout.getByText('Update')).toBeInTheDocument()
  28. })
  29. it('renders a custom submit label', async () => {
  30. await flyout.rerender({
  31. footerActionOptions: {
  32. actionLabel: 'Submit',
  33. },
  34. })
  35. expect(flyout.getByText('Submit')).toBeInTheDocument()
  36. })
  37. it('renders a default cancel label', () => {
  38. expect(flyout.getByText('Cancel & Go Back')).toBeInTheDocument()
  39. })
  40. it('renders a custom cancel label', async () => {
  41. await flyout.rerender({
  42. footerActionOptions: {
  43. cancelLabel: 'Exit',
  44. },
  45. })
  46. expect(flyout.getByText('Exit')).toBeInTheDocument()
  47. })
  48. it('renders the resize handle as a default', () => {
  49. expect(flyout.queryByLabelText('Resize side panel')).toBeInTheDocument()
  50. })
  51. it('does not render the resize handle when allowResizing is false', async () => {
  52. await flyout.rerender({
  53. resizable: false,
  54. })
  55. expect(
  56. flyout.queryByLabelText('Resize side panel'),
  57. ).not.toBeInTheDocument()
  58. })
  59. it('renders slot content', async () => {
  60. const flyout = renderComponent(CommonFlyout, {
  61. props: {
  62. name: 'test-identifier',
  63. label: 'Test',
  64. headerTitle: 'Test Title',
  65. titleIcon: 'buildings',
  66. showBackdrop: false,
  67. },
  68. slots: {
  69. header: 'Foo header',
  70. default: 'Hello world!',
  71. footer: 'Foo submit',
  72. },
  73. })
  74. expect(flyout.getByText('Hello world!')).toBeInTheDocument()
  75. expect(flyout.getByText('Foo header')).toBeInTheDocument()
  76. expect(flyout.getByText('Foo submit')).toBeInTheDocument()
  77. })
  78. it('focuses the first focusable element when opened', async () => {
  79. const flyout = renderComponent(CommonFlyout, {
  80. props: {
  81. headerTitle: 'Test Title',
  82. name: 'test-identifier',
  83. showBackdrop: false,
  84. },
  85. slots: {
  86. default: html`<input
  87. type="text"
  88. placeholder="test"
  89. name="test-input"
  90. />`,
  91. },
  92. })
  93. await nextTick()
  94. expect(flyout.getByPlaceholderText('test')).toHaveFocus()
  95. })
  96. it('has a default container width of 500px', async () => {
  97. expect(flyout.getByRole('complementary')).toHaveStyle({
  98. width: '500px',
  99. })
  100. })
  101. describe('events', () => {
  102. it('emits close event when cancel button is clicked', async () => {
  103. await flyout.events.click(flyout.getByText('Cancel & Go Back'))
  104. expect(flyout.emitted('close')).toHaveLength(1)
  105. })
  106. it('emits close event when x button is clicked', async () => {
  107. await flyout.events.click(
  108. flyout.getAllByLabelText('Close side panel').at(-1) as HTMLElement,
  109. )
  110. expect(flyout.emitted('close')).toHaveLength(1)
  111. })
  112. it('emits close event when escape key is pressed, by default', async () => {
  113. await flyout.events.keyboard('{Escape}')
  114. expect(flyout.emitted('close')).toHaveLength(1)
  115. })
  116. it('emits close event when escape key is pressed, if specified', async () => {
  117. await flyout.rerender({ noCloseOnEscape: true })
  118. await flyout.events.keyboard('{Escape}')
  119. expect(flyout.emitted('close')).toBeUndefined()
  120. })
  121. it('emits event when action button is clicked', async () => {
  122. await flyout.events.click(flyout.getByText('Update'))
  123. expect(flyout.emitted('action')).toHaveLength(1)
  124. })
  125. })
  126. })
  127. })