DynamicInitializer.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { render } from '@testing-library/vue'
  3. import { h, nextTick, ref } from 'vue'
  4. import DynamicInitializer from '../DynamicInitializer.vue'
  5. import { destroyComponent, pushComponent } from '../manage.ts'
  6. describe('dynamicaly add components to dom', () => {
  7. test('adds and destroys components', async () => {
  8. const view = render(DynamicInitializer, {
  9. props: {
  10. name: 'dialog',
  11. },
  12. })
  13. await pushComponent('dialog', '1', () => h('div', 'Hello, World!'))
  14. expect(view.container).toHaveTextContent('Hello, World!')
  15. await destroyComponent('dialog', '1')
  16. expect(view.container).not.toHaveTextContent('Hello, World!')
  17. })
  18. test("doesn't add other components", async () => {
  19. const view = render(DynamicInitializer, {
  20. props: {
  21. name: 'dialog',
  22. },
  23. })
  24. await pushComponent('not-dialog', '1', () => h('div', 'Hello, World!'))
  25. expect(view.container).not.toHaveTextContent('Hello, World!')
  26. })
  27. test('can pass down reactive variables', async () => {
  28. const view = render(DynamicInitializer, {
  29. props: {
  30. name: 'dialog',
  31. },
  32. })
  33. const name = ref('dialog')
  34. await pushComponent(
  35. 'dialog',
  36. '1',
  37. (props) => h('div', `Hello, ${props.name}!`),
  38. { name },
  39. )
  40. expect(view.container).toHaveTextContent('Hello, dialog!')
  41. name.value = 'world'
  42. await nextTick()
  43. expect(view.container).toHaveTextContent('Hello, world!')
  44. })
  45. })