ssrAttrFallthrough.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @jest-environment node
  3. */
  4. import { createApp } from 'vue'
  5. import { renderToString } from '../src/renderToString'
  6. describe('ssr: attr fallthrough', () => {
  7. test('basic', async () => {
  8. const Child = {
  9. template: `<div class="foo" />`
  10. }
  11. const Parent = {
  12. components: { Child },
  13. template: `<child class="bar"/>`
  14. }
  15. const app = createApp(Parent)
  16. expect(await renderToString(app)).toBe(`<div class="foo bar"></div>`)
  17. })
  18. test('with v-if', async () => {
  19. const Child = {
  20. props: ['ok'],
  21. template: `<div v-if="ok" class="foo" /><span v-else />`
  22. }
  23. const Parent = {
  24. props: ['ok'],
  25. components: { Child },
  26. template: `<child :ok="ok" class="bar"/>`
  27. }
  28. expect(await renderToString(createApp(Parent, { ok: true }))).toBe(
  29. `<div class="foo bar"></div>`
  30. )
  31. expect(await renderToString(createApp(Parent, { ok: false }))).toBe(
  32. `<span class="bar"></span>`
  33. )
  34. })
  35. test('with v-model', async () => {
  36. const Child = {
  37. props: ['text'],
  38. template: `<input v-model="text">`
  39. }
  40. const Parent = {
  41. components: { Child },
  42. template: `<child text="hello" class="bar"/>`
  43. }
  44. expect(await renderToString(createApp(Parent))).toBe(
  45. `<input class="bar" value="hello">`
  46. )
  47. })
  48. test('with v-bind', async () => {
  49. const Child = {
  50. props: ['obj'],
  51. template: `<div v-bind="obj" />`
  52. }
  53. const Parent = {
  54. components: { Child },
  55. template: `<child :obj="{ class: 'foo' }" class="bar"/>`
  56. }
  57. expect(await renderToString(createApp(Parent))).toBe(
  58. `<div class="foo bar"></div>`
  59. )
  60. })
  61. test('nested fallthrough', async () => {
  62. const Child = {
  63. props: ['id'],
  64. template: `<div :id="id"></div>`
  65. }
  66. const Parent = {
  67. components: { Child },
  68. template: `<child id="foo" class="bar"/>`
  69. }
  70. // pass to parent, fallthrough to child and merge
  71. const app = createApp(Parent, { class: 'baz' })
  72. expect(await renderToString(app)).toBe(
  73. `<div id="foo" class="bar baz"></div>`
  74. )
  75. })
  76. })