All files / runtime-test/src nodeOps.ts

93.08% Statements 175/188
90% Branches 27/30
92.85% Functions 13/14
93.08% Lines 175/188

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 2643x   3x 3x 3x 3x     3x 3x 3x 3x 3x 3x 3x                                                                                   3x 3x   3x 7129x 7129x   3x 13x 13x   3x 7x 7x 7x 7x   1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x 1708x   1708x 1708x 1708x   678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x 678x   678x 678x 678x   523x 523x 523x 523x 523x 523x 523x 523x 523x 523x 523x 523x 523x   523x 523x 523x   55x 55x 55x 55x 55x 55x 55x 55x   2505x 2505x 2505x 2505x 2505x 2505x 2505x 632x 632x         632x 2505x 2505x 2505x 2505x 2505x 2505x   2505x   2505x 2505x 1873x 1873x 2503x 632x 632x 632x 2505x   3164x 3164x 3164x 1086x 659x 659x 659x 659x 659x 659x 1086x 1086x 1086x 1086x         1086x 1086x 3164x   686x 686x 686x 686x 686x 686x 686x 99x 686x 686x 42x 686x 644x 644x 644x 644x 644x 644x 644x 644x 644x 686x   849x 849x 849x   1253x 1253x 1253x     1253x 1253x 1253x           52x 52x 52x   3x                       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
import { markRaw } from '@vue/reactivity'
 
export enum TestNodeTypes {
  TEXT = 'text',
  ELEMENT = 'element',
  COMMENT = 'comment',
}
 
export enum NodeOpTypes {
  CREATE = 'create',
  INSERT = 'insert',
  REMOVE = 'remove',
  SET_TEXT = 'setText',
  SET_ELEMENT_TEXT = 'setElementText',
  PATCH = 'patch',
}
 
export interface TestElement {
  id: number
  type: TestNodeTypes.ELEMENT
  parentNode: TestElement | null
  tag: string
  children: TestNode[]
  props: Record<string, any>
  eventListeners: Record<string, Function | Function[]> | null
}
 
export interface TestText {
  id: number
  type: TestNodeTypes.TEXT
  parentNode: TestElement | null
  text: string
}
 
export interface TestComment {
  id: number
  type: TestNodeTypes.COMMENT
  parentNode: TestElement | null
  text: string
}
 
export type TestNode = TestElement | TestText | TestComment
 
export interface NodeOp {
  type: NodeOpTypes
  nodeType?: TestNodeTypes
  tag?: string
  text?: string
  targetNode?: TestNode
  parentNode?: TestElement
  refNode?: TestNode | null
  propKey?: string
  propPrevValue?: any
  propNextValue?: any
}
 
let nodeId: number = 0
let recordedNodeOps: NodeOp[] = []
 
export function logNodeOp(op: NodeOp): void {
  recordedNodeOps.push(op)
}
 
export function resetOps(): void {
  recordedNodeOps = []
}
 
export function dumpOps(): NodeOp[] {
  const ops = recordedNodeOps.slice()
  resetOps()
  return ops
}
 
function createElement(tag: string): TestElement {
  const node: TestElement = {
    id: nodeId++,
    type: TestNodeTypes.ELEMENT,
    tag,
    children: [],
    props: {},
    parentNode: null,
    eventListeners: null,
  }
  logNodeOp({
    type: NodeOpTypes.CREATE,
    nodeType: TestNodeTypes.ELEMENT,
    targetNode: node,
    tag,
  })
  // avoid test nodes from being observed
  markRaw(node)
  return node
}
 
function createText(text: string): TestText {
  const node: TestText = {
    id: nodeId++,
    type: TestNodeTypes.TEXT,
    text,
    parentNode: null,
  }
  logNodeOp({
    type: NodeOpTypes.CREATE,
    nodeType: TestNodeTypes.TEXT,
    targetNode: node,
    text,
  })
  // avoid test nodes from being observed
  markRaw(node)
  return node
}
 
function createComment(text: string): TestComment {
  const node: TestComment = {
    id: nodeId++,
    type: TestNodeTypes.COMMENT,
    text,
    parentNode: null,
  }
  logNodeOp({
    type: NodeOpTypes.CREATE,
    nodeType: TestNodeTypes.COMMENT,
    targetNode: node,
    text,
  })
  // avoid test nodes from being observed
  markRaw(node)
  return node
}
 
function setText(node: TestText, text: string): void {
  logNodeOp({
    type: NodeOpTypes.SET_TEXT,
    targetNode: node,
    text,
  })
  node.text = text
}
 
function insert(
  child: TestNode,
  parent: TestElement,
  ref?: TestNode | null,
): void {
  let refIndex
  if (ref) {
    refIndex = parent.children.indexOf(ref)
    if (refIndex === -1) {
      console.error('ref: ', ref)
      console.error('parent: ', parent)
      throw new Error('ref is not a child of parent')
    }
  }
  logNodeOp({
    type: NodeOpTypes.INSERT,
    targetNode: child,
    parentNode: parent,
    refNode: ref,
  })
  // remove the node first, but don't log it as a REMOVE op
  remove(child, false)
  // re-calculate the ref index because the child's removal may have affected it
  refIndex = ref ? parent.children.indexOf(ref) : -1
  if (refIndex === -1) {
    parent.children.push(child)
    child.parentNode = parent
  } else {
    parent.children.splice(refIndex, 0, child)
    child.parentNode = parent
  }
}
 
function remove(child: TestNode, logOp = true): void {
  const parent = child.parentNode
  if (parent) {
    if (logOp) {
      logNodeOp({
        type: NodeOpTypes.REMOVE,
        targetNode: child,
        parentNode: parent,
      })
    }
    const i = parent.children.indexOf(child)
    if (i > -1) {
      parent.children.splice(i, 1)
    } else {
      console.error('target: ', child)
      console.error('parent: ', parent)
      throw Error('target is not a childNode of parent')
    }
    child.parentNode = null
  }
}
 
function setElementText(el: TestElement, text: string): void {
  logNodeOp({
    type: NodeOpTypes.SET_ELEMENT_TEXT,
    targetNode: el,
    text,
  })
  el.children.forEach(c => {
    c.parentNode = null
  })
  if (!text) {
    el.children = []
  } else {
    el.children = [
      {
        id: nodeId++,
        type: TestNodeTypes.TEXT,
        text,
        parentNode: el,
      },
    ]
  }
}
 
function parentNode(node: TestNode): TestElement | null {
  return node.parentNode
}
 
function nextSibling(node: TestNode): TestNode | null {
  const parent = node.parentNode
  if (!parent) {
    return null
  }
  const i = parent.children.indexOf(node)
  return parent.children[i + 1] || null
}
 
function querySelector(): never {
  throw new Error('querySelector not supported in test renderer.')
}
 
function setScopeId(el: TestElement, id: string): void {
  el.props[id] = ''
}
 
export const nodeOps: {
  insert: typeof insert
  remove: typeof remove
  createElement: typeof createElement
  createText: typeof createText
  createComment: typeof createComment
  setText: typeof setText
  setElementText: typeof setElementText
  parentNode: typeof parentNode
  nextSibling: typeof nextSibling
  querySelector: typeof querySelector
  setScopeId: typeof setScopeId
} = {
  insert,
  remove,
  createElement,
  createText,
  createComment,
  setText,
  setElementText,
  parentNode,
  nextSibling,
  querySelector,
  setScopeId,
}