vIf.spec.ts 8.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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
import { ErrorCodes, IfNode, NodeTypes } from '@vue/compiler-core'
import { compile } from '../src'
import { CompilerOptions } from '../src/options'
import { assert } from './testUtils'

function compileWithIfTransform(
  template: string,
  options: CompilerOptions = {},
  returnIndex: number = 0,
  childrenLen: number = 1
) {
  const { ast } = compile(template, options)
  if (!options.onError) {
    expect(ast.children.length).toBe(childrenLen)
    for (let i = 0; i < childrenLen; i++) {
      expect(ast.children[i].type).toBe(NodeTypes.IF)
    }
  }
  return {
    root: ast,
    node: ast.children[returnIndex] as IfNode,
  }
}

describe(`compiler: v-if`, () => {
  describe(`codegen`, () => {
    test(`basic v-if`, () => {
      assert(
        `<view v-if="ok"/>`,
        `<view wx:if="{{a}}"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
32
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40
}`
      )
    })
    test(`template v-if`, () => {
      assert(
        `<template v-if="ok"><view/>hello<view/></template>`,
        `<block wx:if="{{a}}"><view/>hello<view/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
41
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49
}`
      )
    })
    test(`template v-if w/ single <slot/> child`, () => {
      assert(
        `<template v-if="ok"><slot/></template>`,
        `<block wx:if="{{a}}"><slot/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
50
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58
}`
      )
    })
    test(`v-if on <slot/>`, () => {
      assert(
        `<slot v-if="ok"/>`,
        `<slot wx:if="{{a}}"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
59
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
fxy060608's avatar
fxy060608 已提交
60 61 62 63
}`
      )
    })
    test(`component v-if`, () => {
64 65 66 67
      //       assert(
      //         `<Component v-if="ok"></Component>`,
      //         `<Component wx:if="{{a}}"></Component>`,
      //         `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
68
      //   return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
69 70
      // }`
      //       )
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76
    })
    test(`v-if + v-else`, () => {
      assert(
        `<view v-if="ok"/><view v-else/>`,
        `<view wx:if="{{a}}"/><view wx:else/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
77
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : {})
fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83 84 85
}`
      )
    })
    test(`v-if + v-else-if`, () => {
      assert(
        `<view v-if="ok"/><view v-else-if="orNot"/>`,
        `<view wx:if="{{a}}"/><view wx:elif="{{b}}"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
86
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot })
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92 93 94
}`
      )
    })
    test(`v-if + v-else-if + v-else`, () => {
      assert(
        `<view v-if="ok"/><view v-else-if="orNot"/><template v-else>fine</template>`,
        `<view wx:if="{{a}}"/><view wx:elif="{{b}}"/><block wx:else>fine</block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
95
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot })
fxy060608's avatar
fxy060608 已提交
96 97 98
}`
      )
    })
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    test(`multiple v-if that are sibling nodes should have different keys`, () => {
      // <div v-if="ok"/><p v-if="orNot"/>
    })
    test(`increasing key: v-if + v-else-if + v-else`, () => {
      // <div v-if="ok"/><p v-else/><div v-if="another"/><p v-else-if="orNot"/><p v-else/>
    })
    test(`key injection (only v-bind)`, () => {
      // <div v-if="ok" v-bind="obj"/>
    })
    test(`key injection (before v-bind)`, () => {
      // <div v-if="ok" id="foo" v-bind="obj"/>
    })
    test(`key injection (after v-bind)`, () => {
      // <div v-if="ok" v-bind="obj" id="foo"/>
    })
    test(`key injection (w/ custom directive)`, () => {
      // <div v-if="ok" v-foo />
    })
fxy060608's avatar
fxy060608 已提交
117 118 119 120 121
    test(`v-if + v-else-if + v-else-if + v-else`, () => {
      assert(
        `<view v-if="ok"/><view v-else-if="orNot"/><view v-else-if="3"/><template v-else>fine</template>`,
        `<view wx:if="{{a}}"/><view wx:elif="{{b}}"/><view wx:elif="{{3}}"/><block wx:else>fine</block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
122
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : 3 ? {} : {}, { b: _ctx.orNot })
fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
}`
      )
    })
    test(`comment between branches`, () => {
      assert(
        `
        <view v-if="ok"/>
        <!--foo-->
        <view v-else-if="orNot"/>
        <!--bar-->
        <template v-else>fine</template>
      `,
        `<view wx:if="{{a}}"/><view wx:elif="{{b}}"/><block wx:else>fine</block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
137
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot })
fxy060608's avatar
fxy060608 已提交
138 139 140
}`
      )
    })
fxy060608's avatar
fxy060608 已提交
141 142 143 144 145
    test(`with spaces between branches`, () => {
      assert(
        `<view v-if="ok"/> <view v-else-if="no"/> <view v-else/>`,
        `<view wx:if="{{a}}"/><view wx:elif="{{b}}"/><view wx:else/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
146
  return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.no ? {} : {}, { b: _ctx.no })
fxy060608's avatar
fxy060608 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}`
      )
    })
    test(`with comments`, () => {
      assert(
        `
        <template v-if="ok">
        <!--comment1-->
        <view v-if="ok2">
          <!--comment2-->
        </view>
        <!--comment3-->
        <view v-else/>
        <!--comment4-->
        <view/>
      </template>
        `,
fxy060608's avatar
fxy060608 已提交
164
        `<block wx:if="{{a}}"><view wx:if="{{b}}"></view><view wx:else/><view/></block>`,
fxy060608's avatar
fxy060608 已提交
165
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
166
  return _e({ a: _ctx.ok }, _ctx.ok ? _e({ b: _ctx.ok2 }, _ctx.ok2 ? {} : {}) : {})
fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172
}`
      )
    })
    test(`v-on with v-if`, () => {
      // <button v-on="{ click: clickEvent }" v-if="true">w/ v-if</button>
    })
fxy060608's avatar
fxy060608 已提交
173
  })
fxy060608's avatar
fxy060608 已提交
174

fxy060608's avatar
fxy060608 已提交
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  describe('errors', () => {
    test('error on v-else missing adjacent v-if', () => {
      const onError = jest.fn()

      const { node: node1 } = compileWithIfTransform(`<view v-else/>`, {
        onError,
      })
      expect(onError.mock.calls[0]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node1.loc,
        },
      ])

      const { node: node2 } = compileWithIfTransform(
        `<view/><view v-else/>`,
        { onError },
        1
      )
      expect(onError.mock.calls[1]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node2.loc,
        },
      ])

      const { node: node3 } = compileWithIfTransform(
        `<view/>foo<view v-else/>`,
        { onError },
        2
      )
      expect(onError.mock.calls[2]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node3.loc,
        },
      ])
    })

    test('error on v-else-if missing adjacent v-if or v-else-if', () => {
      const onError = jest.fn()

      const { node: node1 } = compileWithIfTransform(
        `<view v-else-if="foo"/>`,
        {
          onError,
        }
      )
      expect(onError.mock.calls[0]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node1.loc,
        },
      ])

      const { node: node2 } = compileWithIfTransform(
        `<view/><view v-else-if="foo"/>`,
        { onError },
        1
      )
      expect(onError.mock.calls[1]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node2.loc,
        },
      ])

      const { node: node3 } = compileWithIfTransform(
        `<view/>foo<view v-else-if="foo"/>`,
        { onError },
        2
      )
      expect(onError.mock.calls[2]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: node3.loc,
        },
      ])

      const {
        node: { branches },
      } = compileWithIfTransform(
        `<view v-if="notOk"/><view v-else/><view v-else-if="ok"/>`,
        { onError },
        0
      )

      expect(onError.mock.calls[3]).toMatchObject([
        {
          code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
          loc: branches[branches.length - 1].loc,
        },
      ])
    })

    // test('error on user key', () => {
    //   const onError = jest.fn()
    //   // dynamic
    //   compileWithIfTransform(
    //     `<view v-if="ok" :key="a + 1" /><view v-else :key="a + 1" />`,
    //     { onError }
    //   )
    //   expect(onError.mock.calls[0]).toMatchObject([
    //     {
    //       code: ErrorCodes.X_V_IF_SAME_KEY,
    //     },
    //   ])
    //   // static
    //   compileWithIfTransform(
    //     `<view v-if="ok" key="1" /><view v-else key="1" />`,
    //     {
    //       onError,
    //     }
    //   )
    //   expect(onError.mock.calls[1]).toMatchObject([
    //     {
    //       code: ErrorCodes.X_V_IF_SAME_KEY,
    //     },
    //   ])
    // })
  })
})