
/**
 * WPCOM module
 */

var WPCOM = require('../');
var Site = require('../lib/site');
var assert = require('assert');

/**
 * Testing data
 */

var fixture = require('./fixture');

/**
 * WPCOM instance
 */

describe('site.media', function(){
    // Create `wpcom` and `site` global instances
  var wpcom = WPCOM(fixture.site.private.token);
  var site = wpcom.site(fixture.site.private.url);

  var add_urls_array;
  var add_urls_object;

  // Create a testing_media before to start tests

  var testing_media;
  before(function(done){
    site.addMediaFiles(fixture.media.files[0], function(err, data) {
      if (err) throw err;

      testing_media = data ? data.media[0] : {};
      done();
    });
  });

  after(function(done){
    // clean media added through of array by urls
    site.deleteMedia(add_urls_array.media[0].ID, function(err, data) {
      if (err) throw err;

      site.deleteMedia(add_urls_array.media[1].ID, function(err, data) {
        if (err) throw err;

        site.deleteMedia(add_urls_array.media[2].ID, function(err, data) {
          if (err) throw err;

          site.deleteMedia(add_urls_object.media[0].ID, function(err, data) {
            if (err) throw err;

            done();
          });

        });

      });

    });

  });

  describe('media.get()', function(){

    it('should get added media', function(done){
      var media = site.media(testing_media.ID);
      media.get(function(err, data){
        if (err) throw err;

        assert.equal(testing_media.ID, data.ID);
        done();
      });
    });

  });

  describe('media.update()', function(){

    it('should edit the media title', function(done){
      var edited_title = "This is the new title";

      site
      .media(testing_media.ID)
      .update({ apiVersion: '1.1' }, { title: edited_title }, function(err, data){
        if (err) throw err;

        assert.ok(data);
        assert.equal(edited_title, data.title);

        done();
      });
    });

  });

  describe('media.addFiles(files)', function(){

    it('should create a new media from a file', function(done){
      site
      .media()
      .addFiles(fixture.media.files, function(err, data){
        if (err) throw err;

        assert.ok(data);
        assert.ok(data.media instanceof Array);
        assert.equal(fixture.media.files.length, data.media.length);
        done();
      });

    });

  });

  describe('media.addUrls(object)', function(){

    it('should create a new media from an object', function(done){
      var media_object = fixture.media.urls[1];

      site
      .media()
      .addUrls(media_object, function(err, data){
        if (err) throw err;

        assert.ok(data);
        add_urls_object = data;
        done();
      });
    });

  });

  describe('media.addUrls(array)', function(){

    it('should create a new media', function(done){
      site
      .media()
      .addUrls(fixture.media.urls, function(err, data){
        if (err) throw err;

        assert.ok(data);
        assert.ok(data.media instanceof Array);
        assert.equal(fixture.media.urls.length, data.media.length);

        add_urls_array = data;

        done();
      });

    });

  });

  describe('media.delete()', function(){

    it('should delete a media', function(done){

      site
      .media(testing_media.ID)
      .del(function(err, data){
        if (err) throw err;

        assert.equal(testing_media.ID, data.ID);
        done();
      });

    });

  });

});
@@ -214,16 +214,22 @@
 scribe('
+wpcom.
 site.med
@@ -245,16 +245,16 @@
 tion()%7B%0A
-
     // C
@@ -1287,24 +1287,35 @@
   describe('
+wpcom.site.
 media.get()'
@@ -1591,24 +1591,35 @@
   describe('
+wpcom.site.
 media.update
@@ -2015,32 +2015,43 @@
 );%0A%0A  describe('
+wpcom.site.
 media.addFiles(f
@@ -2437,32 +2437,43 @@
 );%0A%0A  describe('
+wpcom.site.
 media.addUrls(ob
@@ -2821,24 +2821,35 @@
   describe('
+wpcom.site.
 media.addUrl
@@ -3244,32 +3244,32 @@
     %7D);%0A%0A  %7D);%0A%0A
-
   describe('medi
@@ -3264,16 +3264,27 @@
 scribe('
+wpcom.site.
 media.de

rename `media` testing names adding `wpcom` prefix