import { userMessage } from './messageReducers';
import * as types from './messageActionTypes';
import expect from 'expect.js';

describe('userMessages Reducer', function() {
  describe(`userMessages() -  ${ types.SEND_MESSAGE }`, function() {
    const testUid = 123;
    const testMessage = 'a test message';
    const testCreatedAt = 999343454;

    const result = userMessage(
      undefined, {
        type: types.SEND_MESSAGE,
        uid: testUid,
        message: testMessage,
        createdAt: testCreatedAt
      });

    it('is expected to return an object', function() {
      expect(result).to.be.an('object');
    });
    it('is expected to return an object with items array', function() {
      expect(result.items).to.be.an('array');
    });

    describe('items[] array in userMessage', function() {
      it('is expected to have length 1', function() {
        expect(result.items).to.have.length(1);
      });
      it('is expected to have length 2 after next action', function() {
        expect(
          userMessage(
            result, {
              type: types.SEND_MESSAGE,
              uid: testUid,
              message: testMessage,
              createdAt: testCreatedAt
            }).items
        ).to.have.length(2);
      })
    });

    it('is expected to return the uid, message and createdAt properties from action', function() {
      expect(result.items[0].uid).to.be(testUid);
      expect(result.items[0].message).to.be(testMessage);
      expect(result.items[0].createdAt).to.be(testCreatedAt);
    });
    it('is expected to trigger isSending to true', function() {
      expect(result.isSending).to.be(true);
    });
  });

  describe(`userMessages() -  ${ types.SEND_MESSAGE_SUCCESS }`, function() {
    it('is expected to return an object', function() {
      expect(
        userMessage(undefined, {
          type: types.SEND_MESSAGE_SUCCESS
        })
      ).to.be.an('object');
    });
    it('is expected to trigger isSending to false', function() {
      expect(
        userMessage(undefined, {
          type: types.SEND_MESSAGE_SUCCESS
        }).isSending
      ).to.be(false);
    });
  });

  describe(`userMessages() -  ${ types.SEND_MESSAGE_ERROR }`, function() {
    it('is expected to return an object', function() {
      expect(userMessage(undefined, {
        type: types.SEND_MESSAGE_ERROR
      })).to.be.an('object');
    });
    it('is expected to trigger hasError to true', function() {
      expect(userMessage(undefined, {
        type: types.SEND_MESSAGE_ERROR
      }).hasError).to.be(true);
    });
    it('is expected to add errorMessage to the state', function() {
      const testErrorMessage = 'this is test error';
      const errorState = userMessage(
        undefined,
        {
          type: types.SEND_MESSAGE_ERROR,
          errorMessage: testErrorMessage
        }
      );

      expect(errorState.errorMessage).to.be(testErrorMessage);
    });
  });
});

@@ -13,16 +13,26 @@
 rMessage
+, messages
  %7D from 
@@ -2953,12 +2953,1547 @@
 ;%0A  %7D);%0A%7D);%0A
+%0Adescribe('messages Reducer', function() %7B%0A  describe(%60messages() - $%7B types.RETRIEVE_MESSAGE %7D%60, function() %7B%0A    it('is expected to return an array', function() %7B%0A      expect(%0A        messages(undefined, %7B type: types.RETRIEVE_MESSAGE %7D)%0A      ).to.be.an('array');%0A    %7D);%0A    it('is expected to add the message and uid props as an object to array', function() %7B%0A      const testUid = 123;%0A      const testMessage = 'this is a test message';%0A%0A      const result = messages(%0A        undefined,%0A        %7B%0A          type: types.RETRIEVE_MESSAGE,%0A          message: testMessage,%0A          uid: testUid%0A        %7D%0A      );%0A%0A      expect(result).to.have.length(1);%0A      expect(result%5B0%5D.message).to.be(testMessage);%0A      expect(result%5B0%5D.uid).to.be(testUid);%0A%0A    %7D);%0A    it('is expected to add the second message with keeping a previous state', function() %7B%0A      const testUid = 123;%0A      const testMessage = 'this is a test message';%0A%0A      const testUid2 = 123;%0A      const testMessage2 = 'this is a test message';%0A%0A      const result = messages(%0A        %5B%0A          %7B%0A            uid: testUid,%0A            message: testMessage%0A          %7D%0A        %5D,%0A        %7B%0A          type: types.RETRIEVE_MESSAGE,%0A          message: testMessage2,%0A          uid: testUid2%0A        %7D%0A      );%0A%0A      expect(result).to.have.length(2);%0A      expect(result%5B0%5D.message).to.be(testMessage);%0A      expect(result%5B0%5D.uid).to.be(testUid);%0A%0A      expect(result%5B1%5D.message).to.be(testMessage2);%0A      expect(result%5B1%5D.uid).to.be(testUid2);%0A%0A    %7D);%0A  %7D);%0A%7D);%0A

write tests for messages reducer - retrieving message