Skip to content

Commit ecd049a

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 ecd049a

File tree

12 files changed

+19577
-299
lines changed

12 files changed

+19577
-299
lines changed

.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: npm run 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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
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 --watchAll",
99
"coverage": "nyc report --reporter=text-lcov | coveralls",
1010
"build": "babel src --out-dir build ",
1111
"heroku-postbuild": "npm run build",
1212
"start": "node build/app.js",
1313
"webpack": "webpack",
1414
"precommit": "lint-staged"
1515
},
16-
1716
"lint-staged": {
1817
"*.{js,jsx}": [
19-
20-
"git add"
18+
"git add"
2119
]
22-
},
23-
20+
},
2421
"nyc": {
2522
"require": [
2623
"@babel/register"
@@ -36,13 +33,16 @@
3633
"author": "",
3734
"license": "ISC",
3835
"dependencies": {
36+
"@testing-library/jest-dom": "^5.1.1",
37+
"@testing-library/react": "^9.4.0",
3938
"apollo-boost": "^0.4.4",
4039
"babel-preset-env": "^2.0.0-alpha.20",
4140
"bcryptjs": "^2.4.3",
4241
"chai-http": "^4.2.0",
4342
"classnames": "^2.2.6",
4443
"express": "^4.16.3",
4544
"graphql": "^14.5.8",
45+
"jest": "^25.1.0",
4646
"joi": "^14.0.1",
4747
"jsonwebtoken": "^8.3.0",
4848
"mongoose": "^5.3.6",
@@ -53,6 +53,7 @@
5353
"react-apollo": "^3.1.3",
5454
"react-router": "^5.0.1",
5555
"react-router-dom": "^5.0.1",
56+
"react-test-renderer": "^16.12.0",
5657
"swagger-ui-express": "^4.0.1"
5758
},
5859
"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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 = [{url: 'sample.png'}]
19+
const { getByTestId } = render(
20+
<Card featured_image={image} description='testing' title='new component' tags="test"/>
21+
);
22+
23+
expect(getByTestId("card-component")).toHaveTextContent("new component")
24+
});
25+
});

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)