| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import {
- NodeOpTypes,
- type TestElement,
- TestNodeTypes,
- type TestText,
- dumpOps,
- h,
- nextTick,
- nodeOps,
- reactive,
- ref,
- render,
- resetOps,
- serialize,
- triggerEvent,
- } from '../src'
- describe('test renderer', () => {
- it('should work', () => {
- const root = nodeOps.createElement('div')
- render(
- h(
- 'div',
- {
- id: 'test',
- },
- 'hello',
- ),
- root,
- )
- expect(root.children.length).toBe(1)
- const el = root.children[0] as TestElement
- expect(el.type).toBe(TestNodeTypes.ELEMENT)
- expect(el.props.id).toBe('test')
- expect(el.children.length).toBe(1)
- const text = el.children[0] as TestText
- expect(text.type).toBe(TestNodeTypes.TEXT)
- expect(text.text).toBe('hello')
- })
- it('should record ops', async () => {
- const state = reactive({
- id: 'test',
- text: 'hello',
- })
- const App = {
- render() {
- return h(
- 'div',
- {
- id: state.id,
- },
- state.text,
- )
- },
- }
- const root = nodeOps.createElement('div')
- resetOps()
- render(h(App), root)
- const ops = dumpOps()
- expect(ops.length).toBe(4)
- expect(ops[0]).toEqual({
- type: NodeOpTypes.CREATE,
- nodeType: TestNodeTypes.ELEMENT,
- tag: 'div',
- targetNode: root.children[0],
- })
- expect(ops[1]).toEqual({
- type: NodeOpTypes.SET_ELEMENT_TEXT,
- text: 'hello',
- targetNode: root.children[0],
- })
- expect(ops[2]).toEqual({
- type: NodeOpTypes.PATCH,
- targetNode: root.children[0],
- propKey: 'id',
- propPrevValue: null,
- propNextValue: 'test',
- })
- expect(ops[3]).toEqual({
- type: NodeOpTypes.INSERT,
- targetNode: root.children[0],
- parentNode: root,
- refNode: null,
- })
- // test update ops
- state.id = 'foo'
- state.text = 'bar'
- await nextTick()
- const updateOps = dumpOps()
- expect(updateOps.length).toBe(2)
- expect(updateOps[0]).toEqual({
- type: NodeOpTypes.SET_ELEMENT_TEXT,
- targetNode: root.children[0],
- text: 'bar',
- })
- expect(updateOps[1]).toEqual({
- type: NodeOpTypes.PATCH,
- targetNode: root.children[0],
- propKey: 'id',
- propPrevValue: 'test',
- propNextValue: 'foo',
- })
- })
- it('should be able to serialize nodes', () => {
- const App = {
- render() {
- return h(
- 'div',
- {
- id: 'test',
- boolean: '',
- },
- [h('span', 'foo'), 'hello'],
- )
- },
- }
- const root = nodeOps.createElement('div')
- render(h(App), root)
- expect(serialize(root)).toEqual(
- `<div><div id="test" boolean><span>foo</span>hello</div></div>`,
- )
- // indented output
- expect(serialize(root, 2)).toEqual(
- `<div>
- <div id="test" boolean>
- <span>
- foo
- </span>
- hello
- </div>
- </div>`,
- )
- })
- it('should be able to trigger events', async () => {
- const count = ref(0)
- const App = () => {
- return h(
- 'span',
- {
- onClick: () => {
- count.value++
- },
- },
- count.value,
- )
- }
- const root = nodeOps.createElement('div')
- render(h(App), root)
- triggerEvent(root.children[0] as TestElement, 'click')
- expect(count.value).toBe(1)
- await nextTick()
- expect(serialize(root)).toBe(`<div><span>1</span></div>`)
- })
- it('should be able to trigger events with multiple listeners', async () => {
- const count = ref(0)
- const count2 = ref(1)
- const App = () => {
- return h(
- 'span',
- {
- onClick: [
- () => {
- count.value++
- },
- () => {
- count2.value++
- },
- ],
- },
- `${count.value}, ${count2.value}`,
- )
- }
- const root = nodeOps.createElement('div')
- render(h(App), root)
- triggerEvent(root.children[0] as TestElement, 'click')
- expect(count.value).toBe(1)
- expect(count2.value).toBe(2)
- await nextTick()
- expect(serialize(root)).toBe(`<div><span>1, 2</span></div>`)
- })
- })
|