Todorov_Stanimir

03.-Info-Parser_Test JS Advanced - 23 February 2020

Feb 24th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let Parser = require("./solution.js");
  2. let expect = require("chai").expect;
  3. describe("MyTests", () => {
  4.     let parser;
  5.     beforeEach(function () {
  6.         parser = new Parser('[{"Nancy":"architect"},{"John":"developer"},{"Kate":"HR"}]');
  7.     });
  8.  
  9.     describe("check constructor of class", function () {
  10.         it("shoud corectly inisialize an instance with parsed data and two properties", function () {
  11.             expect(parser._data).to.deep.equal([{ Nancy: 'architect' }, { John: 'developer' }, { Kate: 'HR' }]);
  12.             expect(parser._log).to.deep.equal([]);
  13.             expect(typeof parser._addToLog).to.deep.equal(`function`);
  14.             expect(parser._addToLog()).to.equal('Added to log');
  15.             expect(parser.data).to.deep.equal([{ Nancy: 'architect' }, { John: 'developer' }, { Kate: 'HR' }]);
  16.         });
  17.     });
  18.  
  19.     describe("check function print()", function () {
  20.         it("shoud add to log 'getData'", function () {
  21.             const result = parser.print();
  22.             expect(parser._log).to.deep.equal(['0: print']);
  23.             expect(result).to.equal('id|name|position\n0|Nancy|architect\n1|John|developer\n2|Kate|HR');
  24.         });
  25.     });
  26.  
  27.     describe("check function addEntries(entries)", function () {
  28.         it("shoud add new entries to '_data' and 'Entries added!' to '_log'", function () {
  29.             const result = parser.addEntries("Steven:tech-support Edd:administrator");
  30.             expect(parser.data).to.deep.equal([
  31.                 { Nancy: 'architect' },
  32.                 { John: 'developer' },
  33.                 { Kate: 'HR' },
  34.                 { Steven: 'tech-support' },
  35.                 { Edd: 'administrator' }
  36.             ]
  37.             );
  38.             expect(result).to.equal('Entries added!');
  39.             expect(parser._log).to.deep.equal(['0: addEntries', '1: getData']);
  40.         });
  41.     });
  42.  
  43.     describe("check function removeEntry(key)", function () {
  44.         it("shoud throw an error if we have not entries with given key", function () {
  45.             const result = () => parser.removeEntry('Steven');
  46.             expect(result).to.throw("There is no such entry!");
  47.         });
  48.         it("should return message 'Removed correctly!' if there is a entrie with given key", function () {
  49.             const result = parser.removeEntry('Nancy');
  50.             expect(result).to.equal("Removed correctly!");
  51.         });
  52.         it("should add 'removeEntry' to '_log', if there is a entrie with given key", function () {
  53.             parser.removeEntry('Nancy');
  54.             expect(parser._log).to.deep.equal(['0: removeEntry']);
  55.         });
  56.         it("should add property 'deleted: true' to entrie, if there is a entrie with given key", function () {
  57.             parser.removeEntry('Nancy');;
  58.             expect(parser.data).to.deep.equal([{ John: 'developer' }, { Kate: 'HR' }]);
  59.             expect(parser._data).to.deep.equal([{ "Nancy": "architect", "deleted": true }, { John: 'developer' }, { Kate: 'HR' }]);
  60.         });
  61.     });
  62.  
  63.     describe("check function addTolog(command)", function () {
  64.         it("shoud add add to the log array the command message and increase the counter.", function () {
  65.             parser.removeEntry("Nancy");
  66.             parser.addEntries("Steven:tech-support Edd:administrator");
  67.             parser.removeEntry("Steven");
  68.             expect(parser._log).to.deep.equal(['0: removeEntry', '1: addEntries', '2: removeEntry']);
  69.         });
  70.  
  71.     });
  72. });
  73.  
  74. // Checking Class is down
  75. // class Parser {
  76. //     constructor(data) {
  77. //         this._data = JSON.parse(data);
  78. //         this._log = [];
  79. //         this._addToLog = this._addToLogInitial();
  80. //     }
  81.  
  82. //     get data() {
  83. //         this._addToLog("getData");
  84. //         return this._data.filter(x => !x.hasOwnProperty("deleted"));
  85. //     }
  86.  
  87. //     print() {
  88. //         this._addToLog("print");
  89. //         return this._data
  90. //             .filter(x => !x.hasOwnProperty("deleted"))
  91. //             .reduce((acc, x, index) => {
  92. //                 acc.push(`${index}|${Object.keys(x)[0]}|${Object.values(x)[0]}`);
  93. //                 return acc;
  94. //             }, ["id|name|position"]).join("\n");
  95. //     }
  96.  
  97. //     addEntries(entries) {
  98. //         this._data = this._data.concat(entries.split(" ").map(x => {
  99. //             const [a, b] = x.split(":");
  100. //             let res = {};
  101. //             res[a] = b;
  102. //             return res;
  103. //         }));
  104. //         this._addToLog("addEntries");
  105.  
  106. //         return "Entries added!"
  107. //     }
  108.  
  109. //     removeEntry(key) {
  110. //         let entry = this._data.find(x => x.hasOwnProperty(key) && !x.hasOwnProperty("deleted"));
  111. //         if (entry === undefined) {
  112. //             throw new Error("There is no such entry!")
  113. //         }
  114. //         entry["deleted"] = true;
  115. //         this._addToLog("removeEntry");
  116.  
  117. //         return "Removed correctly!"
  118. //     }
  119.  
  120. //     _addToLogInitial() {
  121. //         let counter = 0;
  122. //         return function (command) {
  123. //             this._log.push(`${counter++}: ${command}`)
  124. //             return "Added to log";
  125. //         }
  126. //     }
  127. // }
  128.  
  129. // module.exports = Parser;
  130.  
  131.  
  132. // let parser = new Parser('[ {"Nancy":"architect"},{"John":"developer"},{"Kate": "HR"} ]');
  133. // console.log(parser.addEntries("Steven:tech-support Edd:administrator"));
  134. // console.log(parser.data);
  135. // console.log(parser.removeEntry("Kate"));
  136. // console.log(parser.data);
  137. // console.log("_".repeat(50));
  138. // console.log();
  139. // console.log(parser.print());
Advertisement
Add Comment
Please, Sign In to add comment