Skip to content

Commit

Permalink
Resolve sendOnClose bug to use fetch(), mocked fetch in tests, fixed …
Browse files Browse the repository at this point in the history
…issues
  • Loading branch information
rthenhaus committed Jun 4, 2024
1 parent f98917c commit 8fcbf85
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 28 deletions.
19 changes: 17 additions & 2 deletions build/UserALEWebExtension/background.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/background.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/UserALEWebExtension/content.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/content.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/UserALEWebExtension/options.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/UserALEWebExtension/options.js.map

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions build/userale-2.4.0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/userale-2.4.0.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/userale-2.4.0.min.js

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions src/sendLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,27 @@ export function sendOnClose(
config: Configuration,
): void {
window.addEventListener("pagehide", function () {
if (config.on && logs.length > 0) {
navigator.sendBeacon(config.url, JSON.stringify(logs));
if (!config.on) {
return;
}

if (logs.length > 0) {
const headers: HeadersInit = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");

if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
}

fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});

logs.splice(0); // clear log queue
}
});
Expand Down
30 changes: 17 additions & 13 deletions test/sendLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,38 @@ describe("sendLogs", () => {
done();
});

it("sends logs on page exit with navigator", () => {
const sendBeaconSpy = jest.fn();
Object.defineProperty(global, "navigator", {
value: { sendBeacon: sendBeaconSpy },
writable: true,
});
it("sends logs on page exit with fetch", () => {
const fetchSpy = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
foo: "bar",
}),
);
global.fetch = fetchSpy;

config.update({ on: true, url: "test" });
sendOnClose([], config);
config.update({ on: true, url: "test" });
sendOnClose([{ foo: "bar" }], config);
global.window.dispatchEvent(new window.CustomEvent("pagehide"));

expect(sendBeaconSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy).toHaveBeenCalledTimes(1);
});

it("does not send logs on page exit when config is off", () => {
const sendBeaconSpy = jest.fn();
Object.defineProperty(global, "navigator", {
value: { sendBeacon: sendBeaconSpy },
writable: true,
});
const fetchSpy = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
foo: "bar",
}),
);
global.fetch = fetchSpy;

config.update({ on: false, url: "test" });
sendOnClose([{ foo: "bar" }], config);
global.window.dispatchEvent(new window.CustomEvent("pagehide"));

expect(sendBeaconSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});

it("sends logs with proper auth header when using registerAuthCallback", (done) => {
Expand Down

0 comments on commit 8fcbf85

Please sign in to comment.