Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let Parser = require("./solution.js");
- let expect = require("chai").expect;
- describe("MyTests", () => {
- let parser;
- beforeEach(function () {
- parser = new Parser('[{"Nancy":"architect"},{"John":"developer"},{"Kate":"HR"}]');
- });
- describe("check constructor of class", function () {
- it("shoud corectly inisialize an instance with parsed data and two properties", function () {
- expect(parser._data).to.deep.equal([{ Nancy: 'architect' }, { John: 'developer' }, { Kate: 'HR' }]);
- expect(parser._log).to.deep.equal([]);
- expect(typeof parser._addToLog).to.deep.equal(`function`);
- expect(parser._addToLog()).to.equal('Added to log');
- expect(parser.data).to.deep.equal([{ Nancy: 'architect' }, { John: 'developer' }, { Kate: 'HR' }]);
- });
- });
- describe("check function print()", function () {
- it("shoud add to log 'getData'", function () {
- const result = parser.print();
- expect(parser._log).to.deep.equal(['0: print']);
- expect(result).to.equal('id|name|position\n0|Nancy|architect\n1|John|developer\n2|Kate|HR');
- });
- });
- describe("check function addEntries(entries)", function () {
- it("shoud add new entries to '_data' and 'Entries added!' to '_log'", function () {
- const result = parser.addEntries("Steven:tech-support Edd:administrator");
- expect(parser.data).to.deep.equal([
- { Nancy: 'architect' },
- { John: 'developer' },
- { Kate: 'HR' },
- { Steven: 'tech-support' },
- { Edd: 'administrator' }
- ]
- );
- expect(result).to.equal('Entries added!');
- expect(parser._log).to.deep.equal(['0: addEntries', '1: getData']);
- });
- });
- describe("check function removeEntry(key)", function () {
- it("shoud throw an error if we have not entries with given key", function () {
- const result = () => parser.removeEntry('Steven');
- expect(result).to.throw("There is no such entry!");
- });
- it("should return message 'Removed correctly!' if there is a entrie with given key", function () {
- const result = parser.removeEntry('Nancy');
- expect(result).to.equal("Removed correctly!");
- });
- it("should add 'removeEntry' to '_log', if there is a entrie with given key", function () {
- parser.removeEntry('Nancy');
- expect(parser._log).to.deep.equal(['0: removeEntry']);
- });
- it("should add property 'deleted: true' to entrie, if there is a entrie with given key", function () {
- parser.removeEntry('Nancy');;
- expect(parser.data).to.deep.equal([{ John: 'developer' }, { Kate: 'HR' }]);
- expect(parser._data).to.deep.equal([{ "Nancy": "architect", "deleted": true }, { John: 'developer' }, { Kate: 'HR' }]);
- });
- });
- describe("check function addTolog(command)", function () {
- it("shoud add add to the log array the command message and increase the counter.", function () {
- parser.removeEntry("Nancy");
- parser.addEntries("Steven:tech-support Edd:administrator");
- parser.removeEntry("Steven");
- expect(parser._log).to.deep.equal(['0: removeEntry', '1: addEntries', '2: removeEntry']);
- });
- });
- });
- // Checking Class is down
- // class Parser {
- // constructor(data) {
- // this._data = JSON.parse(data);
- // this._log = [];
- // this._addToLog = this._addToLogInitial();
- // }
- // get data() {
- // this._addToLog("getData");
- // return this._data.filter(x => !x.hasOwnProperty("deleted"));
- // }
- // print() {
- // this._addToLog("print");
- // return this._data
- // .filter(x => !x.hasOwnProperty("deleted"))
- // .reduce((acc, x, index) => {
- // acc.push(`${index}|${Object.keys(x)[0]}|${Object.values(x)[0]}`);
- // return acc;
- // }, ["id|name|position"]).join("\n");
- // }
- // addEntries(entries) {
- // this._data = this._data.concat(entries.split(" ").map(x => {
- // const [a, b] = x.split(":");
- // let res = {};
- // res[a] = b;
- // return res;
- // }));
- // this._addToLog("addEntries");
- // return "Entries added!"
- // }
- // removeEntry(key) {
- // let entry = this._data.find(x => x.hasOwnProperty(key) && !x.hasOwnProperty("deleted"));
- // if (entry === undefined) {
- // throw new Error("There is no such entry!")
- // }
- // entry["deleted"] = true;
- // this._addToLog("removeEntry");
- // return "Removed correctly!"
- // }
- // _addToLogInitial() {
- // let counter = 0;
- // return function (command) {
- // this._log.push(`${counter++}: ${command}`)
- // return "Added to log";
- // }
- // }
- // }
- // module.exports = Parser;
- // let parser = new Parser('[ {"Nancy":"architect"},{"John":"developer"},{"Kate": "HR"} ]');
- // console.log(parser.addEntries("Steven:tech-support Edd:administrator"));
- // console.log(parser.data);
- // console.log(parser.removeEntry("Kate"));
- // console.log(parser.data);
- // console.log("_".repeat(50));
- // console.log();
- // console.log(parser.print());
Advertisement
Add Comment
Please, Sign In to add comment