@decocms/blocks 7.71.6 → 8.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/deco.js +13 -0
- package/package.json +32 -3
- package/src/cms/schema.ts +1 -1
- package/src/hooks/LiveControls.tsx +2 -0
- package/src/hooks/useLoadMore.ts +2 -0
- package/src/index.ts +37 -0
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +152 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/deferredTrigger.test.ts +0 -32
- package/src/cms/draftSource.test.ts +0 -519
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -557
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/renderJson.test.ts +0 -167
- package/src/cms/resolve.test.ts +0 -1492
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -625
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -239
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/Stats.test.ts +0 -112
- package/src/hooks/useLoadMore.test.ts +0 -205
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -335
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
- package/src/preview/DraftPreviewBadge.test.ts +0 -55
- package/src/sdk/abTesting.test.ts +0 -381
- package/src/sdk/cacheHeaders.test.ts +0 -258
- package/src/sdk/cacheStorage.test.ts +0 -242
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/crypto.test.ts +0 -65
- package/src/sdk/deepOmit.test.ts +0 -45
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/experiments.test.ts +0 -328
- package/src/sdk/fetchCache.test.ts +0 -164
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -691
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -395
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/replaceStream.test.ts +0 -117
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/splitExactRedirects.test.ts +0 -154
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlUtils.test.ts +0 -34
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { renderToString } from "react-dom/server";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { registerSection, registerSectionsSync } from "../cms/registry";
|
|
4
|
-
import RenderSection from "./RenderSection";
|
|
5
|
-
|
|
6
|
-
// NOTE: each test uses its OWN section key. The registry module captures the
|
|
7
|
-
// `globalThis.__deco.*` objects at module load, so re-assigning them in a
|
|
8
|
-
// beforeEach (the registry.test.ts pattern) does not actually clear the
|
|
9
|
-
// registry this module writes to — and RenderSection additionally keeps a
|
|
10
|
-
// module-level lazy cache keyed by resolveType. Unique keys sidestep both.
|
|
11
|
-
|
|
12
|
-
function Hero({ label }: { label?: string }) {
|
|
13
|
-
// Single text node — avoids React's `<!-- -->` separator between
|
|
14
|
-
// adjacent text/expression children, keeping assertions simple.
|
|
15
|
-
return <h1>{`hero-${label ?? "none"}`}</h1>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// A lazy loader that never settles: the lazy path suspends forever, so any
|
|
19
|
-
// content that DOES render must have come from the sync registry.
|
|
20
|
-
const neverResolves = () => new Promise<never>(() => {});
|
|
21
|
-
|
|
22
|
-
describe("RenderSection sync-first", () => {
|
|
23
|
-
it("renders a sync-registered section directly (no Suspense fallback)", () => {
|
|
24
|
-
const KEY = "site/sections/SyncA.tsx";
|
|
25
|
-
registerSectionsSync({ [KEY]: { default: Hero } });
|
|
26
|
-
|
|
27
|
-
const html = renderToString(<RenderSection section={{ __resolveType: KEY, label: "a" }} />);
|
|
28
|
-
expect(html).toContain("hero-a");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("renders sync for the old { Component: string } shape too", () => {
|
|
32
|
-
const KEY = "site/sections/SyncB.tsx";
|
|
33
|
-
registerSectionsSync({ [KEY]: { default: Hero } });
|
|
34
|
-
|
|
35
|
-
const html = renderToString(
|
|
36
|
-
<RenderSection section={{ Component: KEY as any, props: { label: "b" } }} />,
|
|
37
|
-
);
|
|
38
|
-
expect(html).toContain("hero-b");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("prefers the sync component over a pending lazy loader", () => {
|
|
42
|
-
const KEY = "site/sections/SyncC.tsx";
|
|
43
|
-
// Both registered: lazy loader never settles, sync is available.
|
|
44
|
-
// Sync-first must render the content without suspending.
|
|
45
|
-
registerSection(KEY, neverResolves as any);
|
|
46
|
-
registerSectionsSync({ [KEY]: { default: Hero } });
|
|
47
|
-
|
|
48
|
-
const html = renderToString(
|
|
49
|
-
<RenderSection
|
|
50
|
-
section={{ __resolveType: KEY, label: "c" }}
|
|
51
|
-
fallback={<span>loading</span>}
|
|
52
|
-
/>,
|
|
53
|
-
);
|
|
54
|
-
expect(html).toContain("hero-c");
|
|
55
|
-
expect(html).not.toContain("loading");
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("falls back to the lazy path when no sync component is registered", () => {
|
|
59
|
-
const KEY = "site/sections/LazyOnlyD.tsx";
|
|
60
|
-
registerSection(KEY, neverResolves as any);
|
|
61
|
-
|
|
62
|
-
const html = renderToString(
|
|
63
|
-
<RenderSection
|
|
64
|
-
section={{ __resolveType: KEY, label: "d" }}
|
|
65
|
-
fallback={<span>loading</span>}
|
|
66
|
-
/>,
|
|
67
|
-
);
|
|
68
|
-
expect(html).toContain("loading");
|
|
69
|
-
expect(html).not.toContain("hero-d");
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it("renders the fallback when the section is not registered at all", () => {
|
|
73
|
-
const html = renderToString(
|
|
74
|
-
<RenderSection
|
|
75
|
-
section={{ __resolveType: "site/sections/MissingE.tsx" }}
|
|
76
|
-
fallback={<span>missing</span>}
|
|
77
|
-
/>,
|
|
78
|
-
);
|
|
79
|
-
expect(html).toContain("missing");
|
|
80
|
-
});
|
|
81
|
-
});
|
package/src/hooks/Stats.test.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
// The gate and the tag. There is no client behaviour to test — the collector's bundle owns
|
|
2
|
-
// pageviews, SPA navigation and DECO events, and it is tested where it lives. What can break
|
|
3
|
-
// here is what this component actually decides: whether to render at all, where it points, and
|
|
4
|
-
// which attributes it emits.
|
|
5
|
-
//
|
|
6
|
-
// `renderToStaticMarkup` rather than a DOM render: the component has no effects and no state, so
|
|
7
|
-
// mounting it would test React rather than this file.
|
|
8
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
9
|
-
|
|
10
|
-
const ENV = { ...process.env };
|
|
11
|
-
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
process.env = { ...ENV };
|
|
14
|
-
vi.resetModules();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
/** Re-imported per test, because the gate is read at MODULE LOAD. A test that sets the variable
|
|
18
|
-
* after importing would be asserting against the value the previous test left behind — and it
|
|
19
|
-
* would pass or fail depending on file order, which is the worst kind of green. */
|
|
20
|
-
async function render(props: Record<string, unknown> = {}) {
|
|
21
|
-
const { renderToStaticMarkup } = await import("react-dom/server");
|
|
22
|
-
const { default: Stats } = await import("./Stats");
|
|
23
|
-
const { createElement } = await import("react");
|
|
24
|
-
return renderToStaticMarkup(createElement(Stats, props));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe("Stats", () => {
|
|
28
|
-
it("is mounted by the framework, so rendering it unconditionally must be inert", async () => {
|
|
29
|
-
// DecoRootLayout renders <Stats /> for every site, enabled or not. That is only safe
|
|
30
|
-
// because the gate returns null rather than, say, rendering a script pointing at nothing
|
|
31
|
-
// -- a site that never opted in must emit no tag, no preconnect and no request.
|
|
32
|
-
delete process.env.DECO_ANALYTICS_ENABLED;
|
|
33
|
-
expect(await render()).toBe("");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("renders nothing unless explicitly enabled", async () => {
|
|
37
|
-
delete process.env.DECO_ANALYTICS_ENABLED;
|
|
38
|
-
expect(await render()).toBe("");
|
|
39
|
-
|
|
40
|
-
// Not "any truthy value". `ONEDOLLAR_ENABLED` defaults to ON and is disabled with
|
|
41
|
-
// "false"; this one defaults to OFF and needs "true". A loose check here would make
|
|
42
|
-
// `DECO_ANALYTICS_ENABLED=0` turn analytics on, which is the opposite of what anyone
|
|
43
|
-
// setting it to 0 intends.
|
|
44
|
-
process.env.DECO_ANALYTICS_ENABLED = "1";
|
|
45
|
-
vi.resetModules();
|
|
46
|
-
expect(await render()).toBe("");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("points at the same origin by default, and preconnects only when it does not", async () => {
|
|
50
|
-
process.env.DECO_ANALYTICS_ENABLED = "true";
|
|
51
|
-
const same = await render();
|
|
52
|
-
expect(same).toContain('src="/_dq/a.js"');
|
|
53
|
-
// A preconnect to the page's own origin is a wasted hint, and on some browsers a
|
|
54
|
-
// second connection opened for nothing.
|
|
55
|
-
expect(same).not.toContain("preconnect");
|
|
56
|
-
|
|
57
|
-
vi.resetModules();
|
|
58
|
-
process.env.DECO_ANALYTICS_ORIGIN = "https://analytics.example.com";
|
|
59
|
-
const cross = await render();
|
|
60
|
-
expect(cross).toContain('src="https://analytics.example.com/_dq/a.js"');
|
|
61
|
-
expect(cross).toContain("preconnect");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("carries dev and debug as attributes, and omits them when off", async () => {
|
|
65
|
-
process.env.DECO_ANALYTICS_ENABLED = "true";
|
|
66
|
-
// The whole reason these are attributes: TanStack hoists `<script async>` into `<head>`
|
|
67
|
-
// above any inline config block, so a global set alongside the tag loses the race and the
|
|
68
|
-
// collector boots into silence with no error.
|
|
69
|
-
const on = await render({ dev: true, debug: true });
|
|
70
|
-
expect(on).toContain('data-dev="true"');
|
|
71
|
-
expect(on).toContain('data-debug="true"');
|
|
72
|
-
|
|
73
|
-
vi.resetModules();
|
|
74
|
-
const off = await render();
|
|
75
|
-
// Absent, not `="false"`. The collector treats them the same; a reader of the page source
|
|
76
|
-
// does not, and `data-dev="false"` looks like someone decided something.
|
|
77
|
-
expect(off).not.toContain("data-dev");
|
|
78
|
-
expect(off).not.toContain("data-debug");
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("puts the site key in the URL, because that is where the collector reads it", async () => {
|
|
82
|
-
process.env.DECO_ANALYTICS_ENABLED = "true";
|
|
83
|
-
// Sites behind our edge are identified by the Host header, which a visitor cannot forge.
|
|
84
|
-
// Emitting an empty key would put a `tag`-sourced identity on a site that has a
|
|
85
|
-
// trustworthy one, and `tag` is the source that must never reach an invoice.
|
|
86
|
-
const none = await render();
|
|
87
|
-
expect(none).toContain('src="/_dq/a.js"');
|
|
88
|
-
expect(none).not.toContain("?k=");
|
|
89
|
-
|
|
90
|
-
vi.resetModules();
|
|
91
|
-
process.env.DECO_ANALYTICS_SITE_KEY = "dq_abc123";
|
|
92
|
-
const keyed = await render();
|
|
93
|
-
// IN THE QUERY STRING. The collector resolves the site while rendering the bundle, from
|
|
94
|
-
// `?k=` -- a key on the element is read by nothing and arrives after the decision. As
|
|
95
|
-
// `data-site` this rendered fine, resolved nothing, served the `s:"unknown"` fallback and
|
|
96
|
-
// collected zero without an error anywhere.
|
|
97
|
-
expect(keyed).toContain("/_dq/a.js?k=dq_abc123");
|
|
98
|
-
expect(keyed).not.toContain("data-site");
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("uses defer only when asked, async otherwise", async () => {
|
|
102
|
-
process.env.DECO_ANALYTICS_ENABLED = "true";
|
|
103
|
-
// Nothing visual may depend on this script. `async` is what keeps a slow or failed
|
|
104
|
-
// collector from becoming a slow or broken page.
|
|
105
|
-
expect(await render()).toContain("async");
|
|
106
|
-
|
|
107
|
-
vi.resetModules();
|
|
108
|
-
const deferred = await render({ defer: true });
|
|
109
|
-
expect(deferred).toContain("defer");
|
|
110
|
-
expect(deferred).not.toContain("async");
|
|
111
|
-
});
|
|
112
|
-
});
|
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import { createElement } from "react";
|
|
2
|
-
import { act } from "react";
|
|
3
|
-
import { createRoot, type Root } from "react-dom/client";
|
|
4
|
-
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
|
5
|
-
import { useLoadMore, getNextPageUrl } from "./useLoadMore";
|
|
6
|
-
|
|
7
|
-
// ---------------------------------------------------------------------------
|
|
8
|
-
// Minimal hook runner for jsdom (no @testing-library/react dependency)
|
|
9
|
-
// ---------------------------------------------------------------------------
|
|
10
|
-
|
|
11
|
-
function makeHookRunner<T>(useHookFn: () => T) {
|
|
12
|
-
let value!: T;
|
|
13
|
-
let root!: Root;
|
|
14
|
-
const container = document.createElement("div");
|
|
15
|
-
document.body.appendChild(container);
|
|
16
|
-
|
|
17
|
-
const Wrapper = () => {
|
|
18
|
-
value = useHookFn();
|
|
19
|
-
return null;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
async mount() {
|
|
24
|
-
await act(async () => {
|
|
25
|
-
root = createRoot(container);
|
|
26
|
-
root.render(createElement(Wrapper));
|
|
27
|
-
});
|
|
28
|
-
return value;
|
|
29
|
-
},
|
|
30
|
-
get current(): T {
|
|
31
|
-
return value;
|
|
32
|
-
},
|
|
33
|
-
async rerender() {
|
|
34
|
-
await act(async () => {
|
|
35
|
-
root.render(createElement(Wrapper));
|
|
36
|
-
});
|
|
37
|
-
return value;
|
|
38
|
-
},
|
|
39
|
-
async unmount() {
|
|
40
|
-
await act(async () => root.unmount());
|
|
41
|
-
container.remove();
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// ---------------------------------------------------------------------------
|
|
47
|
-
// Test data
|
|
48
|
-
// ---------------------------------------------------------------------------
|
|
49
|
-
|
|
50
|
-
const PAGE1 = { products: ["a"], pageInfo: { nextPage: "/s?page=2" } };
|
|
51
|
-
const PAGE2 = { products: ["b"], pageInfo: { nextPage: undefined } };
|
|
52
|
-
const FLAT_NEXT = { products: ["c"], nextPage: "/s?page=2" };
|
|
53
|
-
|
|
54
|
-
// ---------------------------------------------------------------------------
|
|
55
|
-
// getNextPageUrl (pure helper)
|
|
56
|
-
// ---------------------------------------------------------------------------
|
|
57
|
-
|
|
58
|
-
describe("getNextPageUrl", () => {
|
|
59
|
-
it("reads pageInfo.nextPage", () => {
|
|
60
|
-
expect(getNextPageUrl(PAGE1)).toBe("/s?page=2");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("returns undefined when pageInfo.nextPage is absent", () => {
|
|
64
|
-
expect(getNextPageUrl(PAGE2)).toBeUndefined();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("falls back to flat nextPage", () => {
|
|
68
|
-
expect(getNextPageUrl(FLAT_NEXT)).toBe("/s?page=2");
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("returns undefined for non-objects", () => {
|
|
72
|
-
expect(getNextPageUrl(null)).toBeUndefined();
|
|
73
|
-
expect(getNextPageUrl("string")).toBeUndefined();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// ---------------------------------------------------------------------------
|
|
78
|
-
// useLoadMore (actual hook via React render)
|
|
79
|
-
// ---------------------------------------------------------------------------
|
|
80
|
-
|
|
81
|
-
describe("useLoadMore", () => {
|
|
82
|
-
beforeEach(() => {
|
|
83
|
-
vi.stubGlobal("location", { href: "https://site.com/s?q=telha" });
|
|
84
|
-
});
|
|
85
|
-
afterEach(() => {
|
|
86
|
-
vi.unstubAllGlobals();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it("accumulates pages and flips hasMore off on last page", async () => {
|
|
90
|
-
vi.stubGlobal(
|
|
91
|
-
"fetch",
|
|
92
|
-
vi.fn(() => Promise.resolve({ ok: true, json: async () => PAGE2 })),
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const runner = makeHookRunner(() =>
|
|
96
|
-
useLoadMore(PAGE1, "apps/vtex.ts/loaders/plp.ts"),
|
|
97
|
-
);
|
|
98
|
-
await runner.mount();
|
|
99
|
-
|
|
100
|
-
expect(runner.current.pages).toEqual([PAGE1]);
|
|
101
|
-
expect(runner.current.hasMore).toBe(true);
|
|
102
|
-
|
|
103
|
-
await act(async () => {
|
|
104
|
-
await runner.current.loadMore();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
expect(runner.current.pages).toEqual([PAGE1, PAGE2]);
|
|
108
|
-
expect(runner.current.hasMore).toBe(false);
|
|
109
|
-
await runner.unmount();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("calls /deco/invoke with absolute __pageUrl and __pagePath", async () => {
|
|
113
|
-
const captured: string[] = [];
|
|
114
|
-
vi.stubGlobal(
|
|
115
|
-
"fetch",
|
|
116
|
-
vi.fn((_url: string, init: RequestInit) => {
|
|
117
|
-
captured.push(init.body as string);
|
|
118
|
-
return Promise.resolve({ ok: true, json: async () => PAGE2 });
|
|
119
|
-
}),
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
const runner = makeHookRunner(() =>
|
|
123
|
-
useLoadMore(PAGE1, "apps/vtex.ts/loaders/plp.ts"),
|
|
124
|
-
);
|
|
125
|
-
await runner.mount();
|
|
126
|
-
await act(async () => { await runner.current.loadMore(); });
|
|
127
|
-
|
|
128
|
-
const body = JSON.parse(captured[0]);
|
|
129
|
-
expect(body.props.__pageUrl).toBe("https://site.com/s?page=2");
|
|
130
|
-
expect(body.props.__pagePath).toBe("/s");
|
|
131
|
-
await runner.unmount();
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("sets error state on failed invoke, does not throw", async () => {
|
|
135
|
-
vi.stubGlobal(
|
|
136
|
-
"fetch",
|
|
137
|
-
vi.fn(() => Promise.resolve({ ok: false, status: 500 })),
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
const runner = makeHookRunner(() =>
|
|
141
|
-
useLoadMore(PAGE1, "apps/vtex.ts/loaders/plp.ts"),
|
|
142
|
-
);
|
|
143
|
-
await runner.mount();
|
|
144
|
-
await act(async () => { await runner.current.loadMore(); });
|
|
145
|
-
|
|
146
|
-
expect(runner.current.error).toBeInstanceOf(Error);
|
|
147
|
-
expect(runner.current.error?.message).toContain("500");
|
|
148
|
-
// pages unchanged
|
|
149
|
-
expect(runner.current.pages).toEqual([PAGE1]);
|
|
150
|
-
await runner.unmount();
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("resets pages when resetKey changes", async () => {
|
|
154
|
-
vi.stubGlobal(
|
|
155
|
-
"fetch",
|
|
156
|
-
vi.fn(() => Promise.resolve({ ok: true, json: async () => PAGE2 })),
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
let resetKey = "url-a";
|
|
160
|
-
const runner = makeHookRunner(() =>
|
|
161
|
-
useLoadMore(PAGE1, "apps/vtex.ts/loaders/plp.ts", resetKey),
|
|
162
|
-
);
|
|
163
|
-
await runner.mount();
|
|
164
|
-
|
|
165
|
-
// accumulate one page
|
|
166
|
-
await act(async () => { await runner.current.loadMore(); });
|
|
167
|
-
expect(runner.current.pages).toHaveLength(2);
|
|
168
|
-
|
|
169
|
-
// simulate filter change: resetKey changes, initial stays PAGE1 for simplicity
|
|
170
|
-
resetKey = "url-b";
|
|
171
|
-
await runner.rerender();
|
|
172
|
-
|
|
173
|
-
expect(runner.current.pages).toEqual([PAGE1]);
|
|
174
|
-
expect(runner.current.hasMore).toBe(true);
|
|
175
|
-
await runner.unmount();
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("prevents double-submission (inflight guard)", async () => {
|
|
179
|
-
let resolveFirst!: () => void;
|
|
180
|
-
vi.stubGlobal(
|
|
181
|
-
"fetch",
|
|
182
|
-
vi.fn(
|
|
183
|
-
() =>
|
|
184
|
-
new Promise<{ ok: boolean; json: () => Promise<typeof PAGE2> }>((resolve) => {
|
|
185
|
-
resolveFirst = () => resolve({ ok: true, json: async () => PAGE2 });
|
|
186
|
-
}),
|
|
187
|
-
),
|
|
188
|
-
);
|
|
189
|
-
|
|
190
|
-
const runner = makeHookRunner(() =>
|
|
191
|
-
useLoadMore(PAGE1, "apps/vtex.ts/loaders/plp.ts"),
|
|
192
|
-
);
|
|
193
|
-
await runner.mount();
|
|
194
|
-
|
|
195
|
-
// fire two rapid clicks (no await between them)
|
|
196
|
-
const p1 = act(async () => { runner.current.loadMore(); });
|
|
197
|
-
const p2 = act(async () => { runner.current.loadMore(); });
|
|
198
|
-
resolveFirst();
|
|
199
|
-
await Promise.all([p1, p2]);
|
|
200
|
-
|
|
201
|
-
// fetch must be called only once
|
|
202
|
-
expect(vi.mocked(fetch)).toHaveBeenCalledTimes(1);
|
|
203
|
-
await runner.unmount();
|
|
204
|
-
});
|
|
205
|
-
});
|