-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwidgets.test.tsx
More file actions
213 lines (179 loc) · 7.32 KB
/
Copy pathwidgets.test.tsx
File metadata and controls
213 lines (179 loc) · 7.32 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { FC } from "react"
import { render, waitFor } from "@testing-library/react-native"
import { act } from "react-test-renderer"
import { BudgetsWidget, MasterWidget } from "./MoneyMapWidgets"
import { PulseWidget, MiniPulseCarouselWidget } from "./PulseWidgets"
import { Props } from "./make_component"
import TestingErrorBoundary from "../../test/helpers/TestingErrorBoundary"
import { http, server } from "../../test/mocks/server"
import { Dimensions, triggerDeviceRotation } from "../../test/mocks/react_native"
jest.mock("expo-web-browser", () => {
return {
openAuthSessionAsync: jest.fn().mockResolvedValue({ type: "success" }),
}
})
describe("BudgetsWidget", () => fullWidgetComponentTestSuite(BudgetsWidget))
describe("MasterWidget", () => fullWidgetComponentTestSuite(MasterWidget))
describe("MiniPulseCarouselWidget", () => fullWidgetComponentTestSuite(MiniPulseCarouselWidget))
describe("PulseWidget", () => fullWidgetComponentTestSuite(PulseWidget))
export function fullWidgetComponentTestSuite(Component: FC<Props>) {
testSsoUrlLoading(Component)
testStyling(Component)
}
function testSsoUrlLoading(Component: FC<Props>) {
describe("SSO URL Loading", () => {
describe("Platform API", () => {
test("it is able to load the widget url when Platform API props are passed in", async () => {
const component = render(
<Component
clientId="myVeryOwnClientId"
apiKey="myVeryOwnApiKey"
userGuid="USR-777"
environment="production"
/>,
)
const webView = await waitFor(() => component.findByTestId("widget_webview"))
expect(webView.props.source.uri).toContain("https://widgets.moneydesktop.com/md/")
})
test("an error results in the onSsoUrlLoadError callback being triggered", async () => {
let called = false
server.use(
http.post("https://api.mx.com/users/:userGuid/widget_urls", () => {
return new Response(JSON.stringify({ message: "NO!" }), { status: 500 })
}),
)
render(
<Component
clientId="myVeryOwnClientId"
apiKey="myVeryOwnApiKey"
userGuid="USR-777"
environment="production"
onSsoUrlLoadError={(_error) => {
called = true
}}
/>,
)
await waitFor(() => {
if (!called) throw new Error()
})
expect(called).toBe(true)
})
test("it passes the request back to the host before execution via the ssoRequestPreprocess callback", async () => {
const component = render(
<Component
clientId="myVeryOwnClientId"
apiKey="myVeryOwnApiKey"
userGuid="USR-777"
environment="production"
ssoRequestPreprocess={(req) => {
const body = JSON.parse(req.options.body?.toString() || "")
body.widget_url.widget_type = "something_else"
req.options.body = JSON.stringify(body)
return req
}}
/>,
)
const webView = await waitFor(() => component.findByTestId("widget_webview"))
expect(webView.props.source.uri).toContain(
"https://widgets.moneydesktop.com/md/something_else/",
)
})
})
describe("Proxy server", () => {
test("it is able to load the widget url when proxy props are passed in", async () => {
const component = render(<Component proxy="https://client.com/mx-sso-proxy" />)
const webView = await waitFor(() => component.findByTestId("widget_webview"))
expect(webView.props.source.uri).toContain("https://widgets.moneydesktop.com/md/")
})
test("an error results in the onSsoUrlLoadError callback being triggered", async () => {
let called = false
server.use(
http.post("https://client.com/mx-sso-proxy", () => {
return new Response(JSON.stringify({ message: "NO!" }), { status: 500 })
}),
)
render(
<Component
proxy="https://client.com/mx-sso-proxy"
onSsoUrlLoadError={(_error) => {
called = true
}}
/>,
)
await waitFor(() => {
if (!called) throw new Error()
})
expect(called).toBe(true)
})
test("it passes the request back to the host before execution via the ssoRequestPreprocess callback", async () => {
const component = render(
<Component
proxy="https://client.com/mx-sso-proxy"
ssoRequestPreprocess={(req) => {
const body = JSON.parse(req.options.body?.toString() || "")
body.widget_url.widget_type = "something_else"
req.options.body = JSON.stringify(body)
return req
}}
/>,
)
const webView = await waitFor(() => component.findByTestId("widget_webview"))
expect(webView.props.source.uri).toContain(
"https://widgets.moneydesktop.com/md/something_else/",
)
})
})
describe("URL", () => {
test("it is able to get the widget url from props when a url prop is passed in", async () => {
const component = render(
<Component url="https://widgets.moneydesktop.com/md/hi/tototoken" />,
)
const webView = await waitFor(() => component.findByTestId("widget_webview"))
expect(webView.props.source.uri).toContain(
"https://widgets.moneydesktop.com/md/hi/tototoken",
)
})
})
test("it throws when no loading props are included", () => {
const spy = jest.spyOn(console, "error")
spy.mockImplementation(() => {
/* do nothing */
})
expect.assertions(1)
/* [1]: Widget components expect either URL loading props, API loading
* props, or proxy loading props, but we want to test how it behaves at
* runtime when it's missing that data, so we suppress compilation errors
* errors here in order to do that.
*/
render(
<TestingErrorBoundary onError={(error) => expect(error).toBeDefined()}>
{/* eslint @typescript-eslint/ban-ts-comment: "off" */}
{/* @ts-ignore: see [1] for details */}
<Component />
</TestingErrorBoundary>,
)
spy.mockClear()
})
})
}
function testStyling(Component: FC<Props>) {
describe("Styling", () => {
describe("default styles", () => {
test("height and width match the device's screen dimensions", async () => {
const { width, height } = Dimensions.get("screen")
const component = render(<Component url="https://widgets.moneydesktop.com/..." />)
const view = await waitFor(() => component.findByTestId("widget_view"))
expect(view.props.style.width).toBe(width)
expect(view.props.style.height).toBe(height)
})
test("screen orientation changes result in the view being resized", async () => {
const { width, height } = Dimensions.get("screen")
const component = render(<Component url="https://widgets.moneydesktop.com/..." />)
const view = await waitFor(() => component.findByTestId("widget_view"))
await act(() => triggerDeviceRotation())
expect(view.props.style.width).toBe(height)
expect(view.props.style.height).toBe(width)
})
})
})
}