-
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.
[Feature #163518664] add components tests
- Add test case for highlighting text on read article component - Add test for comment model and HighlightPopover - Adjust the padding and margin for close button [Starts #163518664]
- Loading branch information
Showing
7 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/__tests__/__components__/PopOvers/CommentModel.test.js
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,62 @@ | ||
import React from "react"; | ||
import { shallow } from "enzyme"; | ||
import toJson from "enzyme-to-json"; | ||
import { CommentModel } from "../../../components/PopOvers/CommentModel"; | ||
import Button from "../../../components/common/Buttons/FormButton"; | ||
|
||
const onClose = jest.fn(); | ||
const props = { | ||
isOpen: false, | ||
onClose, | ||
id: "the-comment-model" | ||
}; | ||
const component = shallow(<CommentModel {...props} />); | ||
describe("Comment PopUp Model", () => { | ||
const instance = component.instance(); | ||
beforeEach(() => { | ||
jest.spyOn(instance, "componentWillReceiveProps"); | ||
jest.spyOn(component.instance(), "closeModel"); | ||
}); | ||
afterEach(() => { | ||
instance.componentWillReceiveProps.mockClear(); | ||
instance.closeModel.mockClear(); | ||
}); | ||
test("should match the snapshot", () => { | ||
expect(toJson(component)).toMatchSnapshot(); | ||
}); | ||
test("should render button and text area input", () => { | ||
expect(component.find(Button).length).toBe(1); | ||
expect(component.find(`[data-test="comment-model-input"]`).length).toBe(1); | ||
}); | ||
test("should not update the state on opening model", () => { | ||
component.setProps({ | ||
isOpen: true | ||
}); | ||
component.update(); | ||
expect(instance.componentWillReceiveProps).toBeCalled(); | ||
expect(instance.closeModel).not.toBeCalled(); | ||
}); | ||
test("should update state on text input change", () => { | ||
component.setProps({ | ||
isOpen: true | ||
}); | ||
const input = component.find(`[data-test="comment-model-input"]`).at(0); | ||
const event = { | ||
target: { | ||
value: `I don't know who I am, but it working in my favor!!` | ||
} | ||
}; | ||
input.simulate("change", event); | ||
expect(component.state().comment).toEqual(event.target.value); | ||
}); | ||
test("should close model and update the comment", () => { | ||
const closeBtn = component.find(`[data-test="close-btn"]`).at(0); | ||
closeBtn.simulate("click"); | ||
expect(onClose).toBeCalled(); | ||
component.setProps({ | ||
isOpen: false | ||
}); | ||
component.update(); | ||
expect(component.state().comment).toBe(""); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
src/__tests__/__components__/PopOvers/HighlightPopOver.test.js
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 React from "react"; | ||
import { shallow } from "enzyme"; | ||
import toJson from "enzyme-to-json"; | ||
import HighlighPopover from "../../../components/PopOvers/HighlighPopover"; | ||
|
||
const onClick = jest.fn(); | ||
const props = { | ||
onClick | ||
}; | ||
const component = shallow(<HighlighPopover {...props} />); | ||
describe("Highlight Popover", () => { | ||
test("should match the snapshot", () => { | ||
expect(toJson(component)).toMatchSnapshot(); | ||
}); | ||
test("should response on click", () => { | ||
component | ||
.find(`[data-test="button-set"]`) | ||
.at(0) | ||
.simulate("click"); | ||
expect(onClick).toBeCalled(); | ||
}); | ||
}); |
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,38 @@ | ||
import React from "react"; | ||
import { shallow } from "enzyme"; | ||
import { props1 } from "../__mocks__/testData"; | ||
import { Article } from "../../views/ReadArticle"; | ||
import HighlighPopover from "../../components/PopOvers/HighlighPopover"; | ||
|
||
const component = shallow(<Article {...props1} />); | ||
describe("Text highligh and comment", () => { | ||
let instance; | ||
beforeEach(() => { | ||
instance = component.instance(); | ||
window.getSelection = jest.fn().mockReturnValue({ | ||
getRangeAt: () => ({ | ||
getBoundingClientRect: () => ({ | ||
top: 244, | ||
left: 178 | ||
}) | ||
}) | ||
}); | ||
|
||
instance.setArticleBodyRef({ contains: jest.fn() }); | ||
}); | ||
|
||
test("should render highlight popover", () => { | ||
expect(component.find(HighlighPopover).length).toEqual(1); | ||
}); | ||
|
||
test("should trigger highlight handler on mouse move", () => { | ||
const { top, left, highlightedText } = component.state(); | ||
instance.articleBodyRef.contains.mockReturnValue(true); | ||
const articleBody = component.find(`[test-data="article-body"]`); | ||
articleBody.simulate("mouseup", { target: "hello world" }); | ||
expect(window.getSelection).toBeCalled(); | ||
expect(component.state().top).not.toBe(top); | ||
expect(component.state().left).not.toBe(left); | ||
expect(component.state().highlightedText).not.toBe(highlightedText); | ||
}); | ||
}); |
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
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