@canonical/summon-application 0.29.0-experimental.0 → 0.30.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/LICENSE +166 -0
- package/README.md +75 -13
- package/dist/esm/application/react/index.js +347 -0
- package/dist/esm/application/react/index.js.map +1 -0
- package/dist/esm/application/react/templates/.browserslistrc +2 -0
- package/dist/esm/application/react/templates/.storybook/main.ts.ejs +10 -0
- package/{src → dist/esm}/application/react/templates/README.md.ejs +54 -0
- package/dist/esm/application/react/templates/biome.json.ejs +6 -0
- package/dist/esm/application/react/templates/gitignore +25 -0
- package/{src → dist/esm}/application/react/templates/package.json.ejs +36 -2
- package/dist/esm/application/react/templates/patches/react-relay@18.2.0.patch +52 -0
- package/dist/esm/application/react/templates/patches/relay-runtime-network@0.1.0.patch +46 -0
- package/dist/esm/application/react/templates/relay.config.json +8 -0
- package/{src/application/react/templates/src/client/entry.tsx → dist/esm/application/react/templates/src/client/entry.tsx.ejs} +23 -0
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx +50 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tests.tsx +56 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ErrorBoundary.tsx +37 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductCard.tsx +49 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.stories.tsx +103 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tests.tsx +80 -0
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx +59 -0
- package/dist/esm/application/react/templates/src/domains/catalog/routes.ts +11 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tests.tsx +35 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/ClientOnly.tsx +31 -0
- package/dist/esm/application/react/templates/src/lib/ClientOnly/index.ts +1 -0
- package/{src → dist/esm}/application/react/templates/src/lib/Navigation/Navigation.tsx.ejs +3 -0
- package/{src/application/react/templates/src/lib/index.ts → dist/esm/application/react/templates/src/lib/index.ts.ejs} +3 -0
- package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts +82 -0
- package/dist/esm/application/react/templates/src/relay/__generated__/ProductListQuery.graphql.ts +272 -0
- package/dist/esm/application/react/templates/src/relay/environment.tests.ts +87 -0
- package/dist/esm/application/react/templates/src/relay/environment.ts +119 -0
- package/dist/esm/application/react/templates/src/relay/schema.graphql +111 -0
- package/dist/esm/application/react/templates/src/relay/schema.tests.ts +137 -0
- package/dist/esm/application/react/templates/src/relay/schema.ts +205 -0
- package/{src → dist/esm}/application/react/templates/src/routes.tsx.ejs +10 -0
- package/{src/application/react/templates/src/server/entry.tsx → dist/esm/application/react/templates/src/server/entry.tsx.ejs} +36 -0
- package/{src → dist/esm}/application/react/templates/src/sitemap/getSitemapItems.ts.ejs +3 -0
- package/{src/application/react/templates/tsconfig.json → dist/esm/application/react/templates/tsconfig.json.ejs} +3 -0
- package/dist/esm/application/react/templates/vite.config.ts.ejs +92 -0
- package/dist/esm/application/react/templates/vitest.setup.ts.ejs +22 -0
- package/dist/esm/domain/index.js +89 -0
- package/dist/esm/domain/index.js.map +1 -0
- package/{src/index.ts → dist/esm/index.js} +6 -8
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/route/index.js +115 -0
- package/dist/esm/route/index.js.map +1 -0
- package/dist/esm/route/insertRoute.js +178 -0
- package/dist/esm/route/insertRoute.js.map +1 -0
- package/dist/esm/shared/casing.js +15 -0
- package/dist/esm/shared/casing.js.map +1 -0
- package/dist/esm/shared/packageManager.js +38 -0
- package/dist/esm/shared/packageManager.js.map +1 -0
- package/dist/esm/shared/versions.js +88 -0
- package/dist/esm/shared/versions.js.map +1 -0
- package/dist/esm/wrapper/index.js +72 -0
- package/dist/esm/wrapper/index.js.map +1 -0
- package/dist/types/application/react/index.d.ts +12 -0
- package/dist/types/application/react/index.d.ts.map +1 -0
- package/dist/types/domain/index.d.ts +7 -0
- package/dist/types/domain/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/route/index.d.ts +7 -0
- package/dist/types/route/index.d.ts.map +1 -0
- package/dist/types/route/insertRoute.d.ts +34 -0
- package/dist/types/route/insertRoute.d.ts.map +1 -0
- package/{src/shared/casing.ts → dist/types/shared/casing.d.ts} +2 -7
- package/dist/types/shared/casing.d.ts.map +1 -0
- package/dist/types/shared/packageManager.d.ts +19 -0
- package/dist/types/shared/packageManager.d.ts.map +1 -0
- package/dist/types/shared/versions.d.ts +15 -0
- package/dist/types/shared/versions.d.ts.map +1 -0
- package/dist/types/wrapper/index.d.ts +7 -0
- package/dist/types/wrapper/index.d.ts.map +1 -0
- package/package.json +17 -15
- package/src/application/react/index.ts +0 -294
- package/src/application/react/templates/.storybook/main.ts +0 -5
- package/src/application/react/templates/biome.json.ejs +0 -6
- package/src/application/react/templates/vite.config.ts +0 -45
- package/src/application/react/templates/vitest.setup.ts +0 -9
- package/src/domain/index.ts +0 -119
- package/src/index.test.ts +0 -398
- package/src/route/index.ts +0 -154
- package/src/route/insertRoute.test.ts +0 -98
- package/src/route/insertRoute.ts +0 -236
- package/src/shared/versions.ts +0 -48
- package/src/wrapper/index.ts +0 -100
- /package/{src → dist/esm}/application/react/templates/.storybook/decorators/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/.storybook/decorators/withRouter.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/.storybook/preview.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/index.html.ejs +0 -0
- /package/{src → dist/esm}/application/react/templates/public/.gitkeep +0 -0
- /package/{src → dist/esm}/application/react/templates/public/robots.txt +0 -0
- /package/{src → dist/esm}/application/react/templates/src/assets/.gitkeep +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/AccountPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/LoginPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/account/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/contact/ContactPage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/contact/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/GuidePage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/HomePage.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/domains/marketing/routes.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.stories.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tests.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/ExampleComponent.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/styles.css +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ExampleComponent/types.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.stories.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/LazyComponent.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/LazyComponent/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/Navigation/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/ThemeSelector.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/lib/ThemeSelector/index.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/preview.bun.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/preview.express.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/renderer.tsx +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/server.bun.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/server/server.express.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/sitemap/renderer.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/src/styles/app.css +0 -0
- /package/{src → dist/esm}/application/react/templates/src/styles/index.css.ejs +0 -0
- /package/{src → dist/esm}/application/react/templates/src/vite-env.d.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/test/e2e/serverHarness.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/test/e2e/servers.e2e.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/vitest.config.ts +0 -0
- /package/{src → dist/esm}/application/react/templates/vitest.e2e.config.ts +0 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { RelayParameters } from "@canonical/storybook-addon-relay";
|
|
2
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
3
|
+
import { Suspense } from "react";
|
|
4
|
+
import { expect } from "storybook/test";
|
|
5
|
+
import Component from "./ProductList.js";
|
|
6
|
+
|
|
7
|
+
const meta = {
|
|
8
|
+
title: "Catalog/ProductList",
|
|
9
|
+
component: Component,
|
|
10
|
+
// `useLazyLoadQuery` suspends while the (mocked) query is in flight, so
|
|
11
|
+
// every story renders inside a Suspense boundary — the same shape the
|
|
12
|
+
// catalog page uses.
|
|
13
|
+
decorators: [
|
|
14
|
+
(Story) => (
|
|
15
|
+
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
16
|
+
<Story />
|
|
17
|
+
</Suspense>
|
|
18
|
+
),
|
|
19
|
+
],
|
|
20
|
+
} satisfies Meta<typeof Component>;
|
|
21
|
+
|
|
22
|
+
export default meta;
|
|
23
|
+
|
|
24
|
+
type Story = StoryObj<typeof meta>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `@canonical/storybook-addon-relay` reads `parameters.relay` and resolves
|
|
28
|
+
* the story's operations with `MockPayloadGenerator` + these resolvers.
|
|
29
|
+
* Every displayed field is resolved explicitly so the story is deterministic
|
|
30
|
+
* for visual regression testing.
|
|
31
|
+
*/
|
|
32
|
+
export const Default: Story = {
|
|
33
|
+
parameters: {
|
|
34
|
+
relay: {
|
|
35
|
+
mockResolvers: {
|
|
36
|
+
Viewer: () => ({ name: "Ada Lovelace" }),
|
|
37
|
+
ProductConnection: () => ({
|
|
38
|
+
totalCount: 5,
|
|
39
|
+
pageInfo: { hasNextPage: true },
|
|
40
|
+
edges: [
|
|
41
|
+
{
|
|
42
|
+
node: {
|
|
43
|
+
id: "Product:story-1",
|
|
44
|
+
name: "Aurora Dev Board",
|
|
45
|
+
tagline: "A hackable single-board computer for prototyping",
|
|
46
|
+
priceCents: 14_900,
|
|
47
|
+
currency: "USD",
|
|
48
|
+
rating: 4.6,
|
|
49
|
+
inStock: true,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
node: {
|
|
54
|
+
id: "Product:story-2",
|
|
55
|
+
name: "Polar Sensor Kit",
|
|
56
|
+
tagline: "Twelve calibrated sensors in a rugged case",
|
|
57
|
+
priceCents: 8_900,
|
|
58
|
+
currency: "USD",
|
|
59
|
+
rating: 4.2,
|
|
60
|
+
inStock: false,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
} satisfies RelayParameters,
|
|
67
|
+
},
|
|
68
|
+
play: async ({ canvas }) => {
|
|
69
|
+
await expect(
|
|
70
|
+
await canvas.findByText("Aurora Dev Board"),
|
|
71
|
+
).toBeInTheDocument();
|
|
72
|
+
await expect(canvas.getByText("Ada Lovelace")).toBeInTheDocument();
|
|
73
|
+
await expect(
|
|
74
|
+
canvas.getByText(/showing 2 of 5 products/),
|
|
75
|
+
).toBeInTheDocument();
|
|
76
|
+
// The Polar Sensor Kit is the story's out-of-stock product.
|
|
77
|
+
await expect(canvas.getByText(/out of stock/)).toBeInTheDocument();
|
|
78
|
+
await expect(
|
|
79
|
+
canvas.getByText("More products are available."),
|
|
80
|
+
).toBeInTheDocument();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const EmptyCatalog: Story = {
|
|
85
|
+
parameters: {
|
|
86
|
+
relay: {
|
|
87
|
+
mockResolvers: {
|
|
88
|
+
Viewer: () => ({ name: "Ada Lovelace" }),
|
|
89
|
+
ProductConnection: () => ({
|
|
90
|
+
totalCount: 0,
|
|
91
|
+
pageInfo: { hasNextPage: false },
|
|
92
|
+
edges: [],
|
|
93
|
+
}),
|
|
94
|
+
},
|
|
95
|
+
} satisfies RelayParameters,
|
|
96
|
+
},
|
|
97
|
+
play: async ({ canvas }) => {
|
|
98
|
+
await expect(
|
|
99
|
+
await canvas.findByText(/showing 0 of 0 products/),
|
|
100
|
+
).toBeInTheDocument();
|
|
101
|
+
await expect(canvas.queryByRole("article")).not.toBeInTheDocument();
|
|
102
|
+
},
|
|
103
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { Suspense } from "react";
|
|
3
|
+
import { RelayEnvironmentProvider } from "react-relay";
|
|
4
|
+
import type { IEnvironment } from "relay-runtime";
|
|
5
|
+
import { createMockEnvironment, MockPayloadGenerator } from "relay-test-utils";
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
import { createEnvironment } from "#relay/environment.js";
|
|
8
|
+
import { CATALOG_PRODUCTS } from "#relay/schema.js";
|
|
9
|
+
import ProductList from "./ProductList.js";
|
|
10
|
+
|
|
11
|
+
const renderProductList = (environment: IEnvironment) =>
|
|
12
|
+
render(
|
|
13
|
+
<RelayEnvironmentProvider environment={environment}>
|
|
14
|
+
<Suspense fallback={<p>Loading catalog…</p>}>
|
|
15
|
+
<ProductList />
|
|
16
|
+
</Suspense>
|
|
17
|
+
</RelayEnvironmentProvider>,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
describe("ProductList component", () => {
|
|
21
|
+
it("renders the first page from the app's local mock schema", async () => {
|
|
22
|
+
// Integration path: the real factory, whose local executor resolves the
|
|
23
|
+
// query against src/relay/schema.ts — no relay-test-utils involved.
|
|
24
|
+
renderProductList(createEnvironment());
|
|
25
|
+
|
|
26
|
+
const firstProduct = CATALOG_PRODUCTS[0];
|
|
27
|
+
expect(
|
|
28
|
+
await screen.findByRole("article", { name: firstProduct?.name }),
|
|
29
|
+
).toBeInTheDocument();
|
|
30
|
+
expect(screen.getByText("Ada Lovelace")).toBeInTheDocument();
|
|
31
|
+
expect(
|
|
32
|
+
screen.getByText(new RegExp(`of ${CATALOG_PRODUCTS.length} products`)),
|
|
33
|
+
).toBeInTheDocument();
|
|
34
|
+
// Product:3 (Beacon Micro Server) is the first page's out-of-stock,
|
|
35
|
+
// 4.2-rated product — pins the stock label and the rating formatting.
|
|
36
|
+
expect(screen.getByText(/out of stock/)).toBeInTheDocument();
|
|
37
|
+
expect(screen.getByText(/rated 4\.2 \/ 5/)).toBeInTheDocument();
|
|
38
|
+
expect(
|
|
39
|
+
screen.getByText("More products are available."),
|
|
40
|
+
).toBeInTheDocument();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("renders payloads resolved by a relay-test-utils mock environment", async () => {
|
|
44
|
+
const environment = createMockEnvironment();
|
|
45
|
+
// Queue the resolver before rendering: useLazyLoadQuery issues the
|
|
46
|
+
// request during render, and the queued resolver answers it immediately.
|
|
47
|
+
environment.mock.queueOperationResolver((operation) =>
|
|
48
|
+
MockPayloadGenerator.generate(operation, {
|
|
49
|
+
Viewer: () => ({ name: "Grace Hopper" }),
|
|
50
|
+
ProductConnection: () => ({
|
|
51
|
+
totalCount: 1,
|
|
52
|
+
pageInfo: { hasNextPage: false },
|
|
53
|
+
edges: [
|
|
54
|
+
{
|
|
55
|
+
node: {
|
|
56
|
+
id: "Product:mocked",
|
|
57
|
+
name: "Mocked Product",
|
|
58
|
+
tagline: "Straight from MockPayloadGenerator",
|
|
59
|
+
priceCents: 12_500,
|
|
60
|
+
currency: "USD",
|
|
61
|
+
rating: 4.5,
|
|
62
|
+
inStock: true,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
}),
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
renderProductList(environment);
|
|
71
|
+
|
|
72
|
+
expect(await screen.findByText("Mocked Product")).toBeInTheDocument();
|
|
73
|
+
expect(screen.getByText("Grace Hopper")).toBeInTheDocument();
|
|
74
|
+
expect(screen.getByText("$125.00", { exact: false })).toBeInTheDocument();
|
|
75
|
+
expect(screen.getByText(/in stock/)).toBeInTheDocument();
|
|
76
|
+
expect(
|
|
77
|
+
screen.queryByText("More products are available."),
|
|
78
|
+
).not.toBeInTheDocument();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { graphql, useLazyLoadQuery } from "react-relay";
|
|
3
|
+
import type { ProductListQuery } from "#relay/__generated__/ProductListQuery.graphql.js";
|
|
4
|
+
import ProductCard from "./ProductCard.js";
|
|
5
|
+
|
|
6
|
+
/** How many products the list requests from the connection. */
|
|
7
|
+
const PAGE_SIZE = 4;
|
|
8
|
+
|
|
9
|
+
const productListQuery = graphql`
|
|
10
|
+
query ProductListQuery($count: Int!, $cursor: String) {
|
|
11
|
+
viewer {
|
|
12
|
+
name
|
|
13
|
+
products(first: $count, after: $cursor) {
|
|
14
|
+
totalCount
|
|
15
|
+
pageInfo {
|
|
16
|
+
hasNextPage
|
|
17
|
+
}
|
|
18
|
+
edges {
|
|
19
|
+
node {
|
|
20
|
+
id
|
|
21
|
+
...ProductCard_product
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Fetches the first page of the product catalog with `useLazyLoadQuery` and
|
|
31
|
+
* renders a `ProductCard` (a `useFragment` child) per edge.
|
|
32
|
+
*
|
|
33
|
+
* The hook suspends while the query is in flight, so render this component
|
|
34
|
+
* inside a `Suspense` boundary — and, until the SSR data-serialization PR
|
|
35
|
+
* lands, only on the client (see `CatalogPage`).
|
|
36
|
+
*/
|
|
37
|
+
export default function ProductList(): ReactElement {
|
|
38
|
+
const data = useLazyLoadQuery<ProductListQuery>(productListQuery, {
|
|
39
|
+
count: PAGE_SIZE,
|
|
40
|
+
});
|
|
41
|
+
const { products } = data.viewer;
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<section aria-label="Product catalog">
|
|
45
|
+
<p>
|
|
46
|
+
Signed in as <strong>{data.viewer.name}</strong> — showing{" "}
|
|
47
|
+
{products.edges.length} of {products.totalCount} products.
|
|
48
|
+
</p>
|
|
49
|
+
<ul>
|
|
50
|
+
{products.edges.map(({ node }) => (
|
|
51
|
+
<li key={node.id}>
|
|
52
|
+
<ProductCard product={node} />
|
|
53
|
+
</li>
|
|
54
|
+
))}
|
|
55
|
+
</ul>
|
|
56
|
+
{products.pageInfo.hasNextPage && <p>More products are available.</p>}
|
|
57
|
+
</section>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { renderToString } from "react-dom/server";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import ClientOnly from "./ClientOnly.js";
|
|
5
|
+
|
|
6
|
+
describe("ClientOnly component", () => {
|
|
7
|
+
it("renders children after mounting in the browser", () => {
|
|
8
|
+
render(
|
|
9
|
+
<ClientOnly fallback={<p>Loading…</p>}>
|
|
10
|
+
<p>Client content</p>
|
|
11
|
+
</ClientOnly>,
|
|
12
|
+
);
|
|
13
|
+
expect(screen.getByText("Client content")).toBeInTheDocument();
|
|
14
|
+
expect(screen.queryByText("Loading…")).not.toBeInTheDocument();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("renders the fallback when server-rendered", () => {
|
|
18
|
+
const html = renderToString(
|
|
19
|
+
<ClientOnly fallback={<p>Loading…</p>}>
|
|
20
|
+
<p>Client content</p>
|
|
21
|
+
</ClientOnly>,
|
|
22
|
+
);
|
|
23
|
+
expect(html).toContain("Loading…");
|
|
24
|
+
expect(html).not.toContain("Client content");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("renders nothing on the server without a fallback", () => {
|
|
28
|
+
const html = renderToString(
|
|
29
|
+
<ClientOnly>
|
|
30
|
+
<p>Client content</p>
|
|
31
|
+
</ClientOnly>,
|
|
32
|
+
);
|
|
33
|
+
expect(html).toBe("");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export interface ClientOnlyProps {
|
|
4
|
+
/** Content rendered only after the component mounts in the browser. */
|
|
5
|
+
readonly children: ReactNode;
|
|
6
|
+
/** Rendered during SSR and the initial hydration pass. Defaults to nothing. */
|
|
7
|
+
readonly fallback?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Defers `children` until after hydration, so they render only in the browser.
|
|
12
|
+
*
|
|
13
|
+
* The server (and React's first client pass, so hydration stays
|
|
14
|
+
* mismatch-free) renders `fallback`; the mount effect then flips to
|
|
15
|
+
* `children`. Use this for content that must not run during server
|
|
16
|
+
* rendering — e.g. components that fetch on render, like the Relay example
|
|
17
|
+
* on the catalog page, until server-side data serialization/hydration lands
|
|
18
|
+
* in a follow-up PR.
|
|
19
|
+
*/
|
|
20
|
+
export default function ClientOnly({
|
|
21
|
+
children,
|
|
22
|
+
fallback = null,
|
|
23
|
+
}: ClientOnlyProps): ReactElement {
|
|
24
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
setIsMounted(true);
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
return <>{isMounted ? children : fallback}</>;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { type ClientOnlyProps, default } from "./ClientOnly.js";
|
|
@@ -9,6 +9,9 @@ export default function Navigation(): ReactElement {
|
|
|
9
9
|
<Link params={{ slug: "router-core" }} to="guide">
|
|
10
10
|
Guide
|
|
11
11
|
</Link>
|
|
12
|
+
<% if (relay) { -%>
|
|
13
|
+
<Link to="catalog">Catalog</Link>
|
|
14
|
+
<% } -%>
|
|
12
15
|
<% if (forms) { -%>
|
|
13
16
|
<Link to="contact">Contact</Link>
|
|
14
17
|
<% } -%>
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
<% if (relay) { -%>
|
|
2
|
+
export { default as ClientOnly } from "./ClientOnly/index.js";
|
|
3
|
+
<% } -%>
|
|
1
4
|
export { ExampleComponent } from "./ExampleComponent/index.js";
|
|
2
5
|
export { default as LazyComponent } from "./LazyComponent/index.js";
|
|
3
6
|
export { default as Navigation } from "./Navigation/index.js";
|
package/dist/esm/application/react/templates/src/relay/__generated__/ProductCard_product.graphql.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @generated SignedSource<<a6fcbf029d2f62fcf6f378581e4985a3>>
|
|
3
|
+
* @lightSyntaxTransform
|
|
4
|
+
* @nogrep
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/* tslint:disable */
|
|
8
|
+
/* eslint-disable */
|
|
9
|
+
// @ts-nocheck
|
|
10
|
+
|
|
11
|
+
import { ReaderFragment } from 'relay-runtime';
|
|
12
|
+
import { FragmentRefs } from "relay-runtime";
|
|
13
|
+
export type ProductCard_product$data = {
|
|
14
|
+
readonly currency: string;
|
|
15
|
+
readonly inStock: boolean;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly priceCents: number;
|
|
18
|
+
readonly rating: number;
|
|
19
|
+
readonly tagline: string;
|
|
20
|
+
readonly " $fragmentType": "ProductCard_product";
|
|
21
|
+
};
|
|
22
|
+
export type ProductCard_product$key = {
|
|
23
|
+
readonly " $data"?: ProductCard_product$data;
|
|
24
|
+
readonly " $fragmentSpreads": FragmentRefs<"ProductCard_product">;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const node: ReaderFragment = {
|
|
28
|
+
"argumentDefinitions": [],
|
|
29
|
+
"kind": "Fragment",
|
|
30
|
+
"metadata": null,
|
|
31
|
+
"name": "ProductCard_product",
|
|
32
|
+
"selections": [
|
|
33
|
+
{
|
|
34
|
+
"alias": null,
|
|
35
|
+
"args": null,
|
|
36
|
+
"kind": "ScalarField",
|
|
37
|
+
"name": "name",
|
|
38
|
+
"storageKey": null
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"alias": null,
|
|
42
|
+
"args": null,
|
|
43
|
+
"kind": "ScalarField",
|
|
44
|
+
"name": "tagline",
|
|
45
|
+
"storageKey": null
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"alias": null,
|
|
49
|
+
"args": null,
|
|
50
|
+
"kind": "ScalarField",
|
|
51
|
+
"name": "priceCents",
|
|
52
|
+
"storageKey": null
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"alias": null,
|
|
56
|
+
"args": null,
|
|
57
|
+
"kind": "ScalarField",
|
|
58
|
+
"name": "currency",
|
|
59
|
+
"storageKey": null
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"alias": null,
|
|
63
|
+
"args": null,
|
|
64
|
+
"kind": "ScalarField",
|
|
65
|
+
"name": "rating",
|
|
66
|
+
"storageKey": null
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"alias": null,
|
|
70
|
+
"args": null,
|
|
71
|
+
"kind": "ScalarField",
|
|
72
|
+
"name": "inStock",
|
|
73
|
+
"storageKey": null
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
"type": "Product",
|
|
77
|
+
"abstractKey": null
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
(node as any).hash = "542f84e3fd8c5cc8222c4d637a9ee99d";
|
|
81
|
+
|
|
82
|
+
export default node;
|