Skip to content

Commit e61124a

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 5d11e0b commit e61124a

12 files changed

Lines changed: 19579 additions & 296 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ package-lock.json
88
src/api
99
build
1010
dist
11+
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
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"dev": "nodemon --exec babel-node src/app.js ",
8+
"test:watch": "jest --watchAll",
89
"coverage": "nyc report --reporter=text-lcov | coveralls",
910
"build": "webpack --mode production ",
1011
"heroku-postbuild": "npm run build",
@@ -13,13 +14,11 @@
1314
"precommit": "lint-staged",
1415
"test": ""
1516
},
16-
1717
"lint-staged": {
1818
"*.{js,jsx}": [
19-
"git add"
19+
"git add"
2020
]
21-
},
22-
21+
},
2322
"nyc": {
2423
"require": [
2524
"@babel/register"
@@ -35,13 +34,16 @@
3534
"author": "",
3635
"license": "ISC",
3736
"dependencies": {
37+
"@testing-library/jest-dom": "^5.1.1",
38+
"@testing-library/react": "^9.4.0",
3839
"apollo-boost": "^0.4.4",
3940
"babel-preset-env": "^2.0.0-alpha.20",
4041
"bcryptjs": "^2.4.3",
4142
"chai-http": "^4.2.0",
4243
"classnames": "^2.2.6",
4344
"express": "^4.16.3",
4445
"graphql": "^14.5.8",
46+
"jest": "^25.1.0",
4547
"joi": "^14.0.1",
4648
"jsonwebtoken": "^8.3.0",
4749
"mongoose": "^5.3.6",
@@ -52,6 +54,7 @@
5254
"react-apollo": "^3.1.3",
5355
"react-router": "^5.0.1",
5456
"react-router-dom": "^5.0.1",
57+
"react-test-renderer": "^16.12.0",
5558
"swagger-ui-express": "^4.0.1"
5659
},
5760
"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)