|
1 | 1 | import { mountApp, mockEndpoint, flushPromises } from "@test/util"; |
2 | 2 | import CLS from "./c-loader-status.vue"; |
3 | 3 | import { ComplexModelViewModel } from "@test-targets/viewmodels.g"; |
| 4 | +import { AxiosError } from "axios"; |
4 | 5 |
|
5 | 6 | describe("CLoaderStatus", () => { |
6 | 7 | const vm = new ComplexModelViewModel(); |
@@ -145,61 +146,152 @@ describe("CLoaderStatus", () => { |
145 | 146 | expect(wrapper.text()).toContain("Success"); |
146 | 147 | }); |
147 | 148 |
|
148 | | - test("void-returning endpoint with no-initial-content shows content after success then failure", async () => { |
149 | | - // This tests the fix for void-returning endpoints where hasResult should be true |
150 | | - // when the endpoint succeeds, even though result is null. |
151 | | - const vm = new ComplexModelViewModel(); |
152 | | - vm.$primaryKey = 1; |
153 | | - const voidCaller = vm.methodWithOptionalEnumParam; |
154 | | - |
155 | | - const mockSuccess = mockEndpoint( |
156 | | - vm.$metadata.methods.methodWithOptionalEnumParam, |
157 | | - () => ({ |
| 149 | + const endpoints = [ |
| 150 | + { |
| 151 | + name: "void-returning", |
| 152 | + methodName: "methodWithOptionalEnumParam" as const, |
| 153 | + mockSuccessResponse: () => ({ |
158 | 154 | wasSuccessful: true, |
159 | 155 | // Void endpoints don't have an 'object' property in the response |
160 | 156 | }), |
161 | | - ); |
162 | | - |
163 | | - const wrapper = mountApp(() => ( |
164 | | - <CLS loaders={voidCaller} no-initial-content> |
165 | | - <div>Content</div> |
166 | | - </CLS> |
167 | | - )); |
168 | | - |
169 | | - // Initially, content should be hidden (wasSuccessful is null) |
170 | | - expect(wrapper.text()).not.toContain("Content"); |
171 | | - |
172 | | - // Call the void endpoint successfully |
173 | | - await voidCaller(); |
174 | | - await flushPromises(); |
175 | | - |
176 | | - // After success, content should be visible |
177 | | - expect(voidCaller.wasSuccessful).toBe(true); |
178 | | - expect(voidCaller.result).toBeNull(); |
179 | | - expect(voidCaller._hasLoaded.value).toBe(true); |
180 | | - expect(wrapper.text()).toContain("Content"); |
181 | | - |
182 | | - mockSuccess.destroy(); |
183 | | - |
184 | | - // Now mock a failure |
185 | | - const mockFailure = mockEndpoint( |
186 | | - vm.$metadata.methods.methodWithOptionalEnumParam, |
187 | | - () => ({ |
188 | | - wasSuccessful: false, |
189 | | - message: "Error occurred", |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "string-returning", |
| 160 | + methodName: "methodWithOptionalParams" as const, |
| 161 | + mockSuccessResponse: () => ({ |
| 162 | + wasSuccessful: true, |
| 163 | + object: "test string result", |
190 | 164 | }), |
191 | | - ); |
192 | | - |
193 | | - // Call the endpoint again, this time it fails |
194 | | - await expect(voidCaller()).rejects.toThrow(); |
195 | | - await flushPromises(); |
196 | | - |
197 | | - // Content should still be visible because hasLoaded is true |
198 | | - // (the endpoint has successfully loaded before, even though this call failed) |
199 | | - expect(voidCaller.wasSuccessful).toBe(false); |
200 | | - expect(voidCaller._hasLoaded.value).toBe(true); |
201 | | - expect(wrapper.text()).toContain("Content"); |
202 | | - |
203 | | - mockFailure.destroy(); |
204 | | - }); |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "object-returning", |
| 168 | + methodName: "methodWithManyParams" as const, |
| 169 | + mockSuccessResponse: () => ({ |
| 170 | + wasSuccessful: true, |
| 171 | + object: { externalParentId: 42, name: "Test Parent" }, |
| 172 | + }), |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "ListResult-returning", |
| 176 | + methodName: "returnsListResult" as const, |
| 177 | + mockSuccessResponse: () => ({ |
| 178 | + wasSuccessful: true, |
| 179 | + list: [], |
| 180 | + }), |
| 181 | + }, |
| 182 | + ] as const; |
| 183 | + |
| 184 | + describe.each(endpoints)( |
| 185 | + "$name endpoint", |
| 186 | + function ({ methodName, mockSuccessResponse }) { |
| 187 | + test("no-initial-content shows content after success then Network Error", async () => { |
| 188 | + // This tests that hasResult is properly set when an endpoint succeeds, |
| 189 | + // regardless of the return type (void, primitive, object, ItemResult). |
| 190 | + const vm = new ComplexModelViewModel(); |
| 191 | + vm.$primaryKey = 1; |
| 192 | + const caller = vm[methodName]; |
| 193 | + |
| 194 | + const mockSuccess = mockEndpoint( |
| 195 | + vm.$metadata.methods[methodName], |
| 196 | + mockSuccessResponse, |
| 197 | + ); |
| 198 | + |
| 199 | + const wrapper = mountApp(() => ( |
| 200 | + <CLS loaders={caller} no-initial-content> |
| 201 | + <div>Content</div> |
| 202 | + </CLS> |
| 203 | + )); |
| 204 | + |
| 205 | + // Initially, content should be hidden (wasSuccessful is null) |
| 206 | + expect(wrapper.text()).not.toContain("Content"); |
| 207 | + |
| 208 | + // Call the endpoint successfully |
| 209 | + await caller(); |
| 210 | + await flushPromises(); |
| 211 | + |
| 212 | + // After success, content should be visible |
| 213 | + expect(caller.wasSuccessful).toBe(true); |
| 214 | + expect(caller.hasResult).toBe(true); |
| 215 | + expect(wrapper.text()).toContain("Content"); |
| 216 | + |
| 217 | + mockSuccess.destroy(); |
| 218 | + |
| 219 | + // Now mock a failure |
| 220 | + const mockFailure = mockEndpoint( |
| 221 | + vm.$metadata.methods[methodName], |
| 222 | + () => { |
| 223 | + throw new AxiosError("Network Error"); |
| 224 | + }, |
| 225 | + ); |
| 226 | + |
| 227 | + // Call the endpoint again, this time it fails |
| 228 | + await expect(caller()).rejects.toThrow(); |
| 229 | + await flushPromises(); |
| 230 | + |
| 231 | + // Content should still be visible because Network errors do not wipe a caller's result. |
| 232 | + // This is tested for because its how Coalesce has always worked, |
| 233 | + // and would cause all kinds of subtile behavior changes if it ever stopped doing that. |
| 234 | + expect(caller.wasSuccessful).toBe(false); |
| 235 | + expect(caller.hasResult).toBe(true); |
| 236 | + expect(wrapper.text()).toContain("Content"); |
| 237 | + |
| 238 | + mockFailure.destroy(); |
| 239 | + }); |
| 240 | + |
| 241 | + test("no-initial-content hides content after explicit failure", async () => { |
| 242 | + // This tests that hasResult is properly set when an endpoint succeeds, |
| 243 | + // regardless of the return type (void, primitive, object, ItemResult). |
| 244 | + const vm = new ComplexModelViewModel(); |
| 245 | + vm.$primaryKey = 1; |
| 246 | + const caller = vm[methodName]; |
| 247 | + |
| 248 | + const mockSuccess = mockEndpoint( |
| 249 | + vm.$metadata.methods[methodName], |
| 250 | + mockSuccessResponse, |
| 251 | + ); |
| 252 | + |
| 253 | + const wrapper = mountApp(() => ( |
| 254 | + <CLS loaders={caller} no-initial-content> |
| 255 | + <div>Content</div> |
| 256 | + </CLS> |
| 257 | + )); |
| 258 | + |
| 259 | + // Initially, content should be hidden (wasSuccessful is null) |
| 260 | + expect(wrapper.text()).not.toContain("Content"); |
| 261 | + |
| 262 | + // Call the endpoint successfully |
| 263 | + await caller(); |
| 264 | + await flushPromises(); |
| 265 | + |
| 266 | + // After success, content should be visible |
| 267 | + expect(caller.wasSuccessful).toBe(true); |
| 268 | + expect(caller.hasResult).toBe(true); |
| 269 | + expect(wrapper.text()).toContain("Content"); |
| 270 | + |
| 271 | + mockSuccess.destroy(); |
| 272 | + |
| 273 | + // Now mock a failure |
| 274 | + const mockFailure = mockEndpoint( |
| 275 | + vm.$metadata.methods[methodName], |
| 276 | + () => ({ |
| 277 | + wasSuccessful: false, |
| 278 | + message: "Explicit failure", |
| 279 | + }), |
| 280 | + ); |
| 281 | + |
| 282 | + // Call the endpoint again, this time it fails |
| 283 | + await expect(caller()).rejects.toThrow(); |
| 284 | + await flushPromises(); |
| 285 | + |
| 286 | + // Since no-initial-status is based on `hasResult`, |
| 287 | + // and our result has been wiped by an explicit server error, the content should hide. |
| 288 | + expect(caller.wasSuccessful).toBe(false); |
| 289 | + expect(caller.hasResult).toBe(false); |
| 290 | + expect(caller.result).toBeFalsy(); |
| 291 | + expect(wrapper.text()).not.toContain("Content"); |
| 292 | + |
| 293 | + mockFailure.destroy(); |
| 294 | + }); |
| 295 | + }, |
| 296 | + ); |
205 | 297 | }); |
0 commit comments