'use strict';

var expect = require('chai').expect;
require('chai').use(require('dirty-chai'));
var worlds = require("../../components/worlds");
var _ = require('lodash');
var authenticatedRequest = require('../../authentication/authentication-helper.spec');
var VALID_USER = authenticatedRequest.VALID_USER;

describe('/api/worlds', function () {

  authenticatedRequest.useAuth(VALID_USER);

  describe('GET', function () {
    var worldList = [];

    beforeEach(function (done) {
      worlds.World.create([{
        name: "Hogwarts"
      }, {
          name: "Narnia"
        }]).then(function (documents) {
        worldList = _.map(documents, function (document) {
          var documentJSON = document.toJSON();
          documentJSON._id = document._id.toString();
          return documentJSON;
        });
        done();
      }, done);
    });

    afterEach(function (done) {
      worlds.World.remove({}, function () {
        done();
      });
    });

    it('should return a list of worlds', function (done) {
      authenticatedRequest
        .get('/api/worlds')
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function (err, res) {
        if (err) return done(err);
        expect(res.body).to.be.eql(worldList);
        done();
      });
    });
  });

  describe('POST', function () {
    afterEach(function (done) {
      worlds.World.remove({}, function () {
        done();
      });
    });

    it('should add a world to the list', function (done) {
      var newWorld = { name: 'Lost World' };

      authenticatedRequest
        .post('/api/worlds')
        .send(newWorld)
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function (err, res) {
        if (err) return done(err);
        expect(res.body.name).to.be.eql('Lost World');
        done();
      });
    });

    it('will reject request to create world with a preexisting id', function (done) {
      worlds.World.create({ name: 'Lost World' }).then(function (world) {
        authenticatedRequest
          .post('/api/worlds')
          .send(world)
          .expect(409)
          .expect('Content-Type', /json/)
          .end(function (err, res) {
          if (err) return done(err);
          expect(res.body).to.be.eql({ message: 'A world with that id already exists.' });
          done();
        });
      });
    });
  });

  describe('PUT', function() {
    afterEach(function(done) {
      worlds.World.remove({}, function () {
        done();
      });
    });

    //TODO: finish the tests
    it('should update a world', function(done) {
      var updatedWorld = {name: 'Updated World'};

      authenticatedRequest
        .put('/api/worlds')
        .send(updatedWorld)
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function (err, res) {
          if (err) return done(err);
          expect(res.body.name).to.be.eql('Lost World');
          done();
        });

    })
  })

});
@@ -1792,36 +1792,37 @@
 ).to.be.eql(
-'Lost World'
+newWorld.name
 );%0A        d
@@ -2402,37 +2402,78 @@
 unction() %7B%0A    
-after
+var world = %7B name: 'Lost World' %7D;%0A    before
 Each(function(do
@@ -2482,38 +2482,160 @@
 ) %7B%0A
+%0A
       
-worlds.World.remove(%7B%7D, 
+authenticatedRequest%0A        .post('/api/worlds')%0A        .send(world)%0A        .expect(200)%0A        .expect('Content-Type', /json/)%0A        .end(
 func
@@ -2632,32 +2632,40 @@
  .end(function (
+err, res
 ) %7B%0A        done
@@ -2652,37 +2652,58 @@
  res) %7B%0A        
+ 
+ if (err) return 
 done(
+err
 );%0A      %7D);%0A   
@@ -2699,49 +2699,150 @@
     
-%7D);%0A    %7D);%0A%0A    //TODO: finish the tests
+    done();%0A        %7D);%0A    %7D);%0A%0A    afterEach(function(done) %7B%0A      worlds.World.remove(%7B%7D, function () %7B%0A        done();%0A      %7D);%0A    %7D);%0A
 %0A   
@@ -2867,16 +2867,21 @@
  a world
+ name
 ', funct
@@ -2906,45 +2906,117 @@
 var 
-u
+newWorldName = 'U
 pdated
+ 
 World
- = %7Bname: 'Updated 
+';%0A      var request = %7BoldWorldName: world.name, updatedWorldName: new
 World
-'
+Name
 %7D;%0A%0A
@@ -3084,28 +3084,23 @@
   .send(
-updatedWorld
+request
 )%0A      
@@ -3268,28 +3268,28 @@
 .be.eql(
-'Lost 
+new
 World
-'
+Name
 );%0A     
@@ -3309,25 +3309,24 @@
         %7D);%0A
-%0A
     %7D)%0A  %7D)%0A
@@ -3329,8 +3329,8 @@
  %7D)%0A
-%0A
 %7D);
+%0A

add update world name test.