Skip to content

Commit 7333075

Browse files
author
devPinheiro
committed
chore(test): Integrate testing library
- Add Jest test framework - Add Reac Testing Library - Add Jest config file - Add Card Component unit tests - Add Image Component unit test
1 parent d8987d7 commit 7333075

12 files changed

Lines changed: 19582 additions & 299 deletions

File tree

.babelrc

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,5 @@
22
"presets": [
33
"env",
44
"@babel/preset-react"
5-
],
6-
"env": {
7-
"test": {
8-
"plugins": [
9-
"istanbul"
10-
]
11-
},
12-
"production": {
13-
"plugins": [
14-
"istanbul"
15-
]
16-
}
17-
}
5+
]
186
}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ node_modules
55
coverage
66
docs.md
77
package-lock.json
8-
src/api
8+
src/api
9+
build
10+
yarn.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
33
- node
4-
script: npm run test:coverage
4+
script: yarn test

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
collectCoverage: true,
3+
// Automatically clear mock calls and instances between every test
4+
clearMocks: true,
5+
testPathIgnorePatterns: ['/node_modules/', '/cypress/','build']
6+
};

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
"main": "index.js",
66
"scripts": {
77
"dev": "nodemon --exec babel-node src/app.js ",
8-
"test": "cross-env NODE_env=production nyc mocha --require babel-core/register src/**/test.js --timeout 8000 --exit",
8+
"test": "jest",
9+
"test:watch": "jest --watchAll",
910
"coverage": "nyc report --reporter=text-lcov | coveralls",
1011
"build": "babel src --out-dir build ",
1112
"heroku-postbuild": "npm run build",
1213
"start": "node build/app.js",
1314
"webpack": "webpack",
1415
"precommit": "lint-staged"
1516
},
16-
1717
"lint-staged": {
1818
"*.{js,jsx}": [
19-
20-
"git add"
19+
"git add"
2120
]
22-
},
23-
21+
},
2422
"nyc": {
2523
"require": [
2624
"@babel/register"
@@ -36,13 +34,16 @@
3634
"author": "",
3735
"license": "ISC",
3836
"dependencies": {
37+
"@testing-library/jest-dom": "^5.1.1",
38+
"@testing-library/react": "^9.4.0",
3939
"apollo-boost": "^0.4.4",
4040
"babel-preset-env": "^2.0.0-alpha.20",
4141
"bcryptjs": "^2.4.3",
4242
"chai-http": "^4.2.0",
4343
"classnames": "^2.2.6",
4444
"express": "^4.16.3",
4545
"graphql": "^14.5.8",
46+
"jest": "^25.1.0",
4647
"joi": "^14.0.1",
4748
"jsonwebtoken": "^8.3.0",
4849
"mongoose": "^5.3.6",
@@ -53,6 +54,7 @@
5354
"react-apollo": "^3.1.3",
5455
"react-router": "^5.0.1",
5556
"react-router-dom": "^5.0.1",
57+
"react-test-renderer": "^16.12.0",
5658
"swagger-ui-express": "^4.0.1"
5759
},
5860
"devDependencies": {

src/client/components/Card/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33

44
const Card = ({ featured_image, title, description, tags, ...rest }) => {
55
return (
6-
<div className="max-w-sm m-2 rounded overflow-hidden shadow-lg">
6+
<div className="max-w-sm m-2 rounded overflow-hidden shadow-lg" data-testid="card-component">
77
<img
88
className="w-full"
99
src={
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import { render, cleanup } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import Card from './index';
5+
6+
afterEach(cleanup);
7+
8+
describe('Card Component Unit Testing', () => {
9+
it('Should render component when mounted', () => {
10+
const { getByTestId } = render(
11+
<Card featured_image="test.png" description="testing" title="new component" tags="test" />
12+
);
13+
14+
expect(getByTestId('card-component')).toHaveTextContent('new component');
15+
});
16+
17+
it('Should render component when mounted with conditional prop', () => {
18+
const image = [
19+
{
20+
url: 'sample.png',
21+
},
22+
];
23+
const { getByTestId } = render(
24+
<Card featured_image={image} description="testing" title="new component" tags="test" />
25+
);
26+
27+
expect(getByTestId('card-component')).toHaveTextContent('new component');
28+
});
29+
});

src/client/components/Image/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
44
const Image = ({src, alt, className }) => {
55
return (
66
<img
7+
data-testid="image-component"
78
src={src}
89
alt={alt}
910
className={className}
@@ -14,7 +15,7 @@ const Image = ({src, alt, className }) => {
1415
Image.propTypes = {
1516
src: PropTypes.string,
1617
alt: PropTypes.string,
17-
className: PropTypes.sttring,
18+
className: PropTypes.string,
1819
};
1920

2021
export default Image
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react";
2+
import { render, cleanup } from "@testing-library/react";
3+
import "@testing-library/jest-dom"
4+
import Image from "./index";
5+
6+
afterEach(cleanup);
7+
8+
describe("Image Component Unit Testing", () => {
9+
it("Should render component when mounted", () => {
10+
const { getByTestId} = render(
11+
<Image className="bg-gray-500" src='test' alt="test"/>
12+
);
13+
14+
expect(getByTestId("image-component")).toHaveAttribute('alt', 'test')
15+
});
16+
});

0 commit comments

Comments
 (0)