mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 12:12:59 -05:00
As the title stays, the main goal of the branch is to kill the infamous runtimeLoaded global store and all the complications, problems and bugs caused by the fact that in many places we needed to ensure/wait that the global runtime was properly set before being able to execute code. The core idea is that runtime is never a global object and that it's passed around explicitly, which means that when a function receives it, it is guaranteed to be initialized&ready. This caused a bit of complications in pybutton.ts, pyinputbox.ts and pyrepl.ts, because they indirectly want to call runtime.run from connectedCallback, which is the only place where we cannot explicitly pass the runtime because it's automatically called by the browser. But also, it is also a sign of a bad design, because it were entirely possible that connectedCallback was called before the runtime was ready, which probably caused many bugs, see e.g. #673 and #747. The solution to is use dependency injection and create the class later on: so instead of having a global PyButton class which relies on a global runtime (whose state is uncertain) we have a make_PyButton function which takes a runtime and make a PyButton class which is tied to that specific runtime (whose state is certainly ready, because we call make_PyButton only when we know that the runtime is ready). Similar for PyInputBox and PyRepl. Other highlights: thanks to this, I could kill the also infamous runAfterRuntimeInitialized and a couple of smelly lines which used setTimeout to "wait" for the runtime. While I was at it, I also called a lot of other stores which were completely unused and where probably leftovers from a past universe.
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
import 'jest';
|
|
import type { Runtime } from "../../src/runtime"
|
|
import { FakeRuntime } from "./fakeruntime"
|
|
import { make_PyRepl } from '../../src/components/pyrepl';
|
|
|
|
const runtime: Runtime = new FakeRuntime();
|
|
const PyRepl = make_PyRepl(runtime);
|
|
customElements.define('py-repl', PyRepl);
|
|
|
|
describe('PyRepl', () => {
|
|
let instance;
|
|
beforeEach(() => {
|
|
instance = new PyRepl();
|
|
});
|
|
|
|
it('should get the current Repl to just instantiate', async () => {
|
|
expect(instance).toBeInstanceOf(PyRepl);
|
|
});
|
|
|
|
it('confirm that codemirror editor is available', async () => {
|
|
// We are assuming that if editorNode has the 'editor-box' class
|
|
// then the div was created properly.
|
|
expect(instance.editorNode.getAttribute('class')).toBe("editor-box")
|
|
})
|
|
|
|
it("connectedCallback gets or sets new id", async () => {
|
|
expect(instance.id).toBe("")
|
|
|
|
instance.connectedCallback()
|
|
|
|
const instanceId = instance.id;
|
|
// id should be similar to py-4850c8c3-d70d-d9e0-03c1-3cfeb0bcec0d
|
|
expect(instanceId).toMatch(/py-(\w+-){1,4}\w+/);
|
|
|
|
// calling checkId directly should return the same id
|
|
instance.checkId();
|
|
expect(instance.id).toEqual(instanceId);
|
|
})
|
|
|
|
it('confirm that calling connectedCallback renders the expected elements', async () => {
|
|
expect(instance.innerHTML).toBe("")
|
|
instance.innerHTML = "<p>Hello</p>"
|
|
instance.connectedCallback()
|
|
|
|
expect(instance.code).toBe("<p>Hello</p>")
|
|
expect(instance.editorNode.id).toBe("code-editor")
|
|
|
|
// Just check that the button was created
|
|
expect(instance.btnRun.getAttribute("class")).toBe("absolute repl-play-button")
|
|
const editorNode = instance.editorNode.innerHTML
|
|
expect(editorNode).toContain("Python Script Run Button")
|
|
// Confirm that our innerHTML is set as well
|
|
expect(editorNode).toContain("Hello")
|
|
})
|
|
|
|
it("confirm that addToOutput updates output element", async () => {
|
|
expect(instance.outputElement).toBe(undefined)
|
|
|
|
// This is just to avoid throwing the test since outputElement is undefined
|
|
instance.outputElement = document.createElement("div")
|
|
|
|
instance.addToOutput("Hello, World!")
|
|
|
|
expect(instance.outputElement.innerHTML).toBe("<div>Hello, World!</div>")
|
|
expect(instance.outputElement.hidden).toBe(false)
|
|
})
|
|
|
|
});
|