testRenderer.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. h,
  3. render,
  4. Component,
  5. nodeOps,
  6. NodeTypes,
  7. TestElement,
  8. TestText,
  9. dumpOps,
  10. NodeOpTypes,
  11. nextTick,
  12. observable,
  13. resetOps,
  14. serialize
  15. } from '../src'
  16. describe('test renderer', () => {
  17. it('should work', () => {
  18. class App extends Component {
  19. render() {
  20. return h(
  21. 'div',
  22. {
  23. id: 'test'
  24. },
  25. 'hello'
  26. )
  27. }
  28. }
  29. const root = nodeOps.createElement('div')
  30. render(h(App), root)
  31. expect(root.children.length).toBe(1)
  32. const el = root.children[0] as TestElement
  33. expect(el.type).toBe(NodeTypes.ELEMENT)
  34. expect(el.props.id).toBe('test')
  35. expect(el.children.length).toBe(1)
  36. const text = el.children[0] as TestText
  37. expect(text.type).toBe(NodeTypes.TEXT)
  38. expect(text.text).toBe('hello')
  39. })
  40. it('should record ops', async () => {
  41. const state = observable({
  42. id: 'test',
  43. text: 'hello'
  44. })
  45. class App extends Component {
  46. render() {
  47. return h(
  48. 'div',
  49. {
  50. id: state.id
  51. },
  52. state.text
  53. )
  54. }
  55. }
  56. const root = nodeOps.createElement('div')
  57. resetOps()
  58. render(h(App), root)
  59. const ops = dumpOps()
  60. expect(ops.length).toBe(5)
  61. expect(ops[0]).toEqual({
  62. type: NodeOpTypes.CREATE,
  63. nodeType: NodeTypes.ELEMENT,
  64. tag: 'div',
  65. targetNode: root.children[0]
  66. })
  67. expect(ops[1]).toEqual({
  68. type: NodeOpTypes.PATCH,
  69. targetNode: root.children[0],
  70. propKey: 'id',
  71. propPrevValue: null,
  72. propNextValue: 'test'
  73. })
  74. expect(ops[2]).toEqual({
  75. type: NodeOpTypes.CREATE,
  76. nodeType: NodeTypes.TEXT,
  77. text: 'hello',
  78. targetNode: (root.children[0] as TestElement).children[0]
  79. })
  80. expect(ops[3]).toEqual({
  81. type: NodeOpTypes.APPEND,
  82. targetNode: (root.children[0] as TestElement).children[0],
  83. parentNode: root.children[0]
  84. })
  85. expect(ops[4]).toEqual({
  86. type: NodeOpTypes.APPEND,
  87. targetNode: root.children[0],
  88. parentNode: root
  89. })
  90. // test update ops
  91. state.id = 'foo'
  92. state.text = 'bar'
  93. await nextTick()
  94. const updateOps = dumpOps()
  95. expect(updateOps.length).toBe(2)
  96. expect(updateOps[0]).toEqual({
  97. type: NodeOpTypes.PATCH,
  98. targetNode: root.children[0],
  99. propKey: 'id',
  100. propPrevValue: 'test',
  101. propNextValue: 'foo'
  102. })
  103. expect(updateOps[1]).toEqual({
  104. type: NodeOpTypes.SET_TEXT,
  105. targetNode: (root.children[0] as TestElement).children[0],
  106. text: 'bar'
  107. })
  108. })
  109. it('should be able to serialize nodes', () => {
  110. class App extends Component {
  111. render() {
  112. return h(
  113. 'div',
  114. {
  115. id: 'test'
  116. },
  117. [h('span', 'foo'), 'hello']
  118. )
  119. }
  120. }
  121. const root = nodeOps.createElement('div')
  122. render(h(App), root)
  123. expect(serialize(root)).toEqual(
  124. `<div><div id="test"><span>foo</span>hello</div></div>`
  125. )
  126. expect(serialize(root, 2)).toEqual(
  127. `<div>
  128. <div id="test">
  129. <span>
  130. foo
  131. </span>
  132. hello
  133. </div>
  134. </div>`
  135. )
  136. })
  137. })