Testing for Beginners

  it('invokes callback when a button is clicked', () => {
    let votedWith;
    const vote = (entry) => { votedWith = entry; };
    const callback = sinon.spy();

    const component = shallow(
      <Vote pair={ List.of('Trainspotting', '28 Days Later') } vote={ callback } />
    );
    component.find('button').at(0).simulate('click');

    // expect(votedWith).to.equal('Trainspotting');
    expect(callback.calledWith('Trainspotting')).to.equal(true);
  });

Spy example

Sinon

Mocha: Test "Framework" that runs in Node

allows you to run mocha from the CLI

 

In code gives you things like "describe, and it"

 

chai gives you a way to test assertions with a vocabularity that makes sense.

"expect(myVar).to.equal(true);"

 

Sinon helps with those confusing words like, Stubs, Mocks, Fakes, spies

 

 

When testing UI, you have to use tools that "simulate" the DOM, since you are running these tests outside of the DOM (in Node) 

 

PhantomJS spins up a "headless" dom.

Selenium runs a DOM and copies actual actions that you took...these aren't unit tests, they are integration testing.

 

JSDom is a popular lib used to simulate DOM functions without actually running a full DOM simulation. This is how you handle unit tests for UI usually.

End to End starts by going to an actual URL and taking every action needed to do what you are testing from the user perspective

describe('angularjs homepage todo list', function() {
  it('should add a todo', function() {
    browser.get('https://angularjs.org');

    element(by.model('todoList.todoText')).sendKeys('write first protractor test');
    element(by.css('[value="add"]')).click();

    var todoList = element.all(by.repeater('todo in todoList.todos'));
    expect(todoList.count()).toEqual(3);
    expect(todoList.get(2).getText()).toEqual('write first protractor test');

    // You wrote your first test, cross it off the list
    todoList.get(2).element(by.css('input')).click();
    var completedAmount = element.all(by.css('.done-true'));
    expect(completedAmount.count()).toEqual(2);
  });
});
Made with Slides.com