settingsAccountsettings
Menusettings

Q: Add Left / Add Right / Clear (Unit Testing)

+3 votes

You are given the following JavaScript code:

function makeList() {
let data = [];
return {
addLeft: function(item) {
data.unshift(item);
},
addRight: function(item) {
data.push(item);
},
clear: function() {
data = [];
},
toString: function() {
return data.join(", ");
}
};
}
module.exports = makeList;

Functionality:
The above code creates a list data structure that holds items (of any type). It supports the following operations:

  • addLeft(item) – adds item at the beginning of the list.
  • addRight(item) – adds item at the end of the list.
  • clear() – removes all elements in the list.
  • toString() – returns the string representations of the list items, separated by “, “.

Examples:
This is an example how this code is intended to be used:

Sample code usage:

let list = makeList();
console.log(`list = [${l
list.addRight(1);
list.addRight("two");
list.addLeft(0);
console.log(`list = [${l
list.clear();
list.addLeft("beer");
list.addLeft(3.14);
console.log(`list = [${list}]`);

Corresponding output:

list = []
list = [0, 1, two]
list = [3.14, beer]

Your Task:
Using Mocha and Chai write JS unit tests to test the entire functionality of the list object. Your code will only be
provided the list function, how you test the list is entirely up to you - whether you create a new list before each
test or share the same list between tests.

You should have at least 6 test cases, make sure you cover all edge cases. You may use the following code as a
template:

describe("TODO …", function() {
let myList = {};
beforeEach(function () {
myList = makeList();
});
it("TODO …", function() {
// TODO: …
});
// TODO: …
});
asked in JavaScript category by user eiorgert

1 Answer

+1 vote

Here are the Mocha/Chai tests:

//let makeList = require('./solved');
//let expect = require('chai').expect;

describe("Pointy List", function () {
    let myList = {};

    beforeEach(function () {
        myList = makeList();
    });

    it("should contain all properties", function () {
        expect(myList.addLeft).to.exist;
        expect(myList.addRight).to.exist;
        expect(myList.clear).to.exist;
        expect(myList.toString).to.exist;
    });

    it("should start empty", function () {
        expect(myList.toString()).to.equal("");
    });

    it("can add on the left", function () {
        myList.addLeft("item");
        expect(myList.toString()).to.equal("item");
    });

    it("can add on the right", function () {
        myList.addRight("item");
        expect(myList.toString()).to.equal("item");
    });

    it("can add multiple on the left", function () {
        myList.addLeft("anchor");
        myList.addLeft("item1");
        myList.addLeft("item2");
        expect(myList.toString()).to.equal("item2, item1, anchor");
    });

    it("can add multiple on the right", function () {
        myList.addRight("anchor");
        myList.addRight("item1");
        myList.addRight("item2");
        expect(myList.toString()).to.equal("anchor, item1, item2");
    });

    it("should clear all items", function () {
        myList.addRight("item");
        myList.addRight("item");
        myList.clear();
        expect(myList.toString()).to.equal("");
    });
});

 

answered by user hues
...