Let's Talk About Testing

Types of Testing

- Unit Testing - Integration Testing - Functional Testing - System Testing - Stress Testing - Performance Testing - Usability Testing - Acceptance Testing - Regression Testing

Unit Testing

Testing a specific unit of code

Responsible: Dev

Integration Testing

Testing how units of code work together

Responsible: Dev

Functional Testing

System functionality requirements are met.

Responsible: QA

Performance Testing

Testing how quickly things are working

Responsible: DevOps, Dev

Why unit test?

Encourages smaller and more modular code?

Built in documentation

More robust (less brittle) code base.

Easier to extend and refactor

What to unit test?

Three Rules

1. Will the test exactly duplicate the code it is meant to test?

2. Will the test duplicate behavior already covered by library code?

3. Does what I am testing matter to the component's public API? (Contract Testing)

Contract Based Testing

What is the contract associated with this piece of code? What is expected of it? What affects that contract?

Example:

							
export const getInitials = (name) => {
  if (!name) {
    return '';
  }
  let ownerName = _.compact(_.pull(_.split(name, ' '), 'true', 'false'));
  return `${_.head(ownerName[0])}${_.head(ownerName[ownerName.length - 1])}`.toUpperCase();
};

export const Avatar = (props) => {

  const { size, name, profileImage, hovering } = props;

  const initialsStyle = {
    fontSize: size / 2
  };

  const avatarStyle = {
    width: size,
    height: size,
    minWidth: size,
    lineHeight: `${size}px`
  };

  return (
    
{profileImage ?
:
{getInitials(name)}
}
); };

Component Contracts

Render

Conditionally render based on props

Take a string and genereate initials from it.


import React from 'react';
import { expect, assert } from 'chai';
import { shallow } from 'enzyme';
import { Avatar, getInitials } from './Avatar';

const wrapper = shallow();

describe('(Component) Avatar', () => {
  it(' renders', () => {
    expect(wrapper).to.have.lengthOf(1);
  });
  describe('(Function) getInitials', () => {
    it('getInitials is a function', () => {
      assert.typeOf(getInitials, 'function', 'we have a function');
    });
    const name = 'GoGrow Dev';
    it('which should return turn GoGrow Dev into GD', () => {
      assert.equal('GD', getInitials(name));
    });
    const trueName = 'Account Name true true';
    it('should filter out true vaules', () => {
      assert.equal('AN', getInitials((trueName)));
    });
    const falseName = 'Account Name false false';
    it('should filter out false values', () => {
      assert.equal('AN', getInitials(falseName));
    });
    const trueFalseName = 'Account Name true false';
    it('should filter out true and false', () => {
      assert.equal('AN', getInitials(trueFalseName));
    });
  });
});
							
						

Resources: