-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ft #[163518661]] add tests for bookmark component
- Loading branch information
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/__tests__/__views__/__snapshots__/Bookmark.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`; |