
/**
 * WPCOM module
 */

var WPCOM = require('../');
var Site = require('../lib/site');
var Post = require('../lib/post');
var util = require('./util');

/**
 * Testing data
 */

var test = require('./data');

/**
 * WPCOM instance
 */

describe('WPCOM#Site#Post', function(){

  // Create a new_post before to start the tests
  var new_post;
  before(function(done){
    var site = util.private_site();
    var post = site.post();

    post.add(test.new_post_data, function(err, post){
      if (err) done(err);

      new_post = post;
      done();
    });
  });

  describe('sync', function(){

    it('should create an `Post` instance from `Site`', function(){
      var post = WPCOM().site().post();
      post
        .should.be.an.instanceOf(Post);
    });

    it('should set post `id`', function(){
      var post = WPCOM().site().post();
      post.id(new_post.ID);

      post._id.should.be.eql(new_post.ID);
    });

    it('should set post `slug`', function(){
      var post = WPCOM().site().post();
      post.slug(new_post.slug);

      post._slug.should.be.eql(new_post.slug);
    });

  });

  describe('async', function(){

    describe('get', function(){
      it('should get added post (by id)', function(done){
        var site = util.private_site();
        var post = site.post(new_post.ID);

        post.get(function(err, post){
          if (err) throw err;

          post.should.be.eql(new_post);
          done();
        });
      });

      it('should get added post (by slug)', function(done){
        var site = util.private_site();
        var post = site.post({ slug: new_post.slug });

        post.get(function(err, post){
          if (err) throw err;

          post.should.be.eql(new_post);
          done();
        });
      });

    });

    describe('add', function(){

      it('should add a new post', function(done){
        var site = util.private_site();
        var post = site.post();

        post.add(test.new_post_data, function(err, data){
          if (err) throw err;

          // checking some data date
          data
            .should.be.ok
            .and.be.an.instanceOf(Object);

          data.ID
            .should.be.an.instanceOf(Number);

          data.site_ID
            .should.be.an.instanceOf(Number)
            .and.be.eql(test.private_site_id);

          done();
        });
      });

    });

    describe('update', function(){

      it('should edit the new added post', function(done){
        var site = util.private_site();
        var post = site.post(new_post.ID);

        var edited_title = new_post.title + ' has been changed';

        post.update({ title: edited_title }, function(err, data){
          if (err) throw err;

          data
            .should.be.ok
            .and.be.an.instanceOf(Object);

          data.title
            .should.be.eql(edited_title);

          done();
        });
      });

    });

    describe('delete', function(){

      it('should delete the new added post', function(done){
        var wpcom = util.private_site();

        wpcom.site.post.del(new_post.ID, function(err, post){
          if (err) throw err;

          post
            .should.be.ok
            .and.be.an.instanceOf(Object);

          post.ID
            .should.be.eql(new_post.ID);

          done();
        });
      });

    });

  });

});

@@ -3024,21 +3024,20 @@
     var 
-wpcom
+site
  = util.
@@ -3052,25 +3052,24 @@
 site();%0A
-%0A
         
 wpcom.si
@@ -3064,14 +3064,19 @@
     
-wpcom.
+var post = 
 site
@@ -3084,12 +3084,8 @@
 post
-.del
 (new
@@ -3092,18 +3092,40 @@
 _post.ID
-, 
+);%0A%0A        post.delete(
 function
@@ -3122,36 +3122,36 @@
 e(function(err, 
-post
+data
 )%7B%0A          if 
@@ -3174,28 +3174,28 @@
 %0A%0A          
-post
+data
 %0A           
@@ -3263,20 +3263,20 @@
         
-post
+data
 .ID%0A    

update post.delete() test