~james/core

core/web/scripts/staleAwait.test.ts -rw-r--r-- 1.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Linter } from "eslint";
import svelte from "eslint-plugin-svelte";
import { describe, expect, it } from "vitest";
import { staleAwait } from "./staleAwait.ts";

const linter = new Linter();
const config = [
	...svelte.configs.base,
	{ files: ["**/*.svelte"], rules: { "no-restricted-syntax": ["error" as const, ...staleAwait] } }
];

const flagged = (markup: string) =>
	linter
		.verify(`<script>let { data } = $props();</script>\n${markup}`, config, "+page.svelte")
		.map((message) => message.line - 1);

describe("stale await", () => {
	it("flags the issue list before 32879b21f3", () =>
		expect(
			flagged(`{#await data.issues}
	<Bones />
{:then issues}
	<TicketList tickets={issues} filtered={withoutStates(data.query) !== ""} />
{/await}`)
		).toEqual([3]));

	it("flags a then branch that reads page", () =>
		expect(flagged("{#await data.x then x}{page.url.search}{/await}")).toEqual([1]));
	it("flags a pending or catch branch that reads data", () => {
		expect(flagged("{#await data.x}{data.q}{:then x}ok{/await}")).toEqual([1]);
		expect(flagged("{#await data.x}wait{:then x}ok{:catch e}{data.q}{/await}")).toEqual([1]);
	});
	it("flags one block once, however often it reads", () =>
		expect(flagged("{#await data.x then x}{data.a}{data.b}{/await}")).toHaveLength(1));

	it("leaves a branch that only reads the settled value", () =>
		expect(flagged("{#await data.x}wait{:then x}{x.length}{/await}")).toEqual([]));
	it("leaves the expression alone, it runs with the navigation", () =>
		expect(flagged("{#await data.x then x}{x.length}{/await}")).toEqual([]));
	it("leaves a property or key that happens to be called data", () => {
		expect(flagged("{#await load() then x}{x.data}{/await}")).toEqual([]);
		expect(flagged("{#await load() then x}{JSON.stringify({ data: x })}{/await}")).toEqual([]);
	});
});