-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathObserverConfigModal.test.tsx
More file actions
229 lines (199 loc) · 8.04 KB
/
Copy pathObserverConfigModal.test.tsx
File metadata and controls
229 lines (199 loc) · 8.04 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// @vitest-environment jsdom
/* eslint-disable react/react-in-jsx-scope */
import { act, type ComponentProps, type ReactNode } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, describe, expect, it, vi, type Mock } from 'vitest'
import type { RpcStub } from 'capnweb'
import type {
AuthenticatedApi,
ConnectedAccountsSubscriber,
ObserverAccountChoice,
ObserverBindingNeed,
} from '@gadgets/workshop-shared/api'
import type { AccountDescription, SupportedResource, VendorDescription } from '@gadgets/workshop-shared/gatekeeper'
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
vi.mock('@cloudflare/kumo', () => {
const Dialog = Object.assign(
({ children }: { children: ReactNode }) => <div>{children}</div>,
{
Root: ({ children }: { children: ReactNode }) => <>{children}</>,
Title: ({ children }: { children: ReactNode }) => <h1>{children}</h1>,
},
)
const Select = Object.assign(
({ children }: { children: ReactNode }) => <div data-testid="account-select">{children}</div>,
{ Option: ({ children }: { children: ReactNode }) => <div>{children}</div> },
)
return {
Dialog,
Loader: () => <span>Loading</span>,
Select,
Text: ({ children }: { children: ReactNode }) => <p>{children}</p>,
useKumoToastManager: () => ({ add: vi.fn<(toast: unknown) => void>() }),
}
})
vi.mock('./components/WorkshopControls', () => ({
WorkshopButton: ({ children, ...props }: ComponentProps<'button'>) => (
<button type="button" {...props}>{children}</button>
),
}))
vi.mock('./components/Avatar', () => ({ default: () => <span data-testid="avatar" /> }))
import ObserverConfigModal from './ObserverConfigModal'
const VENDOR = {
displayName: 'Google',
color: '#4285f4',
} as VendorDescription
const DOC_RESOURCE: SupportedResource = {
urlPattern: 'https://docs.google.com/document/d/:docId/*',
title: 'Google Doc',
description: 'Read and edit documents you choose.',
grantable: true,
}
const GMAIL_RESOURCE_PATTERN = 'https://mail.google.com/*'
const NEED: ObserverBindingNeed = {
gatekeeperId: 12,
vendorId: 'google',
resourceTitle: 'Q3 planning',
resourceUrl: 'https://docs.google.com/document/d/quarterly',
}
function account(id: number, uniqueName: string, grantedResourceUrlPatterns?: string[]) {
return {
id,
description: {
displayName: uniqueName,
uniqueName,
grantedResourceUrlPatterns,
} as AccountDescription,
}
}
type ApiOverrides = {
connectAccount?: Mock<(vendorId: string, resourceUrlPatterns?: string[]) => Promise<{ url: string }>>
ensureAccountResources?: Mock<(
accountId: number,
resourceUrlPatterns: string[],
) => Promise<{ url?: string }>>
reconnectAccount?: Mock<(accountId: number) => Promise<{ url: string }>>
}
function fakeApi(
accountEntries: ReturnType<typeof account>[],
overrides: ApiOverrides = {},
): RpcStub<AuthenticatedApi> {
return {
subscribeConnectedAccounts: async (subscriber: ConnectedAccountsSubscriber) => {
for (const entry of accountEntries) {
subscriber.add(entry.id, entry.description, VENDOR, [DOC_RESOURCE], true, 'google')
}
subscriber.ready()
return { [Symbol.dispose]() {} }
},
listGatekeeperVendors: async () => [{
id: 'google',
description: VENDOR,
supportedResources: [DOC_RESOURCE],
}],
listAddableGatekeepers: async () => [],
connectAccount: overrides.connectAccount ??
vi.fn<(vendorId: string, resourceUrlPatterns?: string[]) => Promise<{ url: string }>>(),
ensureAccountResources: overrides.ensureAccountResources ??
vi.fn<(accountId: number, resourceUrlPatterns: string[]) => Promise<{ url?: string }>>(),
reconnectAccount: overrides.reconnectAccount ??
vi.fn<(accountId: number) => Promise<{ url: string }>>(),
} as unknown as RpcStub<AuthenticatedApi>
}
describe('ObserverConfigModal account selection', () => {
let root: Root | undefined
let container: HTMLDivElement | undefined
afterEach(() => {
act(() => root?.unmount())
container?.remove()
vi.restoreAllMocks()
root = undefined
container = undefined
})
async function render(
accountEntries: ReturnType<typeof account>[],
options: {
api?: RpcStub<AuthenticatedApi>
onConfirm?: (choices: ObserverAccountChoice[]) => void
} = {},
) {
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
await act(async () => {
root!.render(
<ObserverConfigModal
needs={[NEED]}
authenticatedApi={options.api ?? fakeApi(accountEntries)}
onConfirm={options.onConfirm ?? (() => {})}
onCancel={() => {}}
/>,
)
await Promise.resolve()
})
return container
}
it('shows a single matching account directly instead of putting it in a dropdown', async () => {
const rendered = await render([account(1, 'dan@cloudflare.com')])
expect(rendered.textContent).toContain('dan@cloudflare.com')
expect(rendered.querySelector('[data-testid="account-select"]')).toBeNull()
})
it('keeps the account dropdown when multiple accounts match', async () => {
const rendered = await render([
account(1, 'dan@cloudflare.com'),
account(2, 'dan.personal@gmail.com'),
])
expect(rendered.querySelectorAll('[data-testid="account-select"]')).toHaveLength(1)
})
it('requests the resource scope when connecting a new account', async () => {
const connectAccount = vi.fn<
(vendorId: string, resourceUrlPatterns?: string[]) => Promise<{ url: string }>
>().mockResolvedValue({ url: 'https://accounts.google.test/oauth' })
vi.spyOn(window, 'open').mockImplementation(() => null)
const rendered = await render([], {
api: fakeApi([], { connectAccount }),
})
const connect = [...rendered.querySelectorAll('button')]
.find(button => button.textContent === 'Connect')
expect(connect).toBeDefined()
await act(async () => connect!.click())
expect(connectAccount).toHaveBeenCalledWith('google', [DOC_RESOURCE.urlPattern])
expect(window.open).toHaveBeenCalledWith(
'https://accounts.google.test/oauth', '_blank', 'noopener,noreferrer',
)
})
it('expands an existing account grant before allowing verification', async () => {
const ensureAccountResources = vi.fn<
(accountId: number, resourceUrlPatterns: string[]) => Promise<{ url?: string }>
>()
.mockResolvedValue({ url: 'https://accounts.google.test/oauth' })
vi.spyOn(window, 'open').mockImplementation(() => null)
const underScoped = account(1, 'dan@cloudflare.com', [GMAIL_RESOURCE_PATTERN])
const rendered = await render([underScoped], {
api: fakeApi([underScoped], { ensureAccountResources }),
})
const verify = [...rendered.querySelectorAll('button')]
.find(button => button.textContent === 'Verify and open')
const grant = [...rendered.querySelectorAll('button')]
.find(button => button.textContent === 'Grant the access needed to verify this resource')
expect(verify?.disabled).toBe(true)
expect(grant).toBeDefined()
expect(rendered.textContent).not.toContain('Ready')
await act(async () => grant!.click())
expect(ensureAccountResources).toHaveBeenCalledWith(1, [DOC_RESOURCE.urlPattern])
expect(window.open).toHaveBeenCalledWith(
'https://accounts.google.test/oauth', '_blank', 'noopener,noreferrer',
)
})
it('allows verification when the account already has the required grant', async () => {
const onConfirm = vi.fn<(choices: ObserverAccountChoice[]) => void>()
const granted = account(1, 'dan@cloudflare.com', [DOC_RESOURCE.urlPattern])
const rendered = await render([granted], { onConfirm })
const verify = [...rendered.querySelectorAll('button')]
.find(button => button.textContent === 'Verify and open')
expect(verify?.disabled).toBe(false)
expect(rendered.textContent).toContain('Ready')
await act(async () => verify!.click())
expect(onConfirm).toHaveBeenCalledWith([{ gatekeeperId: 12, accountId: 1 }])
})
})