Skip to content

Commit

Permalink
[ft #[163518661]] add tests for bookmark component
Browse files Browse the repository at this point in the history
  • Loading branch information
espoirMur committed Apr 29, 2019
1 parent 34d8da7 commit af4bb40
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/__actions__/bookmarkActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("test login action", () => {
});
});

describe.only("should called like actions creators", () => {
describe("should called like actions creators", () => {
const articleSlug = "a-fake-slug";
const BOOKMARK_URL = `${
process.env.API_BASE_URL
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions src/__tests__/__components__/Bookmark.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "@babel/polyfill";
import React from "react";
import { shallow } from "enzyme";
import Bookmark from "../../components/common/Bookmarks";

describe("test bookmark copmponent", () => {
const setUp = props => shallow(<Bookmark {...props} />);
const props = {
icon: "etst",
onClick: jest.fn()
};
it("should render the component", () => {
const bookmarkComponent = setUp(props);
const imgElement = bookmarkComponent
.find("div")
.at(0)
.find("img");
expect(imgElement.length).toBe(1);
// expect(bookmarkComponent.hasClass("share-icon")).to.equal(true);
expect(bookmarkComponent.find("div").length).toBe(1);
});
});
6 changes: 3 additions & 3 deletions src/__tests__/__reducers__/bookmarkReducers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ describe("test bookmark reducers", () => {
});
});

it("should handle error when bookmarking", () => {
it("should handle errors when bookmarking", () => {
expect(
bookmarkReducer(initialState, {
type: REQUEST_FAILED,
payload: "an error occurs while bookmarking"
payload: "An error occurs while bookmarking"
})
).toEqual({
...initialState,
message: "an error occurs while bookmarking",
message: "An error occurs while bookmarking",
isSubmitting: false
});
});
Expand Down
73 changes: 73 additions & 0 deletions src/__tests__/__views__/Bookmark.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import "@babel/polyfill";
import React from "react";
import { mount } from "enzyme";
import toJson from "enzyme-to-json";
import {
Bookmark,
mapStateToProps,
mapDispatchToProps
} from "../../views/Bookmark";
import { initialState } from "../../redux/reducers/bookmarkReducers";

const setup = () => {
const props = {
bookmark: jest.fn(),
isBookmarked: false
};
const enzymeWrapper = mount(<Bookmark {...props} />);

return {
props,
enzymeWrapper
};
};

describe("test bookmark component", () => {
const { props } = setup();
const bookmarkComponent = mount(<Bookmark {...props} />);
let instance;
beforeEach(() => {
instance = bookmarkComponent.instance();
jest.spyOn(instance.props, "bookmark");
instance.forceUpdate();
});
afterEach(() => {
jest.restoreAllMocks();
});
it("snapshot testing for the component", () => {
expect(toJson(bookmarkComponent)).toMatchSnapshot();
expect(bookmarkComponent).toBeTruthy();
});

describe("handle event on bookmark icon", () => {
it("should handle click bookmark event ", () => {
const bookmarkImg = bookmarkComponent.find("img");
bookmarkImg.simulate("click", {});
expect(instance.props.bookmark).toHaveBeenCalled();
});

it("should handle cancel bookmark if bookmarked", () => {
props.isBookmarked = true;
const bookmarkedComponent = mount(<Bookmark {...props} />);
const bookmarkedImg = bookmarkedComponent.find("img");
bookmarkedImg.simulate("click", {});
expect(instance.props.bookmark).toHaveBeenCalled();
});
});

describe("test map state to props and map dispatch to props", () => {
it("should test map state to props", () => {
expect(mapStateToProps({ bookmark: initialState })).toEqual({
isSubmitting: false,
message: "",
isBookmarked: false
});
});
it("should test map dispatch to props for bookmark", () => {
// can be improved , should ask how to do it
const dispatch = jest.fn();
mapDispatchToProps(dispatch).bookmark();
expect(dispatch.mock.calls[0][0]).toBeInstanceOf(Function);
});
});
});
26 changes: 26 additions & 0 deletions src/__tests__/__views__/__snapshots__/Bookmark.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test bookmark component snapshot testing for the component 1`] = `
<Bookmark
bookmark={[MockFunction]}
isBookmarked={false}
>
<div>
<BookmarkComponent
icon="test-file-stub"
onClick={[MockFunction]}
>
<div>
<img
alt="logo"
className="share-icon"
onClick={[MockFunction]}
onKeyUp={[Function]}
src="test-file-stub"
/>
</div>
</BookmarkComponent>
</div>
</Bookmark>
`;

0 comments on commit af4bb40

Please sign in to comment.