settingsAccountsettings
Menusettings

Q: Sorted List (unit testing task - Mocha and Chai)

+2 votes

Write Mocha tests to verify the functionality of the following JS code:

class SortedList {
    constructor() {
        this.list = [];
    }

    add(element) {
        this.list.push(element);
        this.sort();
    }

    remove(index) {
        this.vrfyRange(index);
        this.list.splice(index, 1);
    }

    get(index) {
        this.vrfyRange(index);
        return this.list[index];
    }

    vrfyRange(index) {
        if (this.list.length == 0) throw new Error("Collection is empty.");
        if (index < 0 || index >= this.list.length) throw new Error("Index was outside the bounds of the collection.");
    }

    sort() {
        this.list.sort((a, b) => a - b);
    }

    get size() {
        return this.list.length;
    }
}

Your tests will be supplied with a class named SortedList. It needs to meet the following requirements:

  • Maintains a collection of numeric elements
  • Has an add(element) function, which adds a new element to the collection
  • Has a remove(index) function, which removes the element at position index
  • Has a get(index) function, which return the value of the element at position index
  • Keeps the elements of the collection sorted in ascending order at all times
  • Throws an error if the functions get() and remove() are supplied with an invalid index (negative or outside the collection) or if the collection is empty
  • Has a size() property getter, which returns the number of elements inside the collection
asked in JavaScript category by user ak47seo

1 Answer

+1 vote

First you need to write down the requirements for the tests:

Test initial state:
--add exists
--remove exists
--get exists
--size exists

add():
--add 1 element
--add many elements

remove():
--test if list is empty (should throw error: "Collection is empty")
--test negative number(index) (should throw error: "Index was outside the bounds of the collection.")
--test if index is equal to list length (should throw error: "Index was outside the bounds of the collection.")
--test if index is bigger than list length (should throw error: "Index was outside the bounds of the collection.")
--test with correct index (element should be removed)

get() (almost the same as remove()):
--test if list is empty (should throw error)
--test negative number (should throw error)
--test if index is equal tp list length (should throw error)
--test if index is bigger than list length (should throw error)
--test with correct index (should return the correct element)

size():
--empty list
--list with elements

Here is the JavaScript file with the tests inside:

let expect = require("chai").expect;

class SortedList {
    constructor() {
        this.list = [];
    }

    add(element) {
        this.list.push(element);
        this.sort();
    }

    remove(index) {
        this.vrfyRange(index);
        this.list.splice(index, 1);
    }

    get(index) {
        this.vrfyRange(index);
        return this.list[index];
    }

    vrfyRange(index) {
        if (this.list.length == 0) throw new Error("Collection is empty.");
        if (index < 0 || index >= this.list.length) throw new Error("Index was outside the bounds of the collection.");
    }

    sort() {
        this.list.sort((a, b) => a - b);
    }

    get size() {
        return this.list.length;
    }
}


describe("Tests for this task", function () {

    let myList;
    beforeEach(function () {
        myList = new SortedList();
    });
//Test initial state:
    describe("Test initial state", function () {
        it("add should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("add")).to.equal(true);
        });
        it("remove should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("remove")).to.equal(true);
        });
        it("get should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("get")).to.equal(true);
        });
        it("size should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("size")).to.equal(true);
        })
    });

//add():
    describe("Test add", function () {
        it("add with 1 element", function () {
            myList.add(5);
            expect(myList.list.join(", ")).to.equal("5");
        });
        it("add with many elements", function () {
            myList.add(5);
            myList.add(4);
            myList.add(3);
            expect(myList.list.join(", ")).to.equal("3, 4, 5");
        });
    });

//remove():
    describe("Test remove", function () {
        it("remove with empty list", function () {
            expect(() => myList.remove()).throw(Error, "Collection is empty.")
        });
        it("remove with negative index", function () {
            myList.add(3);
            expect(() => myList.remove(-1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - index is equal to list length", function () {
            myList.add(3);
            expect(() => myList.remove(1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - index is bigger than list length", function () {
            myList.add(3);
            expect(() => myList.remove(2)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - test with correct index", function () {
            myList.add(3);
            myList.add(4);
            myList.add(5);
            myList.remove(1);
            expect(myList.list.join(", ")).to.equal("3, 5");
        });
    });

//get():
    describe("Test get", function () {
        it("get with empty list", function () {
            expect(() => myList.get()).throw(Error, "Collection is empty.")
        });
        it("get with negative index", function () {
            myList.add(3);
            expect(() => myList.get(-1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - index is equal to list length", function () {
            myList.add(3);
            expect(() => myList.get(1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - index is bigger than list length", function () {
            myList.add(3);
            expect(() => myList.get(2)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - test with correct index", function () {
            myList.add(3);
            myList.add(4);
            myList.add(5);
            let element = myList.get(1);
            expect(element).to.equal(4);
        });
    });

//size():
    describe("Test size", function () {
        it("size with empty list", function () {
            expect(myList.size).to.equal(0);
        });
        it("size with non-empty list", function () {
            myList.add(1);
            myList.add(2);
            myList.add(3);
            expect(myList.size).to.equal(3);
        });
    });
});

Only the Mocha + Chai tests:

describe("Tests for this task", function () {

    let myList;
    beforeEach(function () {
        myList = new SortedList();
    });
//Test initial state:
    describe("Test initial state", function () {
        it("add should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("add")).to.equal(true);
        });
        it("remove should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("remove")).to.equal(true);
        });
        it("get should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("get")).to.equal(true);
        });
        it("size should exist", function () {
            expect(SortedList.prototype.hasOwnProperty("size")).to.equal(true);
        })
    });

//add():
    describe("Test add", function () {
        it("add with 1 element", function () {
            myList.add(5);
            expect(myList.list.join(", ")).to.equal("5");
        });
        it("add with many elements", function () {
            myList.add(5);
            myList.add(4);
            myList.add(3);
            expect(myList.list.join(", ")).to.equal("3, 4, 5");
        });
    });

//remove():
    describe("Test remove", function () {
        it("remove with empty list", function () {
            expect(() => myList.remove()).throw(Error, "Collection is empty.")
        });
        it("remove with negative index", function () {
            myList.add(3);
            expect(() => myList.remove(-1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - index is equal to list length", function () {
            myList.add(3);
            expect(() => myList.remove(1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - index is bigger than list length", function () {
            myList.add(3);
            expect(() => myList.remove(2)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("remove - test with correct index", function () {
            myList.add(3);
            myList.add(4);
            myList.add(5);
            myList.remove(1);
            expect(myList.list.join(", ")).to.equal("3, 5");
        });
    });

//get():
    describe("Test get", function () {
        it("get with empty list", function () {
            expect(() => myList.get()).throw(Error, "Collection is empty.")
        });
        it("get with negative index", function () {
            myList.add(3);
            expect(() => myList.get(-1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - index is equal to list length", function () {
            myList.add(3);
            expect(() => myList.get(1)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - index is bigger than list length", function () {
            myList.add(3);
            expect(() => myList.get(2)).throw(Error, "Index was outside the bounds of the collection.")
        });
        it("get - test with correct index", function () {
            myList.add(3);
            myList.add(4);
            myList.add(5);
            let element = myList.get(1);
            expect(element).to.equal(4);
        });
    });

//size():
    describe("Test size", function () {
        it("size with empty list", function () {
            expect(myList.size).to.equal(0);
        });
        it("size with non-empty list", function () {
            myList.add(1);
            myList.add(2);
            myList.add(3);
            expect(myList.size).to.equal(3);
        });
    });
});
answered by user eiorgert
...