codeandcognition / koconut

a very cool assess pool
2 stars 0 forks source link

Write unit tests for existing classes #94

Open kwokwilliam opened 6 years ago

kwokwilliam commented 6 years ago

I have been looking into writing unit tests for React components.

It's done using a library called Enzyme, created by Airbnb (http://airbnb.io/enzyme/).

I'll start writing a few tests. I've never really wrote any unit tests before, so this'll be new. I'm sure it'll be very useful in the future for expansion.

kwokwilliam commented 6 years ago

Depending on the code, I may modify implementations to use dependency injections, so we can mock dependencies using jest.

kwokwilliam commented 6 years ago

Very seamless to use. This is the code for one test, and it's very straightforward to use. getOrderedConcepts() is a function in wrapper which is defined as a shallow(<WorldView />).

  test('getOrderedConcepts contains only concepts in ConceptAbbreviations', () => {
    const concepts = wrapper.instance().getOrderedConcepts();
    concepts.forEach((concept) => {
      expect(g[concept.name] !== undefined).toBe(true);
    });
  });
kwokwilliam commented 6 years ago

Added a CSS proxy because tests were crashing when trying to import objects with a CSS import at the top.

https://github.com/facebook/jest/issues/3094#issuecomment-385164816

kwokwilliam commented 6 years ago

Wrote guide a couple days ago for writing unit tests with Enzyme https://github.com/codeandcognition/koconut/blob/summer2018-master/docs/jest.md

kwokwilliam commented 6 years ago

Fix existing unit tests with new router change

bxie commented 5 years ago

test suite exists in src/tests, but they're out of date and incomplete