Skip to content

Commit c157717

Browse files
committed
Test for URL object property matches
1 parent f38c34d commit c157717

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

test/seam/connect/seam-http-request.test.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,82 @@ import { SeamHttp } from '@seamapi/http/connect'
55

66
import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js'
77

8-
test('returns a SeamHttpRequest', async (t) => {
8+
test('SeamHttp: returns a SeamHttpRequest', async (t) => {
99
const { seed, endpoint } = await getTestServer(t)
1010
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
1111

12-
const deviceRequest = seam.devices.get({ device_id: seed.august_device_1 })
12+
const request = seam.devices.get({ device_id: seed.august_device_1 })
1313

14-
t.true(deviceRequest instanceof SeamHttpRequest)
15-
t.is(deviceRequest.url.pathname, '/devices/get')
16-
t.deepEqual(deviceRequest.body, {
14+
t.true(request instanceof SeamHttpRequest)
15+
t.truthy(request.url)
16+
t.is(request.responseKey, 'device')
17+
t.deepEqual(request.body, {
1718
device_id: seed.august_device_1,
1819
})
19-
t.is(deviceRequest.responseKey, 'device')
20-
const device = await deviceRequest
20+
21+
const device = await request
2122
t.is(device.workspace_id, seed.seed_workspace_1)
2223
t.is(device.device_id, seed.august_device_1)
2324

2425
// Ensure that the type of the response is correct.
25-
type Expected = ResponseFromSeamHttpRequest<typeof deviceRequest>
26-
26+
type Expected = ResponseFromSeamHttpRequest<typeof request>
2727
const validDeviceType: Expected['device_type'] = 'august_lock'
2828
t.truthy(validDeviceType)
2929

30-
// @ts-expect-error because it's an invalid device type.
30+
// @ts-expect-error invalid device type.
3131
const invalidDeviceType: Expected['device_type'] = 'invalid_device_type'
3232
t.truthy(invalidDeviceType)
3333
})
3434

35-
test("populates SeamHttpRequest's url property", async (t) => {
35+
test('SeamHttpRequest: url is a URL for post requests', async (t) => {
3636
const { seed, endpoint } = await getTestServer(t)
3737
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
3838

39-
const deviceRequest = seam.devices.get({ device_id: 'abc123' })
39+
const { url } = seam.devices.get({ device_id: 'abc123' })
40+
41+
t.true(url instanceof URL)
42+
t.deepEqual(
43+
toPlainUrlObject(url),
44+
toPlainUrlObject(new URL(`${endpoint}/devices/get`)),
45+
)
46+
})
4047

41-
t.is(deviceRequest.url.pathname, '/devices/get')
42-
t.is(deviceRequest.url.search, '')
48+
test('SeamHttpRequest: url is a URL for get requests', async (t) => {
49+
const { seed, endpoint } = await getTestServer(t)
50+
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
4351

44-
const connectWebviewsViewRequest = seam.connectWebviews.view({
45-
connect_webview_id: 'abc123',
46-
auth_token: 'invalid',
52+
const { url } = seam.connectWebviews.view({
53+
connect_webview_id: 'connect_webview_1',
54+
auth_token: 'auth_token_1',
4755
})
4856

49-
t.is(connectWebviewsViewRequest.url.pathname, '/connect_webviews/view')
50-
t.is(connectWebviewsViewRequest.url.searchParams.get('auth_token'), 'invalid')
51-
t.is(
52-
connectWebviewsViewRequest.url.searchParams.get('connect_webview_id'),
53-
'abc123',
57+
t.true(url instanceof URL)
58+
t.deepEqual(
59+
toPlainUrlObject(url),
60+
toPlainUrlObject(
61+
new URL(
62+
`${endpoint}/connect_webviews/view?auth_token=auth_token_1&connect_webview_id=connect_webview_1`,
63+
),
64+
),
5465
)
5566
})
5667

68+
const toPlainUrlObject = (url: URL): Omit<URL, 'searchParams' | 'toJSON'> => {
69+
return {
70+
pathname: url.pathname,
71+
hash: url.hash,
72+
hostname: url.hostname,
73+
protocol: url.protocol,
74+
username: url.username,
75+
port: url.port,
76+
password: url.password,
77+
host: url.host,
78+
href: url.href,
79+
origin: url.origin,
80+
search: url.search,
81+
}
82+
}
83+
5784
type ResponseFromSeamHttpRequest<T> =
5885
T extends SeamHttpRequest<any, infer TResponse, infer TResponseKey>
5986
? TResponseKey extends keyof TResponse

0 commit comments

Comments
 (0)