This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathBrowserView.test.tsx
128 lines (122 loc) · 4.82 KB
/
BrowserView.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { WebviewTag } from "electron"
import { shallow } from "enzyme"
import { shallowToJson } from "enzyme-to-json"
import * as React from "react"
import { Event } from "oni-types"
import {
BrowserView,
IBrowserViewProps,
IBrowserViewState,
} from "../browser/src/Services/Browser/BrowserView"
import { Configuration } from "../browser/src/Services/Configuration"
const mockEvent = new Event<void>()
const dispatch = () => mockEvent.dispatch()
const MockWebviewElement = (spy: (args?: any) => void) =>
({
sendInputEvent(args) {
spy(args)
},
} as WebviewTag)
// Using the disable life cycle methods here as the CDM calls sneak which is
// an external dependency I'm not trying to test here
describe("<BrowserView /> Tests", () => {
const component = (
<BrowserView
initialUrl={"test.com"}
configuration={{} as Configuration}
debug={mockEvent}
goBack={mockEvent}
goForward={mockEvent}
reload={mockEvent}
scrollUp={mockEvent}
scrollLeft={mockEvent}
scrollRight={mockEvent}
scrollDown={mockEvent}
/>
)
const wrapper = shallow<IBrowserViewProps, IBrowserViewState>(component, {
disableLifecycleMethods: true,
})
// can be typed here but the _webviewComponent is expected - TS Error
const instance: any = wrapper.instance()
it("component to render correctly", () => {
expect(wrapper).toBeDefined()
})
it('Should correctly prefix a url with "https" or "http" if needed', () => {
expect(instance.prefixUrl("apple.com")).toBe("http://apple.com")
})
it('Should NOT prefix a url with "https" or "http" if already present', () => {
// subtle difference here as the function always add https as a prefix not http
expect(instance.prefixUrl("https://www.apple.com")).toBe("https://www.apple.com")
})
it("Should match the recent snapshot - unless an intentional change has occurred ", () => {
expect(shallowToJson(wrapper)).toMatchSnapshot()
})
it("it should call the sendInputEvent method of the mocked webview element on Scroll down", () => {
const typedInstance: BrowserView = instance
const spy = jest.fn()
typedInstance._webviewElement = MockWebviewElement(spy)
typedInstance._scrollDown()
// the spy is passed to the mocked webview element so given that it was called the method accurately
// accessed the method on the webview
expect(spy).toHaveBeenCalled()
const args = {
type: "keyDown",
keyCode: "Down",
canScroll: true,
modifiers: ["isAutoRepeat"],
}
expect(spy.mock.calls[0][0]).toMatchObject(args)
})
it("it should call the sendInputEvent method of the mocked webview element on Scroll up", () => {
const typedInstance: BrowserView = instance
const spy = jest.fn()
typedInstance._webviewElement = MockWebviewElement(spy)
typedInstance._scrollUp()
// the spy is passed to the mocked webview element so given that it was called the method accurately
// accessed the method on the webview
expect(spy).toHaveBeenCalled()
const args = {
type: "keyDown",
keyCode: "Up",
canScroll: true,
modifiers: ["isAutoRepeat"],
}
expect(spy.mock.calls[0][0]).toMatchObject(args)
})
it("it should call the sendInputEvent method of the mocked webview element on Scroll left", () => {
const typedInstance: BrowserView = instance
const spy = jest.fn()
typedInstance._webviewElement = MockWebviewElement(spy)
typedInstance._scrollLeft()
expect(spy).toHaveBeenCalled()
const args = {
type: "keyDown",
keyCode: "Left",
canScroll: true,
modifiers: ["isAutoRepeat"],
}
expect(spy.mock.calls[0][0]).toMatchObject(args)
})
it("it should call the sendInputEvent method of the mocked webview element on Scroll right", () => {
const typedInstance: BrowserView = instance
const spy = jest.fn()
typedInstance._webviewElement = MockWebviewElement(spy)
typedInstance._scrollRight()
expect(spy).toHaveBeenCalled()
const args = {
type: "keyDown",
keyCode: "Right",
canScroll: true,
modifiers: ["isAutoRepeat"],
}
expect(spy.mock.calls[0][0]).toMatchObject(args)
})
it("it should NOT call the sendInputEvent method if no webview is present", () => {
const typedInstance: BrowserView = instance
const spy = jest.fn()
typedInstance._webviewElement = null
typedInstance._scrollDown()
expect(spy.mock.calls.length).toBe(0)
})
})