/**
 * Created by Ryan Lamb on 3/19/16.
 */

/* The following comment informs JSLint about the expect method of Jasmine. */
/*global expect*/
/*global fail*/

describe("PersistenceEngine", function() {

    var persistence;
    var MapData;
    var Layer;
    var count = 0;

    beforeEach(function(done) {
        var requirejs = require('requirejs');

        requirejs.config({
            nodeRequire: require,
            baseUrl: '../',
        });

        requirejs(['MapData/mapdata', 'MapData/observable', 'MapData/layer'],
            function(mapdata, observable, layer) {
                MapData = mapdata.MapData;
                Layer = layer.Layer;

                var mapdatabase = require("../mapdatabase.js");
                var database = new mapdatabase('mongodb://localhost:27017/unittest' + count);
                count = count + 1;

                database.connect(function() {
                    //Clear the database so each test runs on a clean instance.
                    database._drop(function() {
                        var persistenceEngine = require("../persistenceengine.js");
                        persistence = new persistenceEngine(database, mapdata, observable);
                        done();
                    });

                });

            });
    });

    it("should be created", function() {
        expect(persistence).not.toBe(null);
        expect(persistence).not.toBe(undefined);
    });

    it("should not have the test map", function(done) {
        persistence.loadMap("TestMap", function(mapLoaded, map) {
            expect(mapLoaded).toBe(false);
            expect(map).toBe(undefined);
            done();
        });
    });

    it("should allow creation of a map", function(done) {
        persistence.createMap("TestMap", function(result) {
            expect(result.result.ok).toBe(1);
            done();
        });
    });

    it("should persist changes to the map name", function(done) {
        persistence.createMap("TestMap", function(result) {
            persistence.loadMap("TestMap", function(mapFound, map) {
                if (mapFound === true && map != undefined) {
                    map.name = "NewMapName";
                    persistence.then(function() {
                        persistence.loadMap("NewMapName", function(mapFound, map) {
                            if (mapFound === true && map != undefined) {
                                expect(map.name).toBe("NewMapName");
                                done();
                            }
                            else if (mapFound !== true) {
                                fail("Map could not be loaded.");
                                done();
                            }
                        });
                    });
                }
                else if (mapFound !== true) {
                    fail("Map could not be loaded.");
                    done();
                }
            });

        });
    });

    it("should persist addition of layers", function(done) {
        persistence.createMap("TestMap", function(result) {
            persistence.loadMap("TestMap", function(mapFound, map) {
                if (mapFound === true && map != undefined) {
                    var testLayer = new Layer();
                    testLayer.name = "TestLayer";

                    var testLayer2 = new Layer();
                    testLayer2.name = "TestLayer2";

                    map.layers.addLayer(testLayer);
                    map.layers.addLayer(testLayer2);

                    persistence.then(function() {
                        persistence.loadMap("TestMap", function(mapFound, map) {
                            if (mapFound === true && map != undefined) {
                                expect(map.layers.getLayerCount()).toBe(2);
                                expect(map.layers.getLayerByName("TestLayer").name).toBe("TestLayer");
                                expect(map.layers.getLayerByName("TestLayer2").name).toBe("TestLayer2");
                                done();
                            }
                        });
                    });

                }
                else if (mapFound !== true) {
                    fail("Map could not be loaded.")
                    done();
                }
            });

        });
    });
});
@@ -3005,57 +3005,48 @@
     
-it(%22should persist addition of layers%22, function(
+function makeAndModifyMap(modify, test, 
 done
@@ -3243,313 +3243,39 @@
 ) %7B%0A
-                    var testLayer = new Layer();%0A                    testLayer.name = %22TestLayer%22;%0A%0A                    var testLayer2 = new Layer();%0A                    testLayer2.name = %22TestLayer2%22;%0A%0A                    map.layers.addLayer(testLayer);%0A                    map.layers.addLayer(testLayer2
+%0A                    modify(map
 );%0A%0A
@@ -3514,56 +3514,89 @@
     
-expect(map.layers.getLayerCount()).toB
+test(map);%0A                                don
 e(
-2
 );%0A    
+                        %7D%0A
     
@@ -3624,182 +3624,110 @@
    e
-xpect(map.layers.getLayerByName(%22TestLayer%22).name).toBe(%22TestLayer%22);%0A                                expect(map.layers.getLayerByName(%22TestLayer2%22).name).toBe(%22TestLayer2
+lse if (mapFound !== true) %7B%0A                                fail(%22Modified map could not be loaded.
 %22)
-;
 %0A   
@@ -3928,33 +3928,41 @@
           fail(%22
-M
+Created m
 ap could not be 
@@ -4025,33 +4025,32 @@
             %7D);%0A
-%0A
         %7D);%0A    
@@ -4037,27 +4037,666 @@
 %7D);%0A        %7D);%0A
+    %7D%0A%0A    it(%22should persist addition of layers%22, function(done) %7B%0A        makeAndModifyMap(function(map) %7B%0A            var testLayer = new Layer();%0A            testLayer.name = %22TestLayer%22;%0A%0A            var testLayer2 = new Layer();%0A            testLayer2.name = %22TestLayer2%22;%0A%0A            map.layers.addLayer(testLayer);%0A            map.layers.addLayer(testLayer2);%0A        %7D, function(map) %7B%0A            expect(map.layers.getLayerCount()).toBe(2);%0A            expect(map.layers.getLayerByName(%22TestLayer%22).name).toBe(%22TestLayer%22);%0A            expect(map.layers.getLayerByName(%22TestLayer2%22).name).toBe(%22TestLayer2%22);%0A%0A        %7D, done);%0A
     %7D);%0A%7D);

Create common function for unit tests to create and modify a map.