import { compile, NodeTypes, CREATE_STATIC } from '../../src' import { stringifyStatic, StringifyThresholds } from '../../src/transforms/stringifyStatic' describe('stringify static html', () => { function compileWithStringify(template: string) { return compile(template, { hoistStatic: true, prefixIdentifiers: true, transformHoist: stringifyStatic }) } function repeat(code: string, n: number): string { return new Array(n) .fill(0) .map(() => code) .join('') } test('should bail on non-eligible static trees', () => { const { ast } = compileWithStringify( `
hello
hello
` ) expect(ast.hoists.length).toBe(1) // should be a normal vnode call expect(ast.hoists[0].type).toBe(NodeTypes.VNODE_CALL) }) test('should work on eligible content (elements with binding > 5)', () => { const { ast } = compileWithStringify( `
${repeat( ``, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) expect(ast.hoists.length).toBe(1) // should be optimized now expect(ast.hoists[0]).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_STATIC, arguments: [ JSON.stringify( `
${repeat( ``, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) ] }) }) test('should work on eligible content (elements > 20)', () => { const { ast } = compileWithStringify( `
${repeat( ``, StringifyThresholds.NODE_COUNT )}
` ) expect(ast.hoists.length).toBe(1) // should be optimized now expect(ast.hoists[0]).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_STATIC, arguments: [ JSON.stringify( `
${repeat( ``, StringifyThresholds.NODE_COUNT )}
` ) ] }) }) test('serliazing constant bindings', () => { const { ast } = compileWithStringify( `
${repeat( `{{ 1 }} + {{ false }}`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) expect(ast.hoists.length).toBe(1) // should be optimized now expect(ast.hoists[0]).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_STATIC, arguments: [ JSON.stringify( `
${repeat( `1 + false`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) ] }) }) test('escape', () => { const { ast } = compileWithStringify( `
${repeat( `{{ 1 }} + {{ '<' }}` + `&`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) expect(ast.hoists.length).toBe(1) // should be optimized now expect(ast.hoists[0]).toMatchObject({ type: NodeTypes.JS_CALL_EXPRESSION, callee: CREATE_STATIC, arguments: [ JSON.stringify( `
${repeat( `1 + <` + `&`, StringifyThresholds.ELEMENT_WITH_BINDING_COUNT )}
` ) ] }) }) })