Skip to content

Commit 3391e84

Browse files
committed
Add unit test for getURLs
1 parent 75b8fd4 commit 3391e84

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { MockPrimaryRegion, MockRegions, MockHealthyWildRegion } from "testHelpers/entities"
2+
import { getURLs } from "./ProxyContext"
3+
4+
describe("ProxyContextGetURLs", () => {
5+
it.each([
6+
["empty", [], undefined, "", ""],
7+
// Primary has no path app URL. Uses relative links
8+
["primary", [MockPrimaryRegion], MockPrimaryRegion, "", MockPrimaryRegion.wildcard_hostname],
9+
["regions selected", MockRegions, MockHealthyWildRegion, MockHealthyWildRegion.path_app_url, MockHealthyWildRegion.wildcard_hostname],
10+
// Primary is the default if none selected
11+
["no selected", [MockPrimaryRegion], undefined, "", MockPrimaryRegion.wildcard_hostname],
12+
["regions no select primary default", MockRegions, undefined, "", MockPrimaryRegion.wildcard_hostname],
13+
// This should never happen, when there is no primary
14+
["no primary", [MockHealthyWildRegion], undefined, "", ""],
15+
])(`%p`, (_, regions, selected, preferredPathAppURL, preferredWildcardHostname) => {
16+
const preferred = getURLs(regions, selected)
17+
expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL)
18+
expect(preferred.preferredWildcardHostname).toBe(preferredWildcardHostname)
19+
})
20+
})

site/src/contexts/ProxyContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ export const useProxy = (): ProxyContextValue => {
143143
/**
144144
* getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
145145
* assumed no proxy is configured and relative paths should be used.
146+
* Exported for testing.
146147
*
147148
* @param regions Is the list of regions returned by coderd. If this is empty, default behavior is used.
148149
* @param selectedRegion Is the region the user has selected. If this is undefined, default behavior is used.
149150
*/
150-
const getURLs = (
151+
export const getURLs = (
151152
regions: Region[],
152153
selectedRegion?: Region,
153154
): PreferredProxy => {

site/src/testHelpers/entities.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ export const MockPrimaryRegion: TypesGen.Region = {
7878
wildcard_hostname: "*.coder.com",
7979
}
8080

81+
export const MockHealthyWildRegion: TypesGen.Region = {
82+
id: "5e2c1ab7-479b-41a9-92ce-aa85625de52c",
83+
name: "haswildcard",
84+
display_name: "Subdomain Supported",
85+
icon_url: "/emojis/1f319.png",
86+
healthy: true,
87+
path_app_url: "https://external.com",
88+
wildcard_hostname: "*.external.com",
89+
}
90+
8191
export const MockRegions: TypesGen.Region[] = [
8292
MockPrimaryRegion,
93+
MockHealthyWildRegion,
8394
{
8495
id: "8444931c-0247-4171-842a-569d9f9cbadb",
8596
name: "unhealthy",

0 commit comments

Comments
 (0)