Skip to content

fix: clear onerror #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ test("script onerror should not reset retry mechanism with parallel loaders", as
expect(console.log).toHaveBeenCalledTimes(loader.retries);
});

test("resetIfRetryingFailed should clear state", async () => {
const loader = new Loader({ apiKey: "foo", retries: 0 });

// eslint-disable-next-line @typescript-eslint/no-empty-function
console.log = jest.fn();

const rejection1 = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
jest.runAllTimers();

await Promise.all([rejection1]);
expect(loader["done"]).toBeTruthy();
expect(loader["loading"]).toBeFalsy();
expect(loader["errors"].length).toBe(1);

loader["resetIfRetryingFailed"]();
expect(loader["done"]).toBeFalsy();
expect(loader["loading"]).toBeFalsy();
expect(loader["onerrorEvent"]).toBe(null);
});

test("loader should not reset retry mechanism if successfully loaded", () => {
const loader = new Loader({ apiKey: "foo", retries: 0 });
const deleteScript = jest.spyOn(loader, "deleteScript");
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export class Loader {
this.done = false;
this.loading = false;
this.errors = [];
this.onerrorEvent = null;
}
}

Expand Down