testRuntime.spec.ts 3.6 KB

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