1.1.8 • Published 9 years ago

react-tc v1.1.8

Weekly downloads
2
License
ISC
Repository
github
Last release
9 years ago

##This modules have 2 export functions:

  • setup - setup react component for testing
  • tc(test component) - test component by type & props & children length
import { setup, tc } from 'react-tc';
import React, { Component, PropTypes } from 'react';
import expect from 'expect';
import styles from './styles'; // scss file (if you use css modules)

/**
 * Simple component
 */
class YouComponent extends Component {

  static propTypes = {
    items: PropTypes.arrayOf(PropTypes.object).isRequired,
    callback: PropTypes.func.isRequired
  };

  render() {
    
    const { callback, items } = this.props;
  
    return (
      <div className={styles.general} onClick={callback}>
        {
          items.map((item) => {
            return (
              <h1 key={item.id} className={styles.item}>{item.name}</h1>
            );
          })
        }
      </div>
    );

  }

}

// %%%%%%%% TEST %%%%%%%%%%

/**
 * @return 
 *  {object} output - shallow render element
 *  {object} props - you passed props
 *  {object} context - you passed context
 */
const { output, props, context } = setup(YouComponent, {
  items: [{
    id: 1,
    name: 'one'
  }, {
    id: 2,
    name: 'two'
  }],
  callback: expect.createSpy()
});

/**
 * test first layer of output by their props and child length
 * if you child undefined, null, empty array [] - this is equal with 0 or miss 4 parameter tc function
 */
tc(output, 'div', {
  className: styles.general,
  onClick: props.callback
}, 2);

expect(props.callback.calls.length).toBe(0);
output.props.onClick();
expect(props.callback.calls.length).toBe(1);

const [item1, item2] = output.props.children;

tc(item1, 'h1', {
  children: props.items[0].name,
  className: styles.item
}, 1);

tc(item2, 'h1', {
  children: props.items[1].name,
  className: styles.item
}, 1);
1.1.8

9 years ago

1.1.7

9 years ago

1.1.6

9 years ago

1.1.5

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago