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, { generatorOpts: { concise: true, }, ...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( ``, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) }` ) }) test(`template v-if`, () => { assert( ``, `hello`, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) }` ) }) test(`template v-if w/ single child`, () => { assert( ``, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) }` ) }) test(`v-if on `, () => { assert( ``, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) }` ) }) test(`component v-if`, () => { // assert( // ``, // ``, // `(_ctx, _cache) => { // return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) // }` // ) }) test(`v-if + v-else`, () => { assert( ``, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : {}) }` ) }) test(`v-if + v-else-if`, () => { assert( ``, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot }) }` ) }) test(`v-if + v-else-if + v-else`, () => { assert( ``, `fine`, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot }) }` ) }) test(`multiple v-if that are sibling nodes should have different keys`, () => { //

}) test(`increasing key: v-if + v-else-if + v-else`, () => { //

}) test(`key injection (only v-bind)`, () => { //

}) test(`key injection (before v-bind)`, () => { //
}) test(`key injection (after v-bind)`, () => { //
}) test(`key injection (w/ custom directive)`, () => { //
}) test(`v-if + v-else-if + v-else-if + v-else`, () => { assert( ``, `fine`, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : 3 ? {} : {}, { b: _ctx.orNot }) }` ) }) test(`comment between branches`, () => { assert( ` `, `fine`, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.orNot ? {} : {}, { b: _ctx.orNot }) }` ) }) test(`with spaces between branches`, () => { assert( ` `, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? {} : _ctx.no ? {} : {}, { b: _ctx.no }) }` ) }) test(`with comments`, () => { assert( ` `, ``, `(_ctx, _cache) => { return _e({ a: _ctx.ok }, _ctx.ok ? _e({ b: _ctx.ok2 }, _ctx.ok2 ? {} : {}) : {}) }` ) }) test(`v-on with v-if`, () => { // }) }) describe('errors', () => { test('error on v-else missing adjacent v-if', () => { const onError = jest.fn() const { node: node1 } = compileWithIfTransform(``, { onError, }) expect(onError.mock.calls[0]).toMatchObject([ { code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, loc: node1.loc, }, ]) const { node: node2 } = compileWithIfTransform( ``, { onError }, 1 ) expect(onError.mock.calls[1]).toMatchObject([ { code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, loc: node2.loc, }, ]) const { node: node3 } = compileWithIfTransform( `foo`, { 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( ``, { onError, } ) expect(onError.mock.calls[0]).toMatchObject([ { code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, loc: node1.loc, }, ]) const { node: node2 } = compileWithIfTransform( ``, { onError }, 1 ) expect(onError.mock.calls[1]).toMatchObject([ { code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, loc: node2.loc, }, ]) const { node: node3 } = compileWithIfTransform( `foo`, { onError }, 2 ) expect(onError.mock.calls[2]).toMatchObject([ { code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF, loc: node3.loc, }, ]) const { node: { branches }, } = compileWithIfTransform( ``, { 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( // ``, // { onError } // ) // expect(onError.mock.calls[0]).toMatchObject([ // { // code: ErrorCodes.X_V_IF_SAME_KEY, // }, // ]) // // static // compileWithIfTransform( // ``, // { // onError, // } // ) // expect(onError.mock.calls[1]).toMatchObject([ // { // code: ErrorCodes.X_V_IF_SAME_KEY, // }, // ]) // }) }) })