Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Unit test a React component (& reviews)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmaschta committed Feb 29, 2016
1 parent 2271553 commit 5a1b725
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"co-body": "~4.0.0",
"co-fs": "~1.2.0",
"co-pg": "~1.3.1",
"compass-mixins": "^0.12.7",
"compass-mixins": "~0.12.7",
"config": "~1.16.0",
"css-loader": "~0.16.0",
"db-migrate": "~0.10.0-beta.7",
Expand Down Expand Up @@ -87,8 +87,10 @@
"babel-preset-react-hmre": "~1.1.0",
"babel-preset-stage-0": "~6.3.13",
"chai": "~3.3.0",
"chai-enzyme": "~0.4.1",
"cheerio": "~0.20.0",
"co-mocha": "~1.1.2",
"enzyme": "~1.2.0",
"enzyme": "~2.0.0",
"eslint": "~1.6.0",
"eslint-config-airbnb": "~0.1.0",
"eslint-plugin-mocha": "~1.0.0",
Expand Down
39 changes: 39 additions & 0 deletions src/frontend/js/product/ProductItem.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import chai, { assert } from 'chai';
import { shallow } from 'enzyme';
import chaiEnzyme from 'chai-enzyme';
import React from 'react';

import '../test/setupJsdom';
import ProductItem from './ProductItem';

chai.use(chaiEnzyme());

describe('Component ProductItem', () => {
const props = {
id: 42,
price: 28.53,
reference: 'REF-654564',
thumbnail: 'http://google.fr/image.jpg',
description: 'Best product ever',
};

it('should display the correct reference', () => {
const wrapper = shallow(<ProductItem {...props} />);
assert.deepEqual(wrapper.find('h4').text(), 'REF-654564');
});

it('should display the correct price', () => {
const wrapper = shallow(<ProductItem {...props} />);
assert.deepEqual(wrapper.find('h6').text(), '$28.53');
});

it('should display the correct description', () => {
const wrapper = shallow(<ProductItem {...props} />);
assert.deepEqual(wrapper.find('p.card-text').text(), 'Best product ever');
});

it('should display the correct thumbnail', () => {
const wrapper = shallow(<ProductItem {...props} />);
assert.deepEqual(wrapper.find('img').node.props.src, 'http://google.fr/image.jpg');
});
});
7 changes: 7 additions & 0 deletions src/frontend/js/test/setupJsdom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import jsdom from 'jsdom';

global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = {
userAgent: 'node.js',
};

0 comments on commit 5a1b725

Please sign in to comment.