Newer
Older
Labyrinth / src / FileStateRepository.spec.ts
import {expect} from "chai";
import * as fs from "fs";
import { FileStateRepository } from "./FileStateRepository";
import { GameState } from "./GameServer";

const file = "./TestStateRepository.lgr";

after(function(){
    // clean up after all tests
    if(fs.existsSync(file)){
        fs.unlinkSync(file);
    }
});

describe("Scenario: Loading content", function(){
    describe("Given no file exists", function(){
        if(fs.existsSync(file)){
            fs.unlinkSync(file);
        }

        describe("When states are loaded", function(){
            const loader = new FileStateRepository(file);
            const states = loader.loadAllGameStates();

            it("then an empty list should be returned", function(){
                expect(states).to.be.not.null;
                expect(states).to.deep.equal([]);
            });

            it("then the file should have been created", function(){
                const fileExists = fs.existsSync(file);
                expect(fileExists).to.be.true;
            });
        });
    });

    describe("Given a file with one entry exists", function(){
        const originialStates = [new GameState("12345", "lvl1", "0")];
        fs.writeFileSync(file, JSON.stringify(originialStates));

        describe("When states are loaded", function(){
            const loader = new FileStateRepository(file);
            const states = loader.loadAllGameStates();

            it("then one entry should be returned.", function(){
                expect(states).to.have.lengthOf(originialStates.length);
            });

            it("then the entry should be returned correctly.", function(){
                expect(states).to.deep.equal(originialStates);
            });
        });
    });

    describe("Given a file with two entries exists", function(){
        const originialStates = [new GameState("12345", "lvl1", "0"), new GameState("6879", "lvl1", "3")];
        fs.writeFileSync(file, JSON.stringify(originialStates));

        describe("When states are loaded", function(){
            const loader = new FileStateRepository(file);
            const states = loader.loadAllGameStates();

            it("then one entry should be returned.", function(){
                expect(states).to.have.lengthOf(originialStates.length);
            });

            it("then the entries should be returned correctly.", function(){
                expect(states).to.deep.equal(originialStates);
            });
        });
    });
});

describe("Scenario: Saving content", function(){
    describe("Given a list of two entries ", function(){
        const originialStates = [new GameState("12345", "lvl1", "0"), new GameState("6879", "lvl1", "3")];

        describe("When states are saved", function(){
            const repo = new FileStateRepository(file);
            const result = repo.saveAllGameStates(originialStates);

            it("then it should return true.", function(){
                expect(result).to.be.true;
            });

            it("then the file should exist.", function(){
                const fileExists = fs.existsSync(file);
                expect(fileExists).to.be.true;
            });

            it("then file should contain exactly the entries.", function(){
                if(fs.existsSync(file))
                {
                    const fileContent = fs.readFileSync(file).toString();
                    const entries = JSON.parse(fileContent);

                    expect(entries).to.deep.equal(originialStates);
                }
                else {
                    expect(false, "No fiel to validate content of.").to.be.true;
                }
            });
        });
    });
});