@cedarjs/web 6.0.0-canary.2672 → 6.0.0-canary.2675

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.
@@ -1,225 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var import_react = __toESM(require("react"), 1);
25
- var import_react2 = require("@apollo/client/react");
26
- var import_react3 = require("@testing-library/react");
27
- var import_graphql = require("graphql");
28
- var import_vitest = require("vitest");
29
- var import_fragmentRegistry = require("../../apollo/fragmentRegistry.js");
30
- var import_createCell = require("./createCell.js");
31
- import_vitest.vi.mock("@apollo/client/react", async (importOriginal) => {
32
- const actual = await importOriginal();
33
- return {
34
- ...actual,
35
- useApolloClient: import_vitest.vi.fn(),
36
- useFragment: import_vitest.vi.fn(),
37
- useQuery: import_vitest.vi.fn()
38
- };
39
- });
40
- const mockUseApolloClient = import_react2.useApolloClient;
41
- const mockApolloUseFragment = import_react2.useFragment;
42
- const mockUseQuery = import_react2.useQuery;
43
- const AUTHOR_FRAGMENT = (0, import_graphql.parse)(`
44
- fragment AuthorCell_author on User {
45
- email
46
- fullName
47
- }
48
- `);
49
- (0, import_vitest.describe)("createFragmentCell", () => {
50
- (0, import_vitest.beforeAll)(() => {
51
- globalThis.RWJS_ENV = {
52
- RWJS_EXP_STREAMING_SSR: false
53
- };
54
- });
55
- (0, import_vitest.beforeEach)(() => {
56
- mockApolloUseFragment.mockReset();
57
- mockUseApolloClient.mockReset();
58
- mockUseApolloClient.mockReturnValue({
59
- cache: {
60
- identify: (obj) => obj.__typename && obj.id !== void 0 ? `${obj.__typename}:${obj.id}` : void 0
61
- }
62
- });
63
- mockApolloUseFragment.mockReturnValue({ data: void 0, complete: false });
64
- });
65
- (0, import_vitest.test)("renders Success with data from its data prop", () => {
66
- const TestCell = (0, import_createCell.createCell)({
67
- FRAGMENT: AUTHOR_FRAGMENT,
68
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
69
- });
70
- (0, import_react3.render)(
71
- /* @__PURE__ */ import_react.default.createElement(
72
- TestCell,
73
- {
74
- author: { __typename: "User", id: 1, fullName: "Story Teller" }
75
- }
76
- )
77
- );
78
- import_react3.screen.getByText(/^By Story Teller$/);
79
- });
80
- (0, import_vitest.test)("prefers complete data from the cache", () => {
81
- const TestCell = (0, import_createCell.createCell)({
82
- FRAGMENT: AUTHOR_FRAGMENT,
83
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
84
- });
85
- mockApolloUseFragment.mockImplementation((options) => {
86
- (0, import_vitest.expect)(options.fragmentName).toEqual("AuthorCell_author");
87
- (0, import_vitest.expect)(options.from).toEqual("User:1");
88
- return {
89
- data: { fullName: "Cache Dweller" },
90
- complete: true
91
- };
92
- });
93
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, { author: { __typename: "User", id: 1 } }));
94
- import_react3.screen.getByText(/^By Cache Dweller$/);
95
- });
96
- (0, import_vitest.test)("falls back to the data prop for incomplete cache reads", () => {
97
- const TestCell = (0, import_createCell.createCell)({
98
- FRAGMENT: AUTHOR_FRAGMENT,
99
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
100
- });
101
- mockApolloUseFragment.mockReturnValue({ data: void 0, complete: false });
102
- (0, import_react3.render)(
103
- /* @__PURE__ */ import_react.default.createElement(
104
- TestCell,
105
- {
106
- author: { __typename: "User", id: 1, fullName: "Ref Reader" }
107
- }
108
- )
109
- );
110
- import_react3.screen.getByText(/^By Ref Reader$/);
111
- });
112
- (0, import_vitest.test)("derives the prop name from the fragment type when there is no underscore", () => {
113
- const TestCell = (0, import_createCell.createCell)({
114
- FRAGMENT: (0, import_graphql.parse)(`
115
- fragment AuthorBio on User {
116
- bio
117
- }
118
- `),
119
- Success: ({ user }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, user.bio)
120
- });
121
- (0, import_react3.render)(
122
- /* @__PURE__ */ import_react.default.createElement(TestCell, { user: { __typename: "User", id: 1, bio: "Writes things" } })
123
- );
124
- import_react3.screen.getByText(/^Writes things$/);
125
- });
126
- (0, import_vitest.test)("renders Empty when isEmpty says so", () => {
127
- const TestCell = (0, import_createCell.createCell)({
128
- FRAGMENT: AUTHOR_FRAGMENT,
129
- isEmpty: () => true,
130
- Empty: () => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "Nothing to see"),
131
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
132
- });
133
- (0, import_react3.render)(
134
- /* @__PURE__ */ import_react.default.createElement(TestCell, { author: { __typename: "User", id: 1, fullName: "Nobody" } })
135
- );
136
- import_react3.screen.getByText(/^Nothing to see$/);
137
- });
138
- (0, import_vitest.test)("applies afterQuery to the fragment data", () => {
139
- const TestCell = (0, import_createCell.createCell)({
140
- FRAGMENT: AUTHOR_FRAGMENT,
141
- afterQuery: (data) => ({
142
- author: { ...data.author, fullName: "Changed Name" }
143
- }),
144
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
145
- });
146
- (0, import_react3.render)(
147
- /* @__PURE__ */ import_react.default.createElement(
148
- TestCell,
149
- {
150
- author: { __typename: "User", id: 1, fullName: "Original Name" }
151
- }
152
- )
153
- );
154
- import_react3.screen.getByText(/^By Changed Name$/);
155
- });
156
- (0, import_vitest.test)("renders Empty when the data prop is null", () => {
157
- const TestCell = (0, import_createCell.createCell)({
158
- FRAGMENT: AUTHOR_FRAGMENT,
159
- Empty: () => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "No author"),
160
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
161
- });
162
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, { author: null }));
163
- import_react3.screen.getByText(/^No author$/);
164
- });
165
- (0, import_vitest.test)("renders Success with null data when the data prop is null and there is no Empty", () => {
166
- const TestCell = (0, import_createCell.createCell)({
167
- FRAGMENT: AUTHOR_FRAGMENT,
168
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author?.fullName ?? "unknown")
169
- });
170
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, { author: null }));
171
- import_react3.screen.getByText(/^By unknown$/);
172
- });
173
- (0, import_vitest.test)("throws a helpful error naming the prop when it is not passed", () => {
174
- const TestCell = (0, import_createCell.createCell)({
175
- FRAGMENT: AUTHOR_FRAGMENT,
176
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName),
177
- displayName: "AuthorCell"
178
- });
179
- const consoleErrorSpy = import_vitest.vi.spyOn(console, "error").mockImplementation(() => {
180
- });
181
- try {
182
- (0, import_vitest.expect)(() => {
183
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, null));
184
- }).toThrow(/`author` prop/);
185
- } finally {
186
- consoleErrorSpy.mockRestore();
187
- }
188
- });
189
- (0, import_vitest.test)("stays a query Cell when both QUERY and FRAGMENT are exported", () => {
190
- const TestCell = (0, import_createCell.createCell)({
191
- // @ts-expect-error - Purposefully using a plain string here.
192
- QUERY: "query AuthorQuery { author { fullName } }",
193
- FRAGMENT: AUTHOR_FRAGMENT,
194
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
195
- });
196
- mockUseQuery.mockImplementation(() => {
197
- return { data: { author: { fullName: "Query Result" } } };
198
- });
199
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, null));
200
- import_react3.screen.getByText(/^By Query Result$/);
201
- });
202
- (0, import_vitest.test)("registers the FRAGMENT even when the Cell stays a query Cell", () => {
203
- (0, import_createCell.createCell)({
204
- // @ts-expect-error - Purposefully using a plain string here.
205
- QUERY: "query MixedExportQuery { author { fullName } }",
206
- // Using a unique fragment name since the registry is shared between
207
- // tests
208
- FRAGMENT: (0, import_graphql.parse)(`
209
- fragment MixedExportCell_author on User {
210
- fullName
211
- }
212
- `),
213
- Success: ({ author }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "By ", author.fullName)
214
- });
215
- (0, import_vitest.expect)(import_fragmentRegistry.fragmentRegistry.lookup("MixedExportCell_author")).toBeTruthy();
216
- });
217
- (0, import_vitest.test)("throws when the FRAGMENT export is not a fragment", () => {
218
- (0, import_vitest.expect)(
219
- () => (0, import_createCell.createCell)({
220
- FRAGMENT: (0, import_graphql.parse)("query FindAuthor { author { id } }"),
221
- Success: () => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "hello")
222
- })
223
- ).toThrow(/fragment definition/);
224
- });
225
- });
@@ -1,96 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var import_react = __toESM(require("react"), 1);
25
- var import_dev = require("@apollo/client/dev");
26
- var import_react2 = require("@apollo/client/react");
27
- var import_react3 = require("@testing-library/react");
28
- var import_vitest = require("vitest");
29
- var import_createSuspendingCell = require("./createSuspendingCell.js");
30
- import_vitest.vi.mock("@apollo/client/react", () => {
31
- return {
32
- useApolloClient: import_vitest.vi.fn(),
33
- useBackgroundQuery: import_vitest.vi.fn(),
34
- useReadQuery: import_vitest.vi.fn()
35
- };
36
- });
37
- const mockUseBackgroundQuery = import_react2.useBackgroundQuery;
38
- const mockUseReadQuery = import_react2.useReadQuery;
39
- (0, import_vitest.describe)("createSuspendingCell", () => {
40
- (0, import_vitest.beforeAll)(() => {
41
- globalThis.RWJS_ENV = {
42
- RWJS_EXP_STREAMING_SSR: true
43
- };
44
- (0, import_dev.loadDevMessages)();
45
- (0, import_dev.loadErrorMessages)();
46
- });
47
- (0, import_vitest.beforeEach)(() => {
48
- mockUseReadQuery.mockReset();
49
- mockUseBackgroundQuery.mockReset();
50
- mockUseBackgroundQuery.mockImplementation(() => {
51
- return ["mocked-query-ref", { refetch: import_vitest.vi.fn(), fetchMore: import_vitest.vi.fn() }];
52
- });
53
- });
54
- (0, import_vitest.test)("Renders a static Success component", () => {
55
- const TestCell = (0, import_createSuspendingCell.createSuspendingCell)({
56
- // @ts-expect-error - Purposefully using a plain string here.
57
- QUERY: "query TestQuery { answer }",
58
- Success: () => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "Great success!")
59
- });
60
- mockUseReadQuery.mockImplementation(() => ({ data: {} }));
61
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, null));
62
- import_react3.screen.getByText(/^Great success!$/);
63
- });
64
- (0, import_vitest.test)("Renders Success with data", () => {
65
- const TestCell = (0, import_createSuspendingCell.createSuspendingCell)({
66
- // @ts-expect-error - Purposefully using a plain string here.
67
- QUERY: "query TestQuery { answer }",
68
- Success: ({ answer }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("dl", null, /* @__PURE__ */ import_react.default.createElement("dt", null, "What's the meaning of life?"), /* @__PURE__ */ import_react.default.createElement("dd", null, answer)))
69
- });
70
- mockUseReadQuery.mockImplementation(() => {
71
- return { data: { answer: 42 } };
72
- });
73
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, null));
74
- import_react3.screen.getByText(/^What's the meaning of life\?$/);
75
- import_react3.screen.getByText(/^42$/);
76
- });
77
- (0, import_vitest.test)("Renders Success if any of the fields have data (i.e. not just the first)", () => {
78
- const TestCell = (0, import_createSuspendingCell.createSuspendingCell)({
79
- // @ts-expect-error - Purposefully using a plain string here.
80
- QUERY: "query TestQuery { users { name } posts { title } }",
81
- Empty: () => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, "No users or posts"),
82
- Success: ({ users, posts }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", null, users.length > 0 ? /* @__PURE__ */ import_react.default.createElement("ul", null, users.map(({ name }) => /* @__PURE__ */ import_react.default.createElement("li", { key: name }, name))) : "no users"), /* @__PURE__ */ import_react.default.createElement("div", null, posts.length > 0 ? /* @__PURE__ */ import_react.default.createElement("ul", null, posts.map(({ title }) => /* @__PURE__ */ import_react.default.createElement("li", { key: title }, title))) : "no posts"))
83
- });
84
- mockUseReadQuery.mockImplementation(() => {
85
- return {
86
- data: {
87
- users: [],
88
- posts: [{ title: "bazinga" }, { title: "kittens" }]
89
- }
90
- };
91
- });
92
- (0, import_react3.render)(/* @__PURE__ */ import_react.default.createElement(TestCell, null));
93
- import_react3.screen.getByText(/bazinga/);
94
- import_react3.screen.getByText(/kittens/);
95
- });
96
- });
@@ -1,314 +0,0 @@
1
- import React from "react";
2
- import { useQuery } from "@apollo/client/react";
3
- import { render, screen } from "@testing-library/react";
4
- import { vi, describe, beforeAll, beforeEach, test, expect } from "vitest";
5
- import { createCell } from "./createCell.js";
6
- vi.mock("@apollo/client/react", () => ({
7
- useQuery: vi.fn()
8
- }));
9
- const mockUseQuery = useQuery;
10
- describe("createCell", () => {
11
- beforeAll(() => {
12
- globalThis.RWJS_ENV = {
13
- RWJS_EXP_STREAMING_SSR: false
14
- };
15
- });
16
- beforeEach(() => {
17
- mockUseQuery.mockReset();
18
- });
19
- test("Renders a static Success component", () => {
20
- const TestCell = createCell({
21
- // @ts-expect-error - Purposefully using a plain string here.
22
- QUERY: "query TestQuery { answer }",
23
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!")
24
- });
25
- mockUseQuery.mockImplementation(() => ({ data: {} }));
26
- render(/* @__PURE__ */ React.createElement(TestCell, null));
27
- screen.getByText(/^Great success!$/);
28
- });
29
- test("Renders Success with data", () => {
30
- const TestCell = createCell({
31
- // @ts-expect-error - Purposefully using a plain string here.
32
- QUERY: "query TestQuery { answer }",
33
- Success: ({ answer }) => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("dl", null, /* @__PURE__ */ React.createElement("dt", null, "What's the meaning of life?"), /* @__PURE__ */ React.createElement("dd", null, answer)))
34
- });
35
- mockUseQuery.mockImplementation(() => {
36
- return { data: { answer: 42 } };
37
- });
38
- render(/* @__PURE__ */ React.createElement(TestCell, null));
39
- screen.getByText(/^What's the meaning of life\?$/);
40
- screen.getByText(/^42$/);
41
- });
42
- test("Renders Success if any of the fields have data (i.e. not just the first)", () => {
43
- const TestCell = createCell({
44
- // @ts-expect-error - Purposefully using a plain string here.
45
- QUERY: "query TestQuery { users { name } posts { title } }",
46
- Empty: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "No users or posts"),
47
- Success: ({ users, posts }) => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", null, users.length > 0 ? /* @__PURE__ */ React.createElement("ul", null, users.map(({ name }) => /* @__PURE__ */ React.createElement("li", { key: name }, name))) : "no users"), /* @__PURE__ */ React.createElement("div", null, posts.length > 0 ? /* @__PURE__ */ React.createElement("ul", null, posts.map(({ title }) => /* @__PURE__ */ React.createElement("li", { key: title }, title))) : "no posts"))
48
- });
49
- mockUseQuery.mockImplementation(() => {
50
- return {
51
- data: {
52
- users: [],
53
- posts: [{ title: "bazinga" }, { title: "kittens" }]
54
- }
55
- };
56
- });
57
- render(/* @__PURE__ */ React.createElement(TestCell, null));
58
- screen.getByText(/bazinga/);
59
- screen.getByText(/kittens/);
60
- });
61
- test("Renders default Loading when there is no data", () => {
62
- const TestCell = createCell({
63
- // @ts-expect-error - Purposefully using a plain string here.
64
- QUERY: "query TestQuery { answer }",
65
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!")
66
- });
67
- mockUseQuery.mockImplementation(() => ({ loading: true }));
68
- render(/* @__PURE__ */ React.createElement(TestCell, null));
69
- screen.getByText(/^Loading...$/);
70
- });
71
- test("Renders custom Loading when there is no data", () => {
72
- const TestCell = createCell({
73
- // @ts-expect-error - Purposefully using a plain string here.
74
- QUERY: "query TestQuery { answer }",
75
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
76
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
77
- });
78
- mockUseQuery.mockImplementation(() => ({ loading: true }));
79
- render(/* @__PURE__ */ React.createElement(TestCell, null));
80
- screen.getByText(/^Fetching answer...$/);
81
- });
82
- test("Renders Success even when `loading` is true if there is data", () => {
83
- const TestCell = createCell({
84
- // @ts-expect-error - Purposefully using a plain string here.
85
- QUERY: "query TestQuery { answer }",
86
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
87
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
88
- });
89
- mockUseQuery.mockImplementation(() => ({ loading: true, data: {} }));
90
- render(/* @__PURE__ */ React.createElement(TestCell, null));
91
- screen.getByText(/^Great success!$/);
92
- });
93
- test("Renders Empty if available, and data field is null", () => {
94
- const TestCell = createCell({
95
- // @ts-expect-error - Purposefully using a plain string here.
96
- QUERY: "query TestQuery { answer }",
97
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
98
- Empty: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "No one knows")
99
- });
100
- mockUseQuery.mockImplementation(() => ({
101
- loading: true,
102
- data: { answer: null }
103
- }));
104
- render(/* @__PURE__ */ React.createElement(TestCell, null));
105
- screen.getByText(/^No one knows$/);
106
- });
107
- test("Renders Empty if available, and data field is an empty array", () => {
108
- const TestCell = createCell({
109
- // @ts-expect-error - Purposefully using a plain string here.
110
- QUERY: "query TestQuery { answers }",
111
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
112
- Empty: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "No one knows")
113
- });
114
- mockUseQuery.mockImplementation(() => ({
115
- loading: true,
116
- data: { answers: [] }
117
- }));
118
- render(/* @__PURE__ */ React.createElement(TestCell, null));
119
- screen.getByText(/^No one knows$/);
120
- });
121
- test("Renders Success even if data is empty when no Empty is available", () => {
122
- const TestCell = createCell({
123
- // @ts-expect-error - Purposefully using a plain string here.
124
- QUERY: "query TestQuery { answer }",
125
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Empty success")
126
- });
127
- mockUseQuery.mockImplementation(() => ({
128
- loading: true,
129
- data: { answer: null }
130
- }));
131
- render(/* @__PURE__ */ React.createElement(TestCell, null));
132
- screen.getByText(/^Empty success$/);
133
- });
134
- test("Allows passing children to Success", () => {
135
- const TestCell = createCell({
136
- // @ts-expect-error - Purposefully using a plain string here.
137
- QUERY: "query TestQuery { answer }",
138
- Success: ({ children }) => /* @__PURE__ */ React.createElement(React.Fragment, null, "Look at my beautiful ", children)
139
- });
140
- mockUseQuery.mockImplementation(() => ({ data: {} }));
141
- render(
142
- /* @__PURE__ */ React.createElement(TestCell, null, /* @__PURE__ */ React.createElement("div", null, "\u{1F986}"))
143
- );
144
- screen.getByText(/^Look at my beautiful$/);
145
- screen.getByText(/^🦆$/);
146
- });
147
- test("Cell props are passed to the query as variables", () => {
148
- const TestCell = createCell({
149
- // @ts-expect-error - Purposefully using a plain string here.
150
- QUERY: `query Greet($name: String!) {
151
- greet(name: $name) {
152
- greeting
153
- }
154
- }`,
155
- Success: ({ greeting }) => /* @__PURE__ */ React.createElement("p", null, greeting)
156
- });
157
- mockUseQuery.mockImplementation((_query, options) => {
158
- return { data: { greeting: `Hello ${options.variables.name}!` } };
159
- });
160
- render(/* @__PURE__ */ React.createElement(TestCell, { name: "Bob" }));
161
- screen.getByText(/^Hello Bob!$/);
162
- });
163
- test("Allows QUERY to be a function", () => {
164
- const TestCell = createCell({
165
- // @ts-expect-error - Purposefully using a plain string here.
166
- QUERY: ({ variables }) => {
167
- if (variables.character === "BEAST") {
168
- return "query BeastQuery { name }";
169
- }
170
- return "query HeroQuery { name }";
171
- },
172
- Success: ({ name }) => /* @__PURE__ */ React.createElement("p", null, "Call me ", name)
173
- });
174
- mockUseQuery.mockImplementation((query) => {
175
- if (query.includes("BeastQuery")) {
176
- return { data: { name: "Boogeyman" } };
177
- } else if (query.includes("HeroQuery")) {
178
- return { data: { name: "Lara Croft" } };
179
- }
180
- return { data: { name: "John Doe" } };
181
- });
182
- render(
183
- /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(TestCell, { character: "BEAST" }), /* @__PURE__ */ React.createElement(TestCell, { character: "HERO" }))
184
- );
185
- screen.getByText(/^Call me Boogeyman$/);
186
- screen.getByText(/^Call me Lara Croft$/);
187
- });
188
- test("Renders Failure when there is an error", () => {
189
- const TestCell = createCell({
190
- // @ts-expect-error - Purposefully using a plain string here.
191
- QUERY: "query TestQuery { answer }",
192
- Failure: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Sad face :("),
193
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
194
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
195
- });
196
- mockUseQuery.mockImplementation(() => ({ error: true }));
197
- render(/* @__PURE__ */ React.createElement(TestCell, null));
198
- screen.getByText(/^Sad face :\($/);
199
- });
200
- test("Passes error to Failure component", () => {
201
- const TestCell = createCell({
202
- // @ts-expect-error - Purposefully using a plain string here.
203
- QUERY: "query TestQuery { answer }",
204
- Failure: ({ error }) => /* @__PURE__ */ React.createElement(React.Fragment, null, JSON.stringify(error)),
205
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
206
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
207
- });
208
- mockUseQuery.mockImplementation(() => ({
209
- error: { msg: "System malfunction" }
210
- }));
211
- render(/* @__PURE__ */ React.createElement(TestCell, null));
212
- screen.getByText(/^{"msg":"System malfunction"}$/);
213
- });
214
- test("Passes error and errorCode to Failure component", () => {
215
- const TestCell = createCell({
216
- // @ts-expect-error - Purposefully using a plain string here.
217
- QUERY: "query TestQuery { answer }",
218
- Failure: ({ error, errorCode }) => /* @__PURE__ */ React.createElement(React.Fragment, null, JSON.stringify(error), ",code:", errorCode),
219
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
220
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
221
- });
222
- mockUseQuery.mockImplementation(() => ({
223
- error: { msg: "System malfunction" },
224
- errorCode: "SIMON_SAYS_NO"
225
- }));
226
- render(/* @__PURE__ */ React.createElement(TestCell, null));
227
- screen.getByText(/^{"msg":"System malfunction"},code:SIMON_SAYS_NO$/);
228
- });
229
- test("Passes children to Failure", () => {
230
- const TestCell = createCell({
231
- // @ts-expect-error - Purposefully using a plain string here.
232
- QUERY: "query TestQuery { answer }",
233
- Failure: ({ children }) => /* @__PURE__ */ React.createElement(React.Fragment, null, "I'm a failure ", children)
234
- });
235
- mockUseQuery.mockImplementation(() => ({ error: {} }));
236
- render(
237
- /* @__PURE__ */ React.createElement(TestCell, null, /* @__PURE__ */ React.createElement("div", null, "Child"))
238
- );
239
- screen.getByText(/^I'm a failure$/);
240
- screen.getByText(/^Child$/);
241
- });
242
- test("Throws an error when there is an error if no Failure component exists", () => {
243
- const TestCell = createCell({
244
- // @ts-expect-error - Purposefully using a plain string here.
245
- QUERY: "query TestQuery { answer }",
246
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
247
- Loading: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Fetching answer...")
248
- });
249
- mockUseQuery.mockImplementation(() => ({
250
- error: { message: "200 GraphQL" }
251
- }));
252
- const err = console.error;
253
- console.error = vi.fn();
254
- let error;
255
- try {
256
- render(/* @__PURE__ */ React.createElement(TestCell, null));
257
- } catch (e) {
258
- error = e;
259
- }
260
- expect(error.message).toEqual("200 GraphQL");
261
- console.error = err;
262
- });
263
- test("Allows overriding of default isDataEmpty", () => {
264
- const TestCell = createCell({
265
- // @ts-expect-error - Purposefully using a plain string here.
266
- QUERY: "query TestQuery { answer }",
267
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
268
- Empty: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Got nothing"),
269
- isEmpty: () => true
270
- });
271
- mockUseQuery.mockImplementation(() => ({
272
- data: {},
273
- loading: false
274
- }));
275
- render(/* @__PURE__ */ React.createElement(TestCell, null));
276
- screen.getByText(/^Got nothing$/);
277
- });
278
- test("Allows mixing isDataEmpty with custom logic", () => {
279
- const TestCell = createCell({
280
- // @ts-expect-error - Purposefully using a plain string here.
281
- QUERY: "query TestQuery { answer }",
282
- Success: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Great success!"),
283
- Empty: () => /* @__PURE__ */ React.createElement(React.Fragment, null, "Got nothing"),
284
- isEmpty: (data, { isDataEmpty }) => isDataEmpty(data) || data.answer === "0"
285
- });
286
- mockUseQuery.mockImplementation(() => ({
287
- data: { answer: "0" },
288
- loading: false
289
- }));
290
- render(/* @__PURE__ */ React.createElement(TestCell, null));
291
- screen.getByText(/^Got nothing$/);
292
- });
293
- test("Allows overriding variables in beforeQuery", () => {
294
- const TestCell = createCell({
295
- // @ts-expect-error - Purposefully using a plain string here.
296
- QUERY: `query Greet($name: String!) {
297
- greet(name: $name) {
298
- greeting
299
- }
300
- }`,
301
- Success: ({ greeting }) => /* @__PURE__ */ React.createElement("p", null, greeting),
302
- beforeQuery: () => ({
303
- variables: {
304
- name: "Bob"
305
- }
306
- })
307
- });
308
- mockUseQuery.mockImplementation((_query, options) => {
309
- return { data: { greeting: `Hello ${options.variables.name}!` } };
310
- });
311
- render(/* @__PURE__ */ React.createElement(TestCell, null));
312
- screen.getByText(/^Hello Bob!$/);
313
- });
314
- });