Skip to content

Commit

Permalink
tests: improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gabsprates authored and rodrigo-brito committed Oct 14, 2019
1 parent d8b663e commit 78cacae
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions __mocks__/react-copy-to-clipboard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const CopyToClipboard = ({ children, onCopy, text }) => (
<div onClick={() => onCopy(text)} >
{children}
</div>
);

module.exports = {
CopyToClipboard
};
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"coverage": "yarn test -- --coverage"
},
"jest": {
"coverageReporters": [
"html",
"json",
"lcov",
"text",
"clover"
]
}
}
20 changes: 20 additions & 0 deletions src/components/converter.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ it('updates the state on the onChange event', () => {

expect(converter.state.result).toBe(expectedValue);
});

it('should show "copied" text copy action', () => {
const converter = ReactTestUtils.renderIntoDocument(<Converter />);
const textarea = ReactTestUtils.findRenderedDOMComponentWithTag(converter, 'textarea');

jest.spyOn(converter, 'setCopiedToTrue');

const initialValue = '{ "a" : true }';
textarea.value = initialValue;
const expectedValue = jsonToGoMap(initialValue);

ReactTestUtils.Simulate.change(textarea);

expect(converter.setCopiedToTrue).not.toHaveBeenCalled();
const button = ReactTestUtils.findRenderedDOMComponentWithTag(converter, 'button');

ReactTestUtils.Simulate.click(button);

expect(converter.setCopiedToTrue).toHaveBeenCalledWith(expectedValue);
});
2 changes: 1 addition & 1 deletion src/lib/__snapshots__/jsonToGoMap.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`the jsonToGoMap converter when a simple key/value object is given, it s
}"
`;

exports[`the jsonToGoMap converter when array is given, it should return a interface list 1`] = `
exports[`the jsonToGoMap converter when array is given, it should return an interface list 1`] = `
"[]interface{}{
\\"A\\",
\\"B\\",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/jsonToGoMap.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import jsonToMap from "./jsonToGoMap";
describe("the jsonToGoMap converter", () => {
test("when a empty string is given, it should return a empty result", () => {
test("when an empty value is given, it should return an empty string", () => {
const result = jsonToMap("");
expect(result).toEqual("");
});

test("when a invalid JSON is given, it should return a error mensage", () => {
test("when an invalid JSON is given, it should return an error mensage", () => {
const result = jsonToMap(`{"test": invalid`);
expect(result).toEqual("Unexpected token i in JSON at position 9");
});

test("when a simple key/value object is given, it should return a valid result", () => {
const result = jsonToMap(`{"key": "value"}`);
expect(result).toMatchSnapshot();
Expand All @@ -20,7 +20,7 @@ describe("the jsonToGoMap converter", () => {
expect(result).toMatchSnapshot();
});

test("when array is given, it should return a interface list", () => {
test("when array is given, it should return an interface list", () => {
const result = jsonToMap(`["A", "B", 1]`);
expect(result).toMatchSnapshot();
});
Expand Down
2 changes: 2 additions & 0 deletions src/pages/home.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import ReactDOM from 'react-dom';
import Home from './home';

jest.mock('react-copy-to-clipboard');

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<Home />, div);
Expand Down

0 comments on commit 78cacae

Please sign in to comment.