release: WRNexusJS 0.2.33
This commit is contained in:
@@ -16,6 +16,39 @@ async function compileAndImport(src: string): Promise<Record<string, unknown>> {
|
||||
return import(pathToFileURL(file).href);
|
||||
}
|
||||
|
||||
type CompiledBehavior = {
|
||||
functions: string;
|
||||
lifecycle: {
|
||||
mount?: string;
|
||||
update?: string;
|
||||
unmount?: string;
|
||||
};
|
||||
watches: Array<{
|
||||
state: string;
|
||||
body: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
function extractCompiledBehavior(output: string): CompiledBehavior {
|
||||
const marker = 'data-wrn-behavior="';
|
||||
|
||||
const start = output.indexOf(marker);
|
||||
|
||||
expect(start).toBeGreaterThanOrEqual(0);
|
||||
|
||||
const valueStart = start + marker.length;
|
||||
|
||||
const valueEnd = output.indexOf('"', valueStart);
|
||||
|
||||
expect(valueEnd).toBeGreaterThan(valueStart);
|
||||
|
||||
const encoded = output.slice(valueStart, valueEnd);
|
||||
|
||||
expect(encoded).toMatch(/^[A-Za-z0-9+/]+=*$/);
|
||||
|
||||
return JSON.parse(Buffer.from(encoded, "base64").toString("utf8")) as CompiledBehavior;
|
||||
}
|
||||
|
||||
test("parses a page with a layout member", () => {
|
||||
const ast = parse(`page Home {\n layout = "public"\n view { <h1>Hi</h1> }\n}`);
|
||||
expect(ast.kind).toBe("page");
|
||||
@@ -382,11 +415,13 @@ component LifecycleTest {
|
||||
`),
|
||||
);
|
||||
|
||||
expect(output).toContain("%22mount%22");
|
||||
const behavior = extractCompiledBehavior(output);
|
||||
|
||||
expect(output).toContain("%22update%22");
|
||||
expect(behavior.lifecycle.mount).toContain("ready = true");
|
||||
|
||||
expect(output).toContain("%22unmount%22");
|
||||
expect(behavior.lifecycle.update).toContain("console.log(ready)");
|
||||
|
||||
expect(behavior.lifecycle.unmount).toContain("ready = false");
|
||||
});
|
||||
test("component watchers compile into behavior metadata", () => {
|
||||
const output = generate(
|
||||
@@ -405,9 +440,13 @@ component WatchTest {
|
||||
`),
|
||||
);
|
||||
|
||||
expect(output).toContain("%22state%22%3A%22visible%22");
|
||||
const behavior = extractCompiledBehavior(output);
|
||||
|
||||
expect(output).toContain("console.log");
|
||||
expect(behavior.watches).toHaveLength(1);
|
||||
|
||||
expect(behavior.watches[0]?.state).toBe("visible");
|
||||
|
||||
expect(behavior.watches[0]?.body).toContain('console.log("changed", visible)');
|
||||
});
|
||||
test("behavior-only components still receive a reactive scope", () => {
|
||||
const output = generate(
|
||||
@@ -479,10 +518,9 @@ component BackToTop {
|
||||
|
||||
expect(output).toContain("data-wrn-behavior=");
|
||||
|
||||
expect(output).toContain("%7B");
|
||||
expect(output).toContain("%22functions%22");
|
||||
|
||||
expect(output).not.toContain('data-wrn-behavior="{');
|
||||
|
||||
expect(output).not.toContain('data-wrn-behavior="%7B');
|
||||
});
|
||||
|
||||
test("component watchers compile into behavior metadata", () => {
|
||||
@@ -506,7 +544,7 @@ component WatchTest {
|
||||
|
||||
expect(match).not.toBeNull();
|
||||
|
||||
const behavior = JSON.parse(decodeURIComponent(match![1]));
|
||||
const behavior = extractBehavior(output);
|
||||
|
||||
expect(behavior.watches).toEqual([
|
||||
{
|
||||
@@ -546,7 +584,7 @@ component LifecycleTest {
|
||||
|
||||
expect(match).not.toBeNull();
|
||||
|
||||
const behavior = JSON.parse(decodeURIComponent(match![1]));
|
||||
const behavior = extractBehavior(output);
|
||||
|
||||
expect(behavior.lifecycle.mount).toContain("ready = true");
|
||||
|
||||
@@ -554,3 +592,21 @@ component LifecycleTest {
|
||||
|
||||
expect(behavior.lifecycle.unmount).toContain("ready = false");
|
||||
});
|
||||
function extractBehavior(output: string): {
|
||||
functions: string;
|
||||
lifecycle: {
|
||||
mount?: string;
|
||||
update?: string;
|
||||
unmount?: string;
|
||||
};
|
||||
watches: Array<{
|
||||
state: string;
|
||||
body: string;
|
||||
}>;
|
||||
} {
|
||||
const match = /data-wrn-behavior="([^"]+)"/.exec(output);
|
||||
|
||||
expect(match).not.toBeNull();
|
||||
|
||||
return JSON.parse(Buffer.from(match![1], "base64").toString("utf8"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user