import transform from '../src/transform.mjs'; describe("transform", () => { const ast_emptyDiv = { "type": "root", "children": [ { "type": "element", "tagName": "div", "properties": {}, "children": [], }, ], }; it("should import transform()", () => { expect(transform).toBeDefined(); }); it("should throw an error if there's no AST", () => { expect(transform).toBeDefined(); expect(() => transform()).toThrow(); }); it("should act as an identity function if there are no tx fxns", () => { expect(transform(ast_emptyDiv)).toEqual({ "type": "root", "children": [ { "type": "element", "tagName": "div", "properties": {}, "children": [], }, ], }); }); it("should leave the ast untouched for undefined", () => { expect(transform(ast_emptyDiv, () => undefined)).toEqual({ "type": "root", "children": [ { "type": "element", "tagName": "div", "properties": {}, "children": [], }, ], }); }); it("should remove the node for null", () => { expect(transform(ast_emptyDiv, () => null)).toEqual({ "type": "root", "children": [], }); }); it("should barf if non-AST is returned", () => { expect(() => {transform(ast_emptyDiv, () => "pigs!")}).toThrow(); }); it("should replace old nodes with transformed nodes", () => { expect(transform( ast_emptyDiv, () => ({type: "element", tagName: "div", children: []}) )).toEqual({ "type": "root", "children": [ { type: "element", tagName: "div", children: [], }, ], }); }); it("should transform nodes anywhere in the child list", () => { expect(transform( { "type": "root", "children": [ { type: "element", tagName: "div", children: [], }, { type: "element", tagName: "span", children: [], }, ], }, (node) => { if (node.tagName === "span") { return { type: "element", tagName: "p", children: [], }; } } )).toEqual({ "type": "root", "children": [ { type: "element", tagName: "div", children: [], }, { type: "element", tagName: "p", children: [], }, ], }); }); it("should delete nodes anywhere in the child list", () => { expect(transform( { "type": "root", "children": [ { type: "element", tagName: "div", children: [], }, { type: "element", tagName: "span", children: [], }, ], }, (node) => { if (node.tagName === "span") { return null; } } )).toEqual({ "type": "root", "children": [ { type: "element", tagName: "div", children: [], }, ], }); }); it("should stop transforming after a transformer returns null", () => { expect(transform( ast_emptyDiv, () => null, () => ({type: "element", tagName: "div", children: []}) )).toEqual({ "type": "root", "children": [], }); }); it("should handle multiple tx fxns", () => { const output = transform(ast_emptyDiv, () => {return}, () => null); expect(output).toEqual({ type: "root", children: [], }); }); });