|
| 1 | +const { omit } = require("lodash"); |
| 2 | +const should = require("should"); |
| 3 | +const CodeBuilder = require("./code-builder"); |
| 4 | + |
| 5 | +const { removeProperty } = require("./general"); |
| 6 | +const { constructAppendedParamsCode } = require("./params"); |
| 7 | + |
| 8 | +describe("Test helpers methods", () => { |
| 9 | + describe("Test RemoveProperty helper", () => { |
| 10 | + it("RemoveProperty called with invalid params", () => { |
| 11 | + (function () { |
| 12 | + removeProperty("str", "property"); |
| 13 | + }.should.throw(new Error("originalObject must be an object."))); |
| 14 | + }); |
| 15 | + |
| 16 | + it("returned object stayed the same if a non existing property name sent", () => { |
| 17 | + const obj = { a: 1, b: 2 }; |
| 18 | + const result = removeProperty(obj, "c"); |
| 19 | + |
| 20 | + result.should.equal(obj); |
| 21 | + }); |
| 22 | + |
| 23 | + it("insensitive case property removed from object successfully", () => { |
| 24 | + const obj = { a: 1, b: 2 }; |
| 25 | + const result = removeProperty(obj, "B"); |
| 26 | + |
| 27 | + result.should.deepEqual(omit(obj, "b")); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("Test constructAppendedParamsCode helper", () => { |
| 32 | + const fakeParams = [ |
| 33 | + { name: "a", value: "1" }, |
| 34 | + { name: "b", value: "2" }, |
| 35 | + ]; |
| 36 | + |
| 37 | + it("called with invalid code argument", () => { |
| 38 | + (function () { |
| 39 | + constructAppendedParamsCode({}, []); |
| 40 | + }.should.throw( |
| 41 | + new Error("code argument must be an instance of CodeBuilder") |
| 42 | + )); |
| 43 | + }); |
| 44 | + |
| 45 | + it("called with invalid params argument", () => { |
| 46 | + (function () { |
| 47 | + constructAppendedParamsCode(new CodeBuilder(), {}); |
| 48 | + }.should.throw(new Error("params argument must be an array"))); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("called with multiple options variations", () => { |
| 52 | + const fakeParamsWithFile = [ |
| 53 | + ...fakeParams, |
| 54 | + { name: "a", fileName: "fakeFileName" }, |
| 55 | + ]; |
| 56 | + const lastIndex = params.length - 1; |
| 57 | + |
| 58 | + it("called with file param and false isBrowser option", () => { |
| 59 | + const result = constructAppendedParamsCode( |
| 60 | + new CodeBuilder(), |
| 61 | + fakeParamsWithFile, |
| 62 | + { |
| 63 | + isBrowser: false, |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + result.should.be.an.instanceof(CodeBuilder); |
| 68 | + result |
| 69 | + .join() |
| 70 | + .should.containEql( |
| 71 | + `fs.createReadStream("/PATH/TO/${fakeParamsWithFile[lastIndex].fileName}")` |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("called with file param and true isBrowser option", () => { |
| 76 | + const result = constructAppendedParamsCode( |
| 77 | + new CodeBuilder(), |
| 78 | + fakeParamsWithFile, |
| 79 | + { |
| 80 | + isBrowser: true, |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + result.should.be.an.instanceof(CodeBuilder); |
| 85 | + result |
| 86 | + .join() |
| 87 | + .should.containEql( |
| 88 | + `yourAppInput.files[0], ${JSON.stringify( |
| 89 | + params[lastIndex].fileName |
| 90 | + )}` |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it("called with dataVarName option", () => { |
| 95 | + const result = constructAppendedParamsCode(new CodeBuilder(), params, { |
| 96 | + dataVarName: "dataObject", |
| 97 | + }); |
| 98 | + |
| 99 | + result.should.be.an.instanceof(CodeBuilder); |
| 100 | + result.join().should.containEql("dataObject.append"); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("returned new code object with two params", () => { |
| 105 | + const result = constructAppendedParamsCode(new CodeBuilder(), fakeParams); |
| 106 | + |
| 107 | + result.should.be.an.instanceof(CodeBuilder); |
| 108 | + result.getLength().should.equal(2); |
| 109 | + result |
| 110 | + .join() |
| 111 | + .should.equal('data.append("a", "1");\ndata.append("b", "2");'); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments