@decocms/blocks 8.0.0 → 8.1.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 +39 -3
- package/src/cms/applySectionConventions.ts +26 -2
- package/src/cms/blockSource.ts +37 -0
- package/src/cms/client.ts +5 -0
- package/src/cms/deferredTrigger.ts +58 -0
- package/src/cms/draftSource.ts +171 -57
- package/src/cms/index.ts +32 -3
- package/src/cms/loader.ts +318 -59
- package/src/cms/registry.ts +22 -0
- package/src/cms/renderJson.ts +179 -0
- package/src/cms/resolve.ts +396 -95
- package/src/cms/schema.ts +9 -5
- package/src/cms/sectionLoaders.ts +106 -56
- package/src/hooks/Image.tsx +70 -6
- package/src/hooks/LiveControls.tsx +30 -8
- package/src/hooks/Stats.tsx +134 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useLoadMore.ts +100 -0
- package/src/index.ts +37 -0
- package/src/middleware/observability.ts +173 -16
- package/src/preview/DraftPreviewBadge.tsx +306 -0
- package/src/preview/decoMark.ts +11 -0
- package/src/preview/index.ts +8 -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/sdk/abTesting.ts +60 -3
- package/src/sdk/analytics.ts +72 -2
- package/src/sdk/cacheHeaders.ts +210 -10
- package/src/sdk/cacheStorage.ts +287 -0
- package/src/sdk/cachedLoader.ts +143 -90
- package/src/sdk/crypto.ts +16 -2
- package/src/sdk/deepOmit.ts +52 -0
- package/src/sdk/encoding.ts +2 -5
- package/src/sdk/experiments.ts +314 -0
- package/src/sdk/fetchCache.ts +269 -0
- package/src/sdk/fetchTimeout.ts +58 -0
- package/src/sdk/flags.ts +38 -8
- package/src/sdk/index.ts +7 -0
- package/src/sdk/instrumentedFetch.ts +25 -2
- package/src/sdk/nonce.ts +46 -0
- package/src/sdk/normalizeUrls.ts +2 -2
- package/src/sdk/observability.ts +8 -1
- package/src/sdk/otel.ts +131 -25
- package/src/sdk/otelHttpLog.ts +4 -4
- package/src/sdk/otelHttpMeter.ts +39 -13
- package/src/sdk/otelHttpTracer.ts +4 -4
- package/src/sdk/redirects.ts +113 -5
- package/src/sdk/responseCache.ts +82 -0
- package/src/sdk/urlUtils.ts +49 -0
- package/src/sdk/useSuggestions.ts +1 -1
- 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/draftSource.test.ts +0 -343
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -533
- 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 -100
- package/src/hooks/RenderSection.test.tsx +0 -81
- 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 -242
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- 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/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- 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 -526
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/urlRedaction.test.ts +0 -73
- 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,242 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Phase 2 (D-11) coverage for the metric surface — canonical label set,
|
|
3
|
-
* cache_layer, commerce_request_duration_ms. The Phase 1 logger/trace
|
|
4
|
-
* tests live under `src/sdk/logger.test.ts` and `src/sdk/otel.test.ts`;
|
|
5
|
-
* this file focuses on the middleware-level helpers.
|
|
6
|
-
*/
|
|
7
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
8
|
-
import {
|
|
9
|
-
configureMeter,
|
|
10
|
-
type MeterAdapter,
|
|
11
|
-
MetricNames,
|
|
12
|
-
recordCacheMetric,
|
|
13
|
-
recordCommerceMetric,
|
|
14
|
-
recordRequestMetric,
|
|
15
|
-
statusClassFor,
|
|
16
|
-
} from "./observability";
|
|
17
|
-
|
|
18
|
-
interface Counter {
|
|
19
|
-
name: string;
|
|
20
|
-
value: number;
|
|
21
|
-
labels?: Record<string, unknown>;
|
|
22
|
-
}
|
|
23
|
-
interface Histogram {
|
|
24
|
-
name: string;
|
|
25
|
-
value: number;
|
|
26
|
-
labels?: Record<string, unknown>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function captureMeter(): {
|
|
30
|
-
adapter: MeterAdapter;
|
|
31
|
-
counters: Counter[];
|
|
32
|
-
histograms: Histogram[];
|
|
33
|
-
} {
|
|
34
|
-
const counters: Counter[] = [];
|
|
35
|
-
const histograms: Histogram[] = [];
|
|
36
|
-
const adapter: MeterAdapter = {
|
|
37
|
-
counterInc(name, value, labels) {
|
|
38
|
-
counters.push({ name, value: value ?? 1, labels });
|
|
39
|
-
},
|
|
40
|
-
histogramRecord(name, value, labels) {
|
|
41
|
-
histograms.push({ name, value, labels });
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
return { adapter, counters, histograms };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
describe("statusClassFor", () => {
|
|
48
|
-
it("maps 2xx / 3xx / 4xx / 5xx to canonical class labels", () => {
|
|
49
|
-
expect(statusClassFor(200)).toBe("2xx");
|
|
50
|
-
expect(statusClassFor(204)).toBe("2xx");
|
|
51
|
-
expect(statusClassFor(301)).toBe("3xx");
|
|
52
|
-
expect(statusClassFor(404)).toBe("4xx");
|
|
53
|
-
expect(statusClassFor(500)).toBe("5xx");
|
|
54
|
-
expect(statusClassFor(503)).toBe("5xx");
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("returns 'unknown' for out-of-range / NaN / non-numeric inputs", () => {
|
|
58
|
-
expect(statusClassFor(-1)).toBe("unknown");
|
|
59
|
-
expect(statusClassFor(99)).toBe("unknown");
|
|
60
|
-
expect(statusClassFor(600)).toBe("unknown");
|
|
61
|
-
expect(statusClassFor(Number.NaN)).toBe("unknown");
|
|
62
|
-
expect(statusClassFor(Infinity)).toBe("unknown");
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe("recordRequestMetric — canonical labels (D-11)", () => {
|
|
67
|
-
afterEach(() => {
|
|
68
|
-
// Reset meter so other tests start clean.
|
|
69
|
-
configureMeter({ counterInc: () => {} });
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it("stamps method + route_pattern + status + status_class by default", () => {
|
|
73
|
-
const { adapter, counters, histograms } = captureMeter();
|
|
74
|
-
configureMeter(adapter);
|
|
75
|
-
|
|
76
|
-
recordRequestMetric("GET", "/products/abc123/p", 200, 42);
|
|
77
|
-
|
|
78
|
-
// Canonical OTel HTTP server metric is histogram-only; the count
|
|
79
|
-
// dimension is derived from the histogram's bucket counts at query
|
|
80
|
-
// time, so we no longer emit a parallel `_total` counter.
|
|
81
|
-
expect(counters).toHaveLength(0);
|
|
82
|
-
expect(histograms).toHaveLength(1);
|
|
83
|
-
expect(histograms[0]?.name).toBe(MetricNames.HTTP_SERVER_REQUEST_DURATION);
|
|
84
|
-
expect(histograms[0]?.value).toBe(0.042); // seconds (semconv)
|
|
85
|
-
expect(histograms[0]?.labels).toMatchObject({
|
|
86
|
-
"http.request.method": "GET",
|
|
87
|
-
// Default normalization: dynamic segments collapsed.
|
|
88
|
-
"http.route": "/products/:slug/p",
|
|
89
|
-
"http.response.status_code": 200,
|
|
90
|
-
"deco.http.status_class": "2xx",
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("prefers caller-supplied route_pattern over normalized path", () => {
|
|
95
|
-
const { adapter, histograms } = captureMeter();
|
|
96
|
-
configureMeter(adapter);
|
|
97
|
-
|
|
98
|
-
recordRequestMetric("GET", "/anything/random/123", 200, 5, {
|
|
99
|
-
route_pattern: "/_products/$slug/p",
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
expect(histograms[0]?.labels?.["http.route"]).toBe("/_products/$slug/p");
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("tags 5xx requests with status_class=5xx for downstream error filtering", () => {
|
|
106
|
-
const { adapter, histograms } = captureMeter();
|
|
107
|
-
configureMeter(adapter);
|
|
108
|
-
|
|
109
|
-
recordRequestMetric("POST", "/checkout", 503, 120);
|
|
110
|
-
|
|
111
|
-
expect(histograms[0]?.labels?.["deco.http.status_class"]).toBe("5xx");
|
|
112
|
-
expect(histograms[0]?.labels?.["http.response.status_code"]).toBe(503);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("propagates optional labels (outcome, cache_decision, cache_layer, region, extra)", () => {
|
|
116
|
-
const { adapter, histograms } = captureMeter();
|
|
117
|
-
configureMeter(adapter);
|
|
118
|
-
|
|
119
|
-
recordRequestMetric("GET", "/", 200, 10, {
|
|
120
|
-
outcome: "ok",
|
|
121
|
-
cache_decision: "STALE-HIT",
|
|
122
|
-
cache_layer: "edge",
|
|
123
|
-
region: "GRU",
|
|
124
|
-
extra: { ab_variant: "B" },
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
expect(histograms[0]?.labels).toMatchObject({
|
|
128
|
-
"deco.http.outcome": "ok",
|
|
129
|
-
"deco.cache.decision": "STALE-HIT",
|
|
130
|
-
"deco.cache.layer": "edge",
|
|
131
|
-
"deco.http.region": "GRU",
|
|
132
|
-
ab_variant: "B",
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("is a no-op when no meter is configured", () => {
|
|
137
|
-
// We can't easily prove a no-op other than verifying no throw —
|
|
138
|
-
// safer than calling configureMeter(null), which would mask real
|
|
139
|
-
// bugs. The previous test's `afterEach` reset already gives us a
|
|
140
|
-
// bare meter; this test confirms the call is benign.
|
|
141
|
-
expect(() => recordRequestMetric("GET", "/", 200, 1)).not.toThrow();
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe("recordCacheMetric — cache_layer label", () => {
|
|
146
|
-
beforeEach(() => {
|
|
147
|
-
configureMeter({ counterInc: () => {} });
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("stamps profile + decision + layer when all are provided", () => {
|
|
151
|
-
const { adapter, counters } = captureMeter();
|
|
152
|
-
configureMeter(adapter);
|
|
153
|
-
|
|
154
|
-
recordCacheMetric(true, "product", "HIT", "edge");
|
|
155
|
-
|
|
156
|
-
expect(counters).toHaveLength(1);
|
|
157
|
-
expect(counters[0]?.name).toBe(MetricNames.CACHE_REQUESTS);
|
|
158
|
-
expect(counters[0]?.labels).toMatchObject({
|
|
159
|
-
"deco.cache.profile": "product",
|
|
160
|
-
"deco.cache.status": "HIT",
|
|
161
|
-
"deco.cache.layer": "edge",
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it("records status=MISS when hit=false", () => {
|
|
166
|
-
const { adapter, counters } = captureMeter();
|
|
167
|
-
configureMeter(adapter);
|
|
168
|
-
|
|
169
|
-
recordCacheMetric(false, "search", "MISS", "edge");
|
|
170
|
-
|
|
171
|
-
expect(counters[0]?.name).toBe(MetricNames.CACHE_REQUESTS);
|
|
172
|
-
expect(counters[0]?.labels?.["deco.cache.status"]).toBe("MISS");
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it("supports the legacy 3-arg signature for backward compat", () => {
|
|
176
|
-
const { adapter, counters } = captureMeter();
|
|
177
|
-
configureMeter(adapter);
|
|
178
|
-
|
|
179
|
-
recordCacheMetric(true, "static");
|
|
180
|
-
|
|
181
|
-
expect(counters[0]?.labels).toEqual({
|
|
182
|
-
"deco.cache.status": "HIT",
|
|
183
|
-
"deco.cache.profile": "static",
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it("distinguishes cachedLoader vs edge vs vtex-swr layers", () => {
|
|
188
|
-
const { adapter, counters } = captureMeter();
|
|
189
|
-
configureMeter(adapter);
|
|
190
|
-
|
|
191
|
-
recordCacheMetric(true, "loader-x", "HIT", "cachedLoader");
|
|
192
|
-
recordCacheMetric(true, "vtex-product", "HIT", "vtex-swr");
|
|
193
|
-
|
|
194
|
-
expect(counters[0]?.labels?.["deco.cache.layer"]).toBe("cachedLoader");
|
|
195
|
-
expect(counters[1]?.labels?.["deco.cache.layer"]).toBe("vtex-swr");
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
describe("recordCommerceMetric (D-11)", () => {
|
|
200
|
-
beforeEach(() => {
|
|
201
|
-
configureMeter({ counterInc: () => {} });
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it("emits http.client.request.duration with provider + operation labels", () => {
|
|
205
|
-
const { adapter, histograms } = captureMeter();
|
|
206
|
-
configureMeter(adapter);
|
|
207
|
-
|
|
208
|
-
recordCommerceMetric(123, {
|
|
209
|
-
provider: "vtex",
|
|
210
|
-
operation: "intelligent-search.product_search",
|
|
211
|
-
status_class: "2xx",
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
expect(histograms).toHaveLength(1);
|
|
215
|
-
expect(histograms[0]?.name).toBe(MetricNames.HTTP_CLIENT_REQUEST_DURATION);
|
|
216
|
-
expect(histograms[0]?.value).toBe(0.123); // seconds (semconv)
|
|
217
|
-
expect(histograms[0]?.labels).toMatchObject({
|
|
218
|
-
provider: "vtex",
|
|
219
|
-
operation: "intelligent-search.product_search",
|
|
220
|
-
status_class: "2xx",
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("includes the cached boolean when provided", () => {
|
|
225
|
-
const { adapter, histograms } = captureMeter();
|
|
226
|
-
configureMeter(adapter);
|
|
227
|
-
|
|
228
|
-
recordCommerceMetric(5, {
|
|
229
|
-
provider: "shopify",
|
|
230
|
-
operation: "graphql.cart_query",
|
|
231
|
-
cached: true,
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
expect(histograms[0]?.labels?.cached).toBe(true);
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it("is a no-op when no meter is configured", () => {
|
|
238
|
-
expect(() =>
|
|
239
|
-
recordCommerceMetric(1, { provider: "vtex", operation: "test" }),
|
|
240
|
-
).not.toThrow();
|
|
241
|
-
});
|
|
242
|
-
});
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
createSectionValidator,
|
|
4
|
-
validateDeferredSectionInput,
|
|
5
|
-
} from "./validateSection";
|
|
6
|
-
|
|
7
|
-
describe("validateDeferredSectionInput", () => {
|
|
8
|
-
it("accepts valid input", () => {
|
|
9
|
-
const result = validateDeferredSectionInput({
|
|
10
|
-
component: "site/sections/ProductShelf.tsx",
|
|
11
|
-
rawProps: { title: "Best Sellers" },
|
|
12
|
-
pagePath: "/",
|
|
13
|
-
});
|
|
14
|
-
expect(result.component).toBe("site/sections/ProductShelf.tsx");
|
|
15
|
-
expect(result.rawProps).toEqual({ title: "Best Sellers" });
|
|
16
|
-
expect(result.pagePath).toBe("/");
|
|
17
|
-
expect(result.pageUrl).toBeUndefined();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("accepts input with optional pageUrl", () => {
|
|
21
|
-
const result = validateDeferredSectionInput({
|
|
22
|
-
component: "site/sections/Hero.tsx",
|
|
23
|
-
rawProps: {},
|
|
24
|
-
pagePath: "/home",
|
|
25
|
-
pageUrl: "https://store.com/home?ref=nav",
|
|
26
|
-
});
|
|
27
|
-
expect(result.pageUrl).toBe("https://store.com/home?ref=nav");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("accepts input with index for SPA navigation ordering", () => {
|
|
31
|
-
const result = validateDeferredSectionInput({
|
|
32
|
-
component: "site/sections/Shelf.tsx",
|
|
33
|
-
rawProps: {},
|
|
34
|
-
pagePath: "/",
|
|
35
|
-
index: 5,
|
|
36
|
-
});
|
|
37
|
-
expect(result.index).toBe(5);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("index is undefined when not provided", () => {
|
|
41
|
-
const result = validateDeferredSectionInput({
|
|
42
|
-
component: "site/sections/Shelf.tsx",
|
|
43
|
-
rawProps: {},
|
|
44
|
-
pagePath: "/",
|
|
45
|
-
});
|
|
46
|
-
expect(result.index).toBeUndefined();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("throws on null input", () => {
|
|
50
|
-
expect(() => validateDeferredSectionInput(null)).toThrow("Expected an object");
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("throws on undefined input", () => {
|
|
54
|
-
expect(() => validateDeferredSectionInput(undefined)).toThrow("Expected an object");
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("throws on string input", () => {
|
|
58
|
-
expect(() => validateDeferredSectionInput("nope")).toThrow("Expected an object");
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("throws when component is missing", () => {
|
|
62
|
-
expect(() =>
|
|
63
|
-
validateDeferredSectionInput({ rawProps: {}, pagePath: "/" }),
|
|
64
|
-
).toThrow("component");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("throws when component is not a string", () => {
|
|
68
|
-
expect(() =>
|
|
69
|
-
validateDeferredSectionInput({ component: 123, rawProps: {}, pagePath: "/" }),
|
|
70
|
-
).toThrow("component");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("throws when rawProps is missing", () => {
|
|
74
|
-
expect(() =>
|
|
75
|
-
validateDeferredSectionInput({ component: "X", pagePath: "/" }),
|
|
76
|
-
).toThrow("rawProps");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("throws when rawProps is an array", () => {
|
|
80
|
-
expect(() =>
|
|
81
|
-
validateDeferredSectionInput({ component: "X", rawProps: [1, 2], pagePath: "/" }),
|
|
82
|
-
).toThrow("rawProps");
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("throws when rawProps is null", () => {
|
|
86
|
-
expect(() =>
|
|
87
|
-
validateDeferredSectionInput({ component: "X", rawProps: null, pagePath: "/" }),
|
|
88
|
-
).toThrow("rawProps");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("throws when pagePath is missing", () => {
|
|
92
|
-
expect(() =>
|
|
93
|
-
validateDeferredSectionInput({ component: "X", rawProps: {} }),
|
|
94
|
-
).toThrow("pagePath");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("throws when pageUrl is not a string", () => {
|
|
98
|
-
expect(() =>
|
|
99
|
-
validateDeferredSectionInput({
|
|
100
|
-
component: "X",
|
|
101
|
-
rawProps: {},
|
|
102
|
-
pagePath: "/",
|
|
103
|
-
pageUrl: 42,
|
|
104
|
-
}),
|
|
105
|
-
).toThrow("pageUrl");
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("throws when index is not a number", () => {
|
|
109
|
-
expect(() =>
|
|
110
|
-
validateDeferredSectionInput({
|
|
111
|
-
component: "X",
|
|
112
|
-
rawProps: {},
|
|
113
|
-
pagePath: "/",
|
|
114
|
-
index: "three" as any,
|
|
115
|
-
}),
|
|
116
|
-
).toThrow("index");
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe("createSectionValidator", () => {
|
|
121
|
-
it("passes when all required fields present", () => {
|
|
122
|
-
const validate = createSectionValidator(["title", "maxItems"]);
|
|
123
|
-
const result = validate({ title: "Shelf", maxItems: 8 });
|
|
124
|
-
expect(result).toEqual({ title: "Shelf", maxItems: 8 });
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("throws when a required field is missing", () => {
|
|
128
|
-
const validate = createSectionValidator(["title", "maxItems"]);
|
|
129
|
-
expect(() => validate({ title: "Shelf" })).toThrow("maxItems");
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it("throws on null input", () => {
|
|
133
|
-
const validate = createSectionValidator(["title"]);
|
|
134
|
-
expect(() => validate(null)).toThrow("Expected an object");
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("allows extra fields", () => {
|
|
138
|
-
const validate = createSectionValidator(["title"]);
|
|
139
|
-
const result = validate({ title: "Hi", extra: true });
|
|
140
|
-
expect(result).toEqual({ title: "Hi", extra: true });
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it("empty required fields always passes for objects", () => {
|
|
144
|
-
const validate = createSectionValidator([]);
|
|
145
|
-
expect(validate({})).toEqual({});
|
|
146
|
-
});
|
|
147
|
-
});
|