'use strict';

var jf = require('../../'),
    expect    = require('chai').expect,
    fs = require('fs');

describe('filed( obj )', function () {
    it('should be a function', function () {
        expect(jf.filed).to.be.a('function');
    });
});

const testFilePath = './' + Math.random() + '.json';
describe('The initialValue', function () {
  it('should be equal to defined in json.filed.', function (done) {
    jf.filed( testFilePath, function( obj ) {
        expect(obj).to.eql(jf.initialValue());
        done();
    });
  });
});


const testFile2Path = './' + Math.random() + '.json';
var updatedValue = { msg: 'updated' };
describe('The updated value', function () {
  it('should be read again as updated.', function (done) {
    jf.filed( testFile2Path, function( obj ) {
      return updatedValue;
    });

    setTimeout(
      function(){
        jf.filed( testFile2Path, function( obj ) {
          expect(obj).to.eql(updatedValue);
          done();
        });
      },
      1000);
    })

});

const testFile3Path = './' + Math.random() + '.json';
var initialValue = { msg: 'initial' };
describe('The value function does not return ', function () {
  it('should not be written.', function (done) {
    jf.filed( testFile3Path, function( obj ) {
      return initialValue;
    });

    setTimeout(
      function(){
        jf.filed( testFile3Path, function( obj ) {
          expect(obj).to.eql(initialValue);
          obj.msg = "this value is not written."
          //return obj
        });
      },
      1000
    );

    setTimeout(
      function(){
        jf.filed( testFile3Path, function( obj ) {
          expect(obj).to.eql(initialValue);
          done();
        });
      },
      1000
    );

  });
});


const testFile4Path = './' + Math.random() + '.json';
describe('Function return null', function () {
  it('does not make json to be written.', function (done) {
    jf.filed( testFile4Path, function( obj ) {
      return initialValue;
    });

    setTimeout(
      function(){
        jf.filed( testFile4Path, function( obj ) {
          expect(obj).to.eql(initialValue);
          obj.msg = "this value is not written also."
          return null
        });
      },
      1000
    );

    setTimeout(
      function(){
        jf.filed( testFile4Path, function( obj ) {
          expect(obj).to.eql(initialValue);
          done();
        });
      },
      1000
    );

  });
});

describe('Null file Path', function () {
  it('should cause JsonFiledError', function (done) {
    try{
      jf.filed( null, function( obj ) {
      });
    }catch(e){
      expect(e).to.be.an.instanceof( jf.JsonFiledError );
      done();
    }
  });
});

@@ -2673,16 +2673,509 @@
     %7D%0A  %7D);%0A%7D);%0A
+%0Aconst testFile5Path = './' + Math.random() + '.xson';%0Avar value_in_xson = %7B msg: %22format is json%22 %7D;%0Adescribe('File with extension neither json nor bson ', function () %7B%0A  it('should be json.', function (done) %7B%0A    jf.filed( testFile5Path, function( obj ) %7B%0A      return value_in_xson;%0A    %7D);%0A%0A    setTimeout(%0A      function()%7B%0A        jf.filed( testFile5Path, function( obj ) %7B%0A          expect(obj).to.eql( value_in_xson );%0A          done();%0A        %7D);%0A      %7D,%0A      1000);%0A    %7D)%0A%0A%7D);%0A

add test case of .xson to json format