'use strict';

var proxyquire = require('proxyquire'),
    should = require('should'),
    sinon = require('sinon'),
    marshaler = {
        marshal: function() {}
    },
    marshalChain = function() {
        return marshaler;
    },
    sut = proxyquire('../index', {
        './lib/marshalChain': marshalChain
    });

describe('marshaler', function() {
    var marshalStub;

    beforeEach(function() {
        marshalStub = sinon.stub(marshaler, 'marshal');
    });

    afterEach(function() {
        marshalStub.restore();
    });

    describe('marshalJson()', function() {
        it('should throw TypeError if item passed in is not an object literal', function() {
            sut.marshalItem.bind(null, 'foo').should.throw(TypeError, {message: 'Item must be plain object literal'});
        });

        it('should delegate to marshaler.marshal to convert item to dynamoDb format', function() {
            var item = {},
                expected = {},
                marshaledItem = {M: expected},
                actual;

            marshalStub.withArgs(item).returns(marshaledItem);

            actual = sut.marshalItem(item);

            marshalStub.calledOnce.should.equal(true);

            actual.should.eql(expected);
        });
    });

    describe('marshalJson()', function() {
        it('should parse a json string and delegate to marshaler.marshal', function() {
            var json = '{\"foo\": \"bar\"}',
                item = {foo: 'bar'},
                expected = {},
                marshaledItem = {M: expected},
                actual;

            marshalStub.withArgs(item).returns(marshaledItem);

            actual = sut.marshalJson(json);

            marshalStub.calledOnce.should.equal(true);

            actual.should.eql(expected);
        });
    });
});

@@ -189,36 +189,58 @@
 n = 
-function() %7B%0A        
+sinon.stub();%0A%0AmarshalChain.onFirstCall().
 return
- 
+s(
 mars
@@ -248,20 +248,15 @@
 aler
+)
 ;%0A
-    %7D,%0A   
+%0Avar
  sut
@@ -283,20 +283,16 @@
 dex', %7B%0A
-    
     './l
@@ -322,20 +322,16 @@
 alChain%0A
-    
 %7D);%0A%0Ades

index testing setup.