UNPKG

@airtasker/form-schema-compiler

Version:
156 lines (152 loc) 4.39 kB
"use strict"; var _compileComponents = require("./compileComponents"); var _const = require("./const"); var _utils = require("./utils"); describe("compileComponents", function () { it("Should throw Error if components is not object", function () { expect(function () { return (0, _compileComponents.compileComponents)(""); }).toThrow(); expect(function () { return (0, _compileComponents.compileComponents)(1); }).toThrow(); expect(function () { return (0, _compileComponents.compileComponents)(1); }).toThrow(); }); it("Should throw Error if component don't have type", function () { expect(function () { return (0, _compileComponents.compileComponents)({}); }).toThrow(); }); it("Should always wrapped with in components", function () { expect((0, _compileComponents.compileComponents)({ type: "Hello" })).toEqual({ type: _const.TYPES.Components, components: [{ type: "Hello" }] }); }); it("Should compile nested components", function () { expect((0, _compileComponents.compileComponents)({ type: "Parent", "<children>": { type: "Children", "<grandChildren>": [{ type: "GrandChild1" }, { type: "GrandChild2" }] } })).toEqual({ type: _const.TYPES.Components, components: [{ type: "Parent", children: { type: _const.TYPES.Components, components: [{ type: "Children", grandChildren: { type: _const.TYPES.Components, components: [{ type: "GrandChild1" }, { type: "GrandChild2" }] } }] } }] }); }); describe("compileProps", function () { it("Should compile normal props", function () { expect((0, _compileComponents.compileProps)({ a: "", b: null, c: 1, d: true, e: [], f: {} })).toEqual({ a: (0, _utils.createValue)(""), b: (0, _utils.createValue)(null), c: (0, _utils.createValue)(1), d: (0, _utils.createValue)(true), e: { type: _const.TYPES.Raw, value: [] }, f: { type: _const.TYPES.Raw, value: {} } }); }); it("Should compile expression props", function () { expect((0, _compileComponents.compileProps)({ "{propertyBinding}": "b", "{nestedObject}": { "{nestedProp}": { "{nestedNestedProp}": "n" }, "<nestedComponent>": { type: "Component" } }, "(event)": "c", "#template#": "a+d" })).toEqual({ propertyBinding: { type: _const.ANNOTATION_TYPES.PropertyBinding, value: (0, _utils.createProgram)((0, _utils.createIdentifier)("b")), nested: false }, nestedObject: { type: _const.ANNOTATION_TYPES.PropertyBinding, nested: true, value: { nestedProp: { type: _const.ANNOTATION_TYPES.PropertyBinding, nested: true, value: { nestedNestedProp: { type: _const.ANNOTATION_TYPES.PropertyBinding, value: (0, _utils.createProgram)((0, _utils.createIdentifier)("n")), nested: false } } }, nestedComponent: { type: _const.TYPES.Components, components: [{ type: "Component" }] } } }, onEvent: { type: _const.ANNOTATION_TYPES.EventBinding, value: (0, _utils.createProgram)((0, _utils.createIdentifier)("c")) }, template: { type: _const.ANNOTATION_TYPES.PropertyBinding, value: (0, _utils.createProgram)({ type: "TemplateLiteral", expressions: [], quasis: [(0, _utils.createValue)("a+d")] }) } }); }); it("Should throw if there is TwoWayBinding", function () { expect(function () { return (0, _compileComponents.compileProps)({ "[twoWayBinding]": "b" }); }).toThrow(); }); }); });