bntzio
8/21/2017 - 10:18 PM

HexCandy.com AddCandy test

HexCandy.com AddCandy test

var React = require('react');
var ReactDOM = require('react-dom');
var $ = require('jquery');
var TestUtils = require('react-addons-test-utils');
var expect = require('expect');

import * as actions from 'actions';
var { AddCandy } = require('AddCandy');

describe('AddCandy', () => {
  it('should exist', () => {
    expect(AddCandy).toExist();
  });

  it('should not dispatch ADD_CANDY with invalid candy name or value', () => {
    var candyName = '';
    var candyValue = '';
    var action = {
      type: 'ADD_CANDY',
      name: candyName,
      value: candyValue
    };
    var spy = expect.createSpy();
    var addCandy = TestUtils.renderIntoDocument(<AddCandy dispatch={spy} />);
    var $el = $(ReactDOM.findDOMNode(addCandy));

    addCandy.refs.candyName.value = candyName;
    addCandy.refs.candyValue.value = candyValue;

    TestUtils.Simulate.submit($el.find('form')[0]);

    expect(spy).toNotHaveBeenCalled();
  });

  it('should not dispatch ADD_CANDY with invalid candy name', () => {
    var candyName = '';
    var candyValue = '#ed317b';
    var action = {
      type: 'ADD_CANDY',
      name: candyName,
      value: candyValue
    };
    var spy = expect.createSpy();
    var addCandy = TestUtils.renderIntoDocument(<AddCandy dispatch={spy} />);
    var $el = $(ReactDOM.findDOMNode(addCandy));

    addCandy.refs.candyName.value = candyName;
    addCandy.refs.candyValue.value = candyValue;

    TestUtils.Simulate.submit($el.find('form')[0]);

    expect(spy).toNotHaveBeenCalled();
  });

  it('should not dispatch ADD_CANDY with invalid candy value', () => {
    var candyName = 'My Candy';
    var candyValue = '';
    var action = {
      type: 'ADD_CANDY',
      name: candyName,
      value: candyValue
    };
    var spy = expect.createSpy();
    var addCandy = TestUtils.renderIntoDocument(<AddCandy dispatch={spy} />);
    var $el = $(ReactDOM.findDOMNode(addCandy));

    addCandy.refs.candyName.value = candyName;
    addCandy.refs.candyValue.value = candyValue;

    TestUtils.Simulate.submit($el.find('form')[0]);

    expect(spy).toNotHaveBeenCalled();
  });

  it('should dispatch ADD_CANDY with valid name and value', () => {
    var candyName = 'My Candy';
    var candyValue = '#ed317b';
    var action = actions.startAddCandy(candyName, candyValue);
    var spy = expect.createSpy();
    var addCandy = TestUtils.renderIntoDocument(<AddCandy dispatch={spy} />);
    var $el = $(ReactDOM.findDOMNode(addCandy));

    addCandy.refs.candyName.value = candyName;
    addCandy.refs.candyValue.value = candyValue;

    TestUtils.Simulate.submit($el.find('form')[0]);

    expect(spy).toHaveBeenCalledWith(action);
  });

  it('should not dispatch ADD_CANDY if candy value if not a hex value', () => {
    var candyName = 'My Candy';
    var candyValue = '#ed317z';
    var action = {
      type: 'ADD_CANDY',
      name: candyName,
      value: candyValue
    };
    var spy = expect.createSpy();
    var addCandy = TestUtils.renderIntoDocument(<AddCandy dispatch={spy} />);
    var $el = $(ReactDOM.findDOMNode(addCandy));

    addCandy.refs.candyName.value = candyName;
    addCandy.refs.candyValue.value = candyValue;

    TestUtils.Simulate.submit($el.find('form')[0]);

    expect(spy).toNotHaveBeenCalled();
  });
});