diff --git a/packages/compiler-core/__tests__/transforms/vIf.spec.ts b/packages/compiler-core/__tests__/transforms/vIf.spec.ts index 6339f4de2..aa56d175a 100644 --- a/packages/compiler-core/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-core/__tests__/transforms/vIf.spec.ts @@ -9,14 +9,25 @@ import { CommentNode } from '../../src/ast' import { ErrorCodes } from '../../src/errors' +import { CompilerOptions } from '../../src' + +function transformWithIf( + template: string, + options: CompilerOptions = {}, + returnIndex: number = 0 +): IfNode { + const node = parse(template, options) + transform(node, { nodeTransforms: [transformIf], ...options }) + if (!options.onError) { + expect(node.children.length).toBe(1) + expect(node.children[0].type).toBe(NodeTypes.IF) + } + return node.children[returnIndex] as IfNode +} describe('compiler: transform v-if', () => { test('basic v-if', () => { - const ast = parse(`
`) - transform(ast, { - nodeTransforms: [transformIf] - }) - const node = ast.children[0] as IfNode + const node = transformWithIf(`
`) expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(1) expect(node.branches[0].condition!.content).toBe(`ok`) @@ -26,11 +37,9 @@ describe('compiler: transform v-if', () => { }) test('template v-if', () => { - const ast = parse(``) - transform(ast, { - nodeTransforms: [transformIf] - }) - const node = ast.children[0] as IfNode + const node = transformWithIf( + `` + ) expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(1) expect(node.branches[0].condition!.content).toBe(`ok`) @@ -44,14 +53,7 @@ describe('compiler: transform v-if', () => { }) test('v-if + v-else', () => { - const ast = parse(`

`) - transform(ast, { - nodeTransforms: [transformIf] - }) - // should fold branches - expect(ast.children.length).toBe(1) - - const node = ast.children[0] as IfNode + const node = transformWithIf(`

`) expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(2) @@ -69,14 +71,7 @@ describe('compiler: transform v-if', () => { }) test('v-if + v-else-if', () => { - const ast = parse(`

`) - transform(ast, { - nodeTransforms: [transformIf] - }) - // should fold branches - expect(ast.children.length).toBe(1) - - const node = ast.children[0] as IfNode + const node = transformWithIf(`

`) expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(2) @@ -94,16 +89,9 @@ describe('compiler: transform v-if', () => { }) test('v-if + v-else-if + v-else', () => { - const ast = parse( + const node = transformWithIf( `

` ) - transform(ast, { - nodeTransforms: [transformIf] - }) - // should fold branches - expect(ast.children.length).toBe(1) - - const node = ast.children[0] as IfNode expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(3) @@ -127,20 +115,13 @@ describe('compiler: transform v-if', () => { }) test('comment between branches', () => { - const ast = parse(` + const node = transformWithIf(`

`) - transform(ast, { - nodeTransforms: [transformIf] - }) - // should fold branches - expect(ast.children.length).toBe(1) - - const node = ast.children[0] as IfNode expect(node.type).toBe(NodeTypes.IF) expect(node.branches.length).toBe(3) @@ -168,83 +149,65 @@ describe('compiler: transform v-if', () => { }) test('error on v-else missing adjacent v-if', () => { - const ast = parse(`

`) - const spy = jest.fn() - transform(ast, { - nodeTransforms: [transformIf], - onError: spy - }) - expect(spy.mock.calls[0]).toMatchObject([ + const onError = jest.fn() + + const node1 = transformWithIf(`
`, { onError }) + expect(onError.mock.calls[0]).toMatchObject([ { code: ErrorCodes.X_ELSE_NO_ADJACENT_IF, - loc: ast.children[0].loc + loc: node1.loc } ]) - const ast2 = parse(`
`) - const spy2 = jest.fn() - transform(ast2, { - nodeTransforms: [transformIf], - onError: spy2 - }) - expect(spy2.mock.calls[0]).toMatchObject([ + const node2 = transformWithIf(`
`, { onError }, 1) + expect(onError.mock.calls[1]).toMatchObject([ { code: ErrorCodes.X_ELSE_NO_ADJACENT_IF, - loc: ast2.children[1].loc + loc: node2.loc } ]) - const ast3 = parse(`
foo
`) - const spy3 = jest.fn() - transform(ast3, { - nodeTransforms: [transformIf], - onError: spy3 - }) - expect(spy3.mock.calls[0]).toMatchObject([ + const node3 = transformWithIf(`
foo
`, { onError }, 2) + expect(onError.mock.calls[2]).toMatchObject([ { code: ErrorCodes.X_ELSE_NO_ADJACENT_IF, - loc: ast3.children[2].loc + loc: node3.loc } ]) }) test('error on v-else-if missing adjacent v-if', () => { - const ast = parse(`
`) - const spy = jest.fn() - transform(ast, { - nodeTransforms: [transformIf], - onError: spy - }) - expect(spy.mock.calls[0]).toMatchObject([ + const onError = jest.fn() + + const node1 = transformWithIf(`
`, { onError }) + expect(onError.mock.calls[0]).toMatchObject([ { code: ErrorCodes.X_ELSE_IF_NO_ADJACENT_IF, - loc: ast.children[0].loc + loc: node1.loc } ]) - const ast2 = parse(`
`) - const spy2 = jest.fn() - transform(ast2, { - nodeTransforms: [transformIf], - onError: spy2 - }) - expect(spy2.mock.calls[0]).toMatchObject([ + const node2 = transformWithIf( + `
`, + { onError }, + 1 + ) + expect(onError.mock.calls[1]).toMatchObject([ { code: ErrorCodes.X_ELSE_IF_NO_ADJACENT_IF, - loc: ast2.children[1].loc + loc: node2.loc } ]) - const ast3 = parse(`
foo
`) - const spy3 = jest.fn() - transform(ast3, { - nodeTransforms: [transformIf], - onError: spy3 - }) - expect(spy3.mock.calls[0]).toMatchObject([ + const node3 = transformWithIf( + `
foo
`, + { onError }, + 2 + ) + expect(onError.mock.calls[2]).toMatchObject([ { code: ErrorCodes.X_ELSE_IF_NO_ADJACENT_IF, - loc: ast3.children[2].loc + loc: node3.loc } ]) })