all files / spec/ chain-state.spec.1.ts

100% Statements 64/64
100% Branches 0/0
100% Functions 16/16
100% Lines 64/64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                                        
import { ChainState } from "../src/";
import * as Collections from "typescript-collections";
import * as Helpers from "./helpers";
 
describe("ChainState<T>", () => {
    describe("fromQueue", () => {
        function makeNameQueue(): Collections.Queue<string> {
            const names: Collections.Queue<string> = new Collections.Queue<string>();
            names.enqueue("jim");
            names.enqueue("george");
            names.enqueue("bob");
            names.enqueue("fred");
            return names;
        }
 
        it("Should be same length as input", function () {
            const names = makeNameQueue();
            const chain = ChainState.fromQueue(names);
            expect(chain.items.length).toEqual(names.size());
        });
 
        it("Should have same elements as input", function () {
            const names = makeNameQueue();
            const chain = ChainState.fromQueue(names);
            for (let x = 0; x < chain.items.length; x++) {
                expect(chain.items[x]).toBe(names.dequeue());
            }
        });
    });
 
    describe("constructor", () => {
        function makeNameArray(): string[] {
            return ["jim", "george", "bob", "fred"];
        }
 
        it("Should be same length as input", function () {
            const names = makeNameArray();
            const chain = new ChainState<string>(names);
            expect(chain.items.length).toEqual(names.length);
        });
 
        it("Should have same elements as input", function () {
            const names = makeNameArray();
            const chain = new ChainState<string>(names);
            for (let x = 0; x < names.length; x++) {
                expect(chain.items[x]).toBe(names[x]);
            }
        });
    });
 
    describe("equals", () => {
        function makeNameArray(): string[] {
            return ["jim", "george", "bob", "fred"];
        }
 
        function makePersonArray(): Helpers.Person[] {
            return [
                new Helpers.Person("jim", 20),
                new Helpers.Person("george", 18),
                new Helpers.Person("bob", 25),
                new Helpers.Person("fred", 46),
            ];
        }
 
        it("Should be same with equal strings", function () {
            const names1 = makeNameArray();
            const names2 = names1.slice(0);
 
            const chain1 = new ChainState<string>(names1);
            const chain2 = new ChainState<string>(names2);
 
            expect(chain1.equals(chain2)).toBe(true);
        });
 
        it("Should be same based on values not references", function () {
            const people1 = makePersonArray();
            const people2 = makePersonArray();
 
            const chain1 = new ChainState<Helpers.Person>(people1);
            const chain2 = new ChainState<Helpers.Person>(people2);
 
            expect(chain1.equals(chain2)).toBe(true);
        });
 
        it("Changing object field renders unequal", function () {
            const people1 = makePersonArray();
            const people2 = makePersonArray();
            people2[2].age = 30;
 
            const chain1 = new ChainState<Helpers.Person>(people1);
            const chain2 = new ChainState<Helpers.Person>(people2);
 
            expect(chain1.equals(chain2)).toBe(false);
        });
 
        it("Changing array length renders unequal", function () {
            const people1 = makePersonArray();
            const people2 = makePersonArray();
            people2.push(new Helpers.Person("tom", 27));
 
            const chain1 = new ChainState<Helpers.Person>(people1);
            const chain2 = new ChainState<Helpers.Person>(people2);
 
            expect(chain1.equals(chain2)).toBe(false);
        });
    });
});