Skip to content

Commit c861a7b

Browse files
fix tests
1 parent 5891213 commit c861a7b

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

tests/getData.spec.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ test.describe("oc-client : getData", () => {
200200
});
201201

202202
// Verify error was passed to callback
203-
expect(errorResult.callbackError).toEqual("details about oups");
203+
// If callbackError is an Error object, compare its message
204+
const errorMsg =
205+
errorResult.callbackError instanceof Error
206+
? errorResult.callbackError.message
207+
: errorResult.callbackError;
208+
expect(errorMsg).toEqual("details about oups");
204209
});
205210

206211
test("should call the callback with server.js error if no details are available", async ({
@@ -250,7 +255,11 @@ test.describe("oc-client : getData", () => {
250255
});
251256

252257
// Verify error was passed to callback
253-
expect(errorResult.callbackError).toEqual("oups");
258+
const errorMsg =
259+
errorResult.callbackError instanceof Error
260+
? errorResult.callbackError.message
261+
: errorResult.callbackError;
262+
expect(errorMsg).toEqual("oups");
254263
});
255264

256265
test("should handle JSON requests correctly", async ({ page }) => {
@@ -555,4 +564,52 @@ test.describe("oc-client : getData", () => {
555564
// Verify error was passed to callback
556565
expect(errorResult.callbackError).toEqual("Network error");
557566
});
567+
568+
test("should reject with an Error object containing API error data when network returns 500", async ({
569+
page,
570+
}) => {
571+
const result = await page.evaluate(async () => {
572+
// Save original fetch
573+
const originalFetch = window.fetch;
574+
// Mock fetch to simulate a 500 error with error object in response
575+
window.fetch = () => {
576+
return Promise.resolve({
577+
headers: { get: () => null },
578+
json: () =>
579+
Promise.resolve({
580+
error: "API error",
581+
details: {
582+
foo: "bar",
583+
message: "Something went wrong",
584+
stack: "stacktrace",
585+
},
586+
}),
587+
});
588+
};
589+
try {
590+
await window.oc.getAction({
591+
component: "test-component",
592+
baseUrl: "http://api",
593+
version: "1.0.0",
594+
action: "do",
595+
});
596+
return { success: true };
597+
} catch (err) {
598+
// Restore original fetch
599+
window.fetch = originalFetch;
600+
return {
601+
success: false,
602+
isError: err instanceof Error,
603+
message: err.message,
604+
stack: err.stack,
605+
foo: err.foo,
606+
};
607+
}
608+
});
609+
expect(result.success).toBe(false);
610+
expect(result.isError).toBe(true);
611+
expect(result.message).toBe("Something went wrong");
612+
expect(result.stack).toBe("stacktrace");
613+
expect(result.foo).toBe("bar");
614+
});
558615
});

0 commit comments

Comments
 (0)