|
| 1 | +import{ |
| 2 | +MockPrimaryWorkspaceProxy, |
| 3 | +MockWorkspaceProxies, |
| 4 | +MockHealthyWildWorkspaceProxy, |
| 5 | +MockUnhealthyWildWorkspaceProxy, |
| 6 | +}from"testHelpers/entities" |
| 7 | +import{ |
| 8 | +getPreferredProxy, |
| 9 | +ProxyProvider, |
| 10 | +saveUserSelectedProxy, |
| 11 | +useProxy, |
| 12 | +}from"./ProxyContext" |
| 13 | +import*asProxyContextModulefrom"./ProxyContext" |
| 14 | +import{ |
| 15 | +renderWithAuth, |
| 16 | +waitForLoaderToBeRemoved, |
| 17 | +}from"testHelpers/renderHelpers" |
| 18 | +import{screen}from"@testing-library/react" |
| 19 | +import{server}from"testHelpers/server" |
| 20 | +import"testHelpers/localstorage" |
| 21 | +import{rest}from"msw" |
| 22 | +import{Region}from"api/typesGenerated" |
| 23 | + |
| 24 | +describe("ProxyContextGetURLs",()=>{ |
| 25 | +it.each([ |
| 26 | +["empty",[],undefined,"",""], |
| 27 | +// Primary has no path app URL. Uses relative links |
| 28 | +[ |
| 29 | +"primary", |
| 30 | +[MockPrimaryWorkspaceProxy], |
| 31 | +MockPrimaryWorkspaceProxy, |
| 32 | +"", |
| 33 | +MockPrimaryWorkspaceProxy.wildcard_hostname, |
| 34 | +], |
| 35 | +[ |
| 36 | +"regions selected", |
| 37 | +MockWorkspaceProxies, |
| 38 | +MockHealthyWildWorkspaceProxy, |
| 39 | +MockHealthyWildWorkspaceProxy.path_app_url, |
| 40 | +MockHealthyWildWorkspaceProxy.wildcard_hostname, |
| 41 | +], |
| 42 | +// Primary is the default if none selected |
| 43 | +[ |
| 44 | +"no selected", |
| 45 | +[MockPrimaryWorkspaceProxy], |
| 46 | +undefined, |
| 47 | +"", |
| 48 | +MockPrimaryWorkspaceProxy.wildcard_hostname, |
| 49 | +], |
| 50 | +[ |
| 51 | +"regions no select primary default", |
| 52 | +MockWorkspaceProxies, |
| 53 | +undefined, |
| 54 | +"", |
| 55 | +MockPrimaryWorkspaceProxy.wildcard_hostname, |
| 56 | +], |
| 57 | +// Primary is the default if the selected is unhealthy |
| 58 | +[ |
| 59 | +"unhealthy selection", |
| 60 | +MockWorkspaceProxies, |
| 61 | +MockUnhealthyWildWorkspaceProxy, |
| 62 | +"", |
| 63 | +MockPrimaryWorkspaceProxy.wildcard_hostname, |
| 64 | +], |
| 65 | +// This should never happen, when there is no primary |
| 66 | +["no primary",[MockHealthyWildWorkspaceProxy],undefined,"",""], |
| 67 | +])( |
| 68 | +`%p`, |
| 69 | +(_,regions,selected,preferredPathAppURL,preferredWildcardHostname)=>{ |
| 70 | +constpreferred=getPreferredProxy(regions,selected) |
| 71 | +expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL) |
| 72 | +expect(preferred.preferredWildcardHostname).toBe( |
| 73 | +preferredWildcardHostname, |
| 74 | +) |
| 75 | +}, |
| 76 | +) |
| 77 | +}) |
| 78 | + |
| 79 | +// interface ProxySelectTest { |
| 80 | +// name: string |
| 81 | +// actions: () |
| 82 | +// } |
| 83 | + |
| 84 | +constTestingComponent=()=>{ |
| 85 | +returnrenderWithAuth( |
| 86 | +<ProxyProvider> |
| 87 | +<TestingScreen/> |
| 88 | +</ProxyProvider>, |
| 89 | +{ |
| 90 | +route:`/proxies`, |
| 91 | +path:"/proxies", |
| 92 | +}, |
| 93 | +) |
| 94 | +} |
| 95 | + |
| 96 | +// TestingScreen just mounts some components that we can check in the unit test. |
| 97 | +constTestingScreen=()=>{ |
| 98 | +const{ proxy, isFetched, isLoading}=useProxy() |
| 99 | +return( |
| 100 | +<> |
| 101 | +<divdata-testid="isFetched"title={isFetched.toString()}></div> |
| 102 | +<divdata-testid="isLoading"title={isLoading.toString()}></div> |
| 103 | +<div |
| 104 | +data-testid="preferredProxy" |
| 105 | +title={proxy.selectedProxy&&proxy.selectedProxy.id} |
| 106 | +></div> |
| 107 | +</> |
| 108 | +) |
| 109 | +} |
| 110 | + |
| 111 | +interfaceProxyContextSelectionTest{ |
| 112 | +expSelectedProxyID:string |
| 113 | +regions:Region[] |
| 114 | +storageProxy:Region|undefined |
| 115 | +} |
| 116 | + |
| 117 | +describe("ProxyContextSelection",()=>{ |
| 118 | +beforeEach(()=>{ |
| 119 | +window.localStorage.clear() |
| 120 | +}) |
| 121 | + |
| 122 | +it.each([ |
| 123 | +[ |
| 124 | +"empty", |
| 125 | +{ |
| 126 | +expSelectedProxyID:"", |
| 127 | +regions:[], |
| 128 | +storageProxy:undefined, |
| 129 | +}, |
| 130 | +], |
| 131 | +[ |
| 132 | +"regions_no_selection", |
| 133 | +{ |
| 134 | +expSelectedProxyID:MockPrimaryWorkspaceProxy.id, |
| 135 | +regions:MockWorkspaceProxies, |
| 136 | +storageProxy:undefined, |
| 137 | +}, |
| 138 | +], |
| 139 | +[ |
| 140 | +"regions_selected_unhealthy", |
| 141 | +{ |
| 142 | +expSelectedProxyID:MockPrimaryWorkspaceProxy.id, |
| 143 | +regions:MockWorkspaceProxies, |
| 144 | +storageProxy:MockUnhealthyWildWorkspaceProxy, |
| 145 | +}, |
| 146 | +], |
| 147 | +]as[string,ProxyContextSelectionTest][])( |
| 148 | +`%s`, |
| 149 | +async(_,{ expSelectedProxyID, regions, storageProxy})=>{ |
| 150 | +// Initial selection if present |
| 151 | +if(storageProxy){ |
| 152 | +saveUserSelectedProxy(storageProxy) |
| 153 | +} |
| 154 | + |
| 155 | +// Mock the API response |
| 156 | +server.use( |
| 157 | +rest.get("/api/v2/regions",async(req,res,ctx)=>{ |
| 158 | +returnres( |
| 159 | +ctx.status(200), |
| 160 | +ctx.json({ |
| 161 | +regions:regions, |
| 162 | +}), |
| 163 | +) |
| 164 | +}), |
| 165 | +) |
| 166 | + |
| 167 | +TestingComponent() |
| 168 | +awaitwaitForLoaderToBeRemoved() |
| 169 | + |
| 170 | +awaitscreen.findByTestId("isFetched").then((x)=>{ |
| 171 | +expect(x.title).toBe("true") |
| 172 | +}) |
| 173 | +awaitscreen.findByTestId("isLoading").then((x)=>{ |
| 174 | +expect(x.title).toBe("false") |
| 175 | +}) |
| 176 | +awaitscreen.findByTestId("preferredProxy").then((x)=>{ |
| 177 | +expect(x.title).toBe(expSelectedProxyID) |
| 178 | +}) |
| 179 | + |
| 180 | +// const { proxy, proxies, isFetched, isLoading, proxyLatencies } = useProxy() |
| 181 | +// expect(isLoading).toBe(false) |
| 182 | +// expect(isFetched).toBe(true) |
| 183 | + |
| 184 | +// expect(x).toBe(2) |
| 185 | +// const preferred = getPreferredProxy(regions, selected) |
| 186 | +// expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL) |
| 187 | +// expect(preferred.preferredWildcardHostname).toBe( |
| 188 | +// preferredWildcardHostname, |
| 189 | +// ) |
| 190 | +}, |
| 191 | +) |
| 192 | +}) |