Files
pyscript/pyscriptjs/tests/unit/main.test.ts
Antonio Cuni 41ebaaf366 More plugins: splashscreen and importmap (#938)
This PR move codes from main.ts into two new plugins:

- splashscreen (formerly known as py-loader)
- importmap

The old setting config.autoclose_loader is still supported but deprecated; the new setting is config.splashscreen.autoclose.

Moreover, it does a small refactoring around UserError: now UserErrors are correctly caught even if they are raised from within afterRuntimeLoad.
2022-11-16 18:08:17 +01:00

70 lines
2.0 KiB
TypeScript

import { jest } from "@jest/globals"
import { UserError } from "../../src/exceptions"
import { PyScriptApp } from "../../src/main"
describe("Test withUserErrorHandler", () => {
class MyApp extends PyScriptApp {
myRealMain: any;
constructor(myRealMain) {
super();
this.myRealMain = myRealMain;
}
_realMain() {
this.myRealMain();
}
}
beforeEach(() => {
// Ensure we always have a clean body
document.body.innerHTML = `<div>Hello World</div>`;
});
it("userError doesn't stop execution", () => {
function myRealMain() {
throw new UserError("Computer says no");
}
const app = new MyApp(myRealMain);
app.main();
const banners = document.getElementsByClassName("alert-banner");
expect(banners.length).toBe(1);
expect(banners[0].innerHTML).toBe("Computer says no");
});
it("userError escapes by default", () => {
function myRealMain() {
throw new UserError("hello <br>");
}
const app = new MyApp(myRealMain);
app.main();
const banners = document.getElementsByClassName("alert-banner");
expect(banners.length).toBe(1);
expect(banners[0].innerHTML).toBe("hello &lt;br&gt;");
});
it("userError messageType=html don't escape", () => {
function myRealMain() {
throw new UserError("hello <br>", "html");
}
const app = new MyApp(myRealMain);
app.main();
const banners = document.getElementsByClassName("alert-banner");
expect(banners.length).toBe(1);
expect(banners[0].innerHTML).toBe("hello <br>");
});
it("any other exception should stop execution and raise", () => {
function myRealMain() {
throw new Error("Explosions!");
}
const app = new MyApp(myRealMain);
expect(() => app.main()).toThrow(new Error("Explosions!"))
});
});