@powerhousedao/builder-tools 6.0.0-dev.6 → 6.0.0-dev.60
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/dist/connect-utils/helpers.d.ts +1 -1
- package/dist/connect-utils/helpers.d.ts.map +1 -1
- package/dist/connect-utils/helpers.js +2 -2
- package/dist/connect-utils/helpers.js.map +1 -1
- package/dist/connect-utils/index.d.ts +0 -1
- package/dist/connect-utils/index.d.ts.map +1 -1
- package/dist/connect-utils/index.js +0 -1
- package/dist/connect-utils/index.js.map +1 -1
- package/dist/connect-utils/types.d.ts +0 -1
- package/dist/connect-utils/types.d.ts.map +1 -1
- package/dist/connect-utils/vite-config.d.ts.map +1 -1
- package/dist/connect-utils/vite-config.js +19 -4
- package/dist/connect-utils/vite-config.js.map +1 -1
- package/dist/editor-utils/index.d.ts +0 -2
- package/dist/editor-utils/index.d.ts.map +1 -1
- package/dist/editor-utils/index.js +0 -2
- package/dist/editor-utils/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -108
- package/dist/browser.d.ts +0 -2
- package/dist/browser.d.ts.map +0 -1
- package/dist/browser.js +0 -2
- package/dist/browser.js.map +0 -1
- package/dist/connect-utils/env-config.d.ts +0 -844
- package/dist/connect-utils/env-config.d.ts.map +0 -1
- package/dist/connect-utils/env-config.js +0 -518
- package/dist/connect-utils/env-config.js.map +0 -1
- package/dist/document-model-editor/components/button.d.ts +0 -2
- package/dist/document-model-editor/components/button.d.ts.map +0 -1
- package/dist/document-model-editor/components/button.js +0 -8
- package/dist/document-model-editor/components/button.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/constants.d.ts +0 -3
- package/dist/document-model-editor/components/code-editors/constants.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/constants.js +0 -38
- package/dist/document-model-editor/components/code-editors/constants.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/factories.d.ts +0 -4
- package/dist/document-model-editor/components/code-editors/factories.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/factories.js +0 -47
- package/dist/document-model-editor/components/code-editors/factories.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/graphql-editor.d.ts +0 -10
- package/dist/document-model-editor/components/code-editors/graphql-editor.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/graphql-editor.js +0 -73
- package/dist/document-model-editor/components/code-editors/graphql-editor.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/hooks.d.ts +0 -23
- package/dist/document-model-editor/components/code-editors/hooks.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/hooks.js +0 -59
- package/dist/document-model-editor/components/code-editors/hooks.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/json-editor.d.ts +0 -8
- package/dist/document-model-editor/components/code-editors/json-editor.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/json-editor.js +0 -47
- package/dist/document-model-editor/components/code-editors/json-editor.js.map +0 -1
- package/dist/document-model-editor/components/code-editors/linting.d.ts +0 -6
- package/dist/document-model-editor/components/code-editors/linting.d.ts.map +0 -1
- package/dist/document-model-editor/components/code-editors/linting.js +0 -59
- package/dist/document-model-editor/components/code-editors/linting.js.map +0 -1
- package/dist/document-model-editor/components/divider.d.ts +0 -8
- package/dist/document-model-editor/components/divider.d.ts.map +0 -1
- package/dist/document-model-editor/components/divider.js +0 -22
- package/dist/document-model-editor/components/divider.js.map +0 -1
- package/dist/document-model-editor/components/errors.d.ts +0 -6
- package/dist/document-model-editor/components/errors.d.ts.map +0 -1
- package/dist/document-model-editor/components/errors.js +0 -5
- package/dist/document-model-editor/components/errors.js.map +0 -1
- package/dist/document-model-editor/components/form.d.ts +0 -12
- package/dist/document-model-editor/components/form.d.ts.map +0 -1
- package/dist/document-model-editor/components/form.js +0 -43
- package/dist/document-model-editor/components/form.js.map +0 -1
- package/dist/document-model-editor/components/input.d.ts +0 -6
- package/dist/document-model-editor/components/input.d.ts.map +0 -1
- package/dist/document-model-editor/components/input.js +0 -9
- package/dist/document-model-editor/components/input.js.map +0 -1
- package/dist/document-model-editor/components/label.d.ts +0 -6
- package/dist/document-model-editor/components/label.d.ts.map +0 -1
- package/dist/document-model-editor/components/label.js +0 -10
- package/dist/document-model-editor/components/label.js.map +0 -1
- package/dist/document-model-editor/components/model-metadata-form.d.ts +0 -31
- package/dist/document-model-editor/components/model-metadata-form.d.ts.map +0 -1
- package/dist/document-model-editor/components/model-metadata-form.js +0 -70
- package/dist/document-model-editor/components/model-metadata-form.js.map +0 -1
- package/dist/document-model-editor/components/module-form.d.ts +0 -10
- package/dist/document-model-editor/components/module-form.d.ts.map +0 -1
- package/dist/document-model-editor/components/module-form.js +0 -22
- package/dist/document-model-editor/components/module-form.js.map +0 -1
- package/dist/document-model-editor/components/module.d.ts +0 -22
- package/dist/document-model-editor/components/module.d.ts.map +0 -1
- package/dist/document-model-editor/components/module.js +0 -10
- package/dist/document-model-editor/components/module.js.map +0 -1
- package/dist/document-model-editor/components/modules.d.ts +0 -20
- package/dist/document-model-editor/components/modules.d.ts.map +0 -1
- package/dist/document-model-editor/components/modules.js +0 -17
- package/dist/document-model-editor/components/modules.js.map +0 -1
- package/dist/document-model-editor/components/operation-description-form.d.ts +0 -9
- package/dist/document-model-editor/components/operation-description-form.d.ts.map +0 -1
- package/dist/document-model-editor/components/operation-description-form.js +0 -13
- package/dist/document-model-editor/components/operation-description-form.js.map +0 -1
- package/dist/document-model-editor/components/operation-error-form.d.ts +0 -13
- package/dist/document-model-editor/components/operation-error-form.d.ts.map +0 -1
- package/dist/document-model-editor/components/operation-error-form.js +0 -40
- package/dist/document-model-editor/components/operation-error-form.js.map +0 -1
- package/dist/document-model-editor/components/operation-errors.d.ts +0 -10
- package/dist/document-model-editor/components/operation-errors.d.ts.map +0 -1
- package/dist/document-model-editor/components/operation-errors.js +0 -17
- package/dist/document-model-editor/components/operation-errors.js.map +0 -1
- package/dist/document-model-editor/components/operation-form.d.ts +0 -13
- package/dist/document-model-editor/components/operation-form.d.ts.map +0 -1
- package/dist/document-model-editor/components/operation-form.js +0 -37
- package/dist/document-model-editor/components/operation-form.js.map +0 -1
- package/dist/document-model-editor/components/operation.d.ts +0 -23
- package/dist/document-model-editor/components/operation.d.ts.map +0 -1
- package/dist/document-model-editor/components/operation.js +0 -25
- package/dist/document-model-editor/components/operation.js.map +0 -1
- package/dist/document-model-editor/components/operations.d.ts +0 -18
- package/dist/document-model-editor/components/operations.d.ts.map +0 -1
- package/dist/document-model-editor/components/operations.js +0 -20
- package/dist/document-model-editor/components/operations.js.map +0 -1
- package/dist/document-model-editor/components/state-error.d.ts +0 -5
- package/dist/document-model-editor/components/state-error.d.ts.map +0 -1
- package/dist/document-model-editor/components/state-error.js +0 -21
- package/dist/document-model-editor/components/state-error.js.map +0 -1
- package/dist/document-model-editor/components/state-schemas.d.ts +0 -15
- package/dist/document-model-editor/components/state-schemas.d.ts.map +0 -1
- package/dist/document-model-editor/components/state-schemas.js +0 -81
- package/dist/document-model-editor/components/state-schemas.js.map +0 -1
- package/dist/document-model-editor/components/tabs.d.ts +0 -8
- package/dist/document-model-editor/components/tabs.d.ts.map +0 -1
- package/dist/document-model-editor/components/tabs.js +0 -13
- package/dist/document-model-editor/components/tabs.js.map +0 -1
- package/dist/document-model-editor/components/text-area.d.ts +0 -9
- package/dist/document-model-editor/components/text-area.d.ts.map +0 -1
- package/dist/document-model-editor/components/text-area.js +0 -27
- package/dist/document-model-editor/components/text-area.js.map +0 -1
- package/dist/document-model-editor/components/text-field.d.ts +0 -21
- package/dist/document-model-editor/components/text-field.d.ts.map +0 -1
- package/dist/document-model-editor/components/text-field.js +0 -80
- package/dist/document-model-editor/components/text-field.js.map +0 -1
- package/dist/document-model-editor/config.d.ts +0 -3
- package/dist/document-model-editor/config.d.ts.map +0 -1
- package/dist/document-model-editor/config.js +0 -4
- package/dist/document-model-editor/config.js.map +0 -1
- package/dist/document-model-editor/constants/documents.d.ts +0 -13
- package/dist/document-model-editor/constants/documents.d.ts.map +0 -1
- package/dist/document-model-editor/constants/documents.js +0 -18
- package/dist/document-model-editor/constants/documents.js.map +0 -1
- package/dist/document-model-editor/constants/graphql-kinds.d.ts +0 -9
- package/dist/document-model-editor/constants/graphql-kinds.d.ts.map +0 -1
- package/dist/document-model-editor/constants/graphql-kinds.js +0 -13
- package/dist/document-model-editor/constants/graphql-kinds.js.map +0 -1
- package/dist/document-model-editor/context/form-context.d.ts +0 -11
- package/dist/document-model-editor/context/form-context.d.ts.map +0 -1
- package/dist/document-model-editor/context/form-context.js +0 -4
- package/dist/document-model-editor/context/form-context.js.map +0 -1
- package/dist/document-model-editor/context/schema-context.d.ts +0 -18
- package/dist/document-model-editor/context/schema-context.d.ts.map +0 -1
- package/dist/document-model-editor/context/schema-context.js +0 -129
- package/dist/document-model-editor/context/schema-context.js.map +0 -1
- package/dist/document-model-editor/document-model-editor.stories.d.ts +0 -8
- package/dist/document-model-editor/document-model-editor.stories.d.ts.map +0 -1
- package/dist/document-model-editor/document-model-editor.stories.js +0 -267
- package/dist/document-model-editor/document-model-editor.stories.js.map +0 -1
- package/dist/document-model-editor/editor.d.ts +0 -2
- package/dist/document-model-editor/editor.d.ts.map +0 -1
- package/dist/document-model-editor/editor.js +0 -217
- package/dist/document-model-editor/editor.js.map +0 -1
- package/dist/document-model-editor/hooks/index.d.ts +0 -3
- package/dist/document-model-editor/hooks/index.d.ts.map +0 -1
- package/dist/document-model-editor/hooks/index.js +0 -3
- package/dist/document-model-editor/hooks/index.js.map +0 -1
- package/dist/document-model-editor/hooks/useDocumentModelDocument.d.ts +0 -5
- package/dist/document-model-editor/hooks/useDocumentModelDocument.d.ts.map +0 -1
- package/dist/document-model-editor/hooks/useDocumentModelDocument.js +0 -8
- package/dist/document-model-editor/hooks/useDocumentModelDocument.js.map +0 -1
- package/dist/document-model-editor/hooks/useFormField.d.ts +0 -13
- package/dist/document-model-editor/hooks/useFormField.d.ts.map +0 -1
- package/dist/document-model-editor/hooks/useFormField.js +0 -22
- package/dist/document-model-editor/hooks/useFormField.js.map +0 -1
- package/dist/document-model-editor/index.d.ts +0 -2
- package/dist/document-model-editor/index.d.ts.map +0 -1
- package/dist/document-model-editor/index.js +0 -2
- package/dist/document-model-editor/index.js.map +0 -1
- package/dist/document-model-editor/module.d.ts +0 -3
- package/dist/document-model-editor/module.d.ts.map +0 -1
- package/dist/document-model-editor/module.js +0 -10
- package/dist/document-model-editor/module.js.map +0 -1
- package/dist/document-model-editor/schemas/inputs.d.ts +0 -25
- package/dist/document-model-editor/schemas/inputs.d.ts.map +0 -1
- package/dist/document-model-editor/schemas/inputs.js +0 -71
- package/dist/document-model-editor/schemas/inputs.js.map +0 -1
- package/dist/document-model-editor/schemas/utils.d.ts +0 -3
- package/dist/document-model-editor/schemas/utils.d.ts.map +0 -1
- package/dist/document-model-editor/schemas/utils.js +0 -3
- package/dist/document-model-editor/schemas/utils.js.map +0 -1
- package/dist/document-model-editor/types/documents.d.ts +0 -26
- package/dist/document-model-editor/types/documents.d.ts.map +0 -1
- package/dist/document-model-editor/types/documents.js +0 -2
- package/dist/document-model-editor/types/documents.js.map +0 -1
- package/dist/document-model-editor/types.d.ts +0 -2
- package/dist/document-model-editor/types.d.ts.map +0 -1
- package/dist/document-model-editor/types.js +0 -2
- package/dist/document-model-editor/types.js.map +0 -1
- package/dist/document-model-editor/utils/helpers.d.ts +0 -112
- package/dist/document-model-editor/utils/helpers.d.ts.map +0 -1
- package/dist/document-model-editor/utils/helpers.js +0 -920
- package/dist/document-model-editor/utils/helpers.js.map +0 -1
- package/dist/document-model-editor/utils/linting.d.ts +0 -7
- package/dist/document-model-editor/utils/linting.d.ts.map +0 -1
- package/dist/document-model-editor/utils/linting.js +0 -49
- package/dist/document-model-editor/utils/linting.js.map +0 -1
- package/dist/document-model-editor/utils/style.d.ts +0 -3
- package/dist/document-model-editor/utils/style.d.ts.map +0 -1
- package/dist/document-model-editor/utils/style.js +0 -6
- package/dist/document-model-editor/utils/style.js.map +0 -1
- package/dist/editor-utils/hooks/index.d.ts +0 -2
- package/dist/editor-utils/hooks/index.d.ts.map +0 -1
- package/dist/editor-utils/hooks/index.js +0 -2
- package/dist/editor-utils/hooks/index.js.map +0 -1
- package/dist/editor-utils/hooks/useEnsInfo.d.ts +0 -15
- package/dist/editor-utils/hooks/useEnsInfo.d.ts.map +0 -1
- package/dist/editor-utils/hooks/useEnsInfo.js +0 -64
- package/dist/editor-utils/hooks/useEnsInfo.js.map +0 -1
- package/dist/editor-utils/storybook.d.ts +0 -32
- package/dist/editor-utils/storybook.d.ts.map +0 -1
- package/dist/editor-utils/storybook.js +0 -104
- package/dist/editor-utils/storybook.js.map +0 -1
- package/dist/style.css +0 -890
- package/dist/test/helpers.test.d.ts +0 -2
- package/dist/test/helpers.test.d.ts.map +0 -1
- package/dist/test/helpers.test.js +0 -805
- package/dist/test/helpers.test.js.map +0 -1
- package/dist/test/index.d.ts +0 -2
- package/dist/test/index.d.ts.map +0 -1
- package/dist/test/index.js +0 -2
- package/dist/test/index.js.map +0 -1
|
@@ -1,805 +0,0 @@
|
|
|
1
|
-
import { Kind, parse } from "graphql";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
import { describe, it } from "node:test";
|
|
4
|
-
import { makeMinimalObjectForStateType, makeStateSchemaNameForScope, StateValidationError, validateStateObject, } from "../document-model-editor/utils/helpers.js";
|
|
5
|
-
function getStateTypeNode(sdl, typeName) {
|
|
6
|
-
const doc = parse(sdl);
|
|
7
|
-
const typeNode = doc.definitions.find((def) => def.kind === Kind.OBJECT_TYPE_DEFINITION &&
|
|
8
|
-
def.name.value === typeName);
|
|
9
|
-
if (!typeNode) {
|
|
10
|
-
throw new Error(`Type ${typeName} not found in schema`);
|
|
11
|
-
}
|
|
12
|
-
return typeNode;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Helper to simulate the component's flow of finding the state type
|
|
16
|
-
* from a shared schema (like in state-schemas.tsx lines 83-94)
|
|
17
|
-
*/
|
|
18
|
-
function getStateTypeFromSharedSchema(sharedSchemaDoc, modelName, scope) {
|
|
19
|
-
const stateTypeName = makeStateSchemaNameForScope(modelName, scope);
|
|
20
|
-
const stateTypeDefinitionNode = sharedSchemaDoc.definitions.find((def) => def.kind === Kind.OBJECT_TYPE_DEFINITION &&
|
|
21
|
-
def.name.value === stateTypeName);
|
|
22
|
-
if (!stateTypeDefinitionNode ||
|
|
23
|
-
stateTypeDefinitionNode.kind !== Kind.OBJECT_TYPE_DEFINITION) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
return stateTypeDefinitionNode;
|
|
27
|
-
}
|
|
28
|
-
describe("makeMinimalObjectForStateType - filling missing fields", () => {
|
|
29
|
-
it("should preserve existing valid values", () => {
|
|
30
|
-
const sdl = `
|
|
31
|
-
type TestState {
|
|
32
|
-
name: String
|
|
33
|
-
}
|
|
34
|
-
`;
|
|
35
|
-
const doc = parse(sdl);
|
|
36
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
37
|
-
const existingValue = JSON.stringify({ name: "test" });
|
|
38
|
-
const result = makeMinimalObjectForStateType({
|
|
39
|
-
sharedSchemaDocumentNode: doc,
|
|
40
|
-
stateTypeDefinitionNode: typeNode,
|
|
41
|
-
existingValue,
|
|
42
|
-
});
|
|
43
|
-
assert.strictEqual(result, JSON.stringify({ name: "test" }, null, 2));
|
|
44
|
-
});
|
|
45
|
-
it("should fill missing optional field with null", () => {
|
|
46
|
-
const sdl = `
|
|
47
|
-
type TestState {
|
|
48
|
-
name: String
|
|
49
|
-
age: Int
|
|
50
|
-
}
|
|
51
|
-
`;
|
|
52
|
-
const doc = parse(sdl);
|
|
53
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
54
|
-
const existingValue = JSON.stringify({ name: "test" });
|
|
55
|
-
const result = makeMinimalObjectForStateType({
|
|
56
|
-
sharedSchemaDocumentNode: doc,
|
|
57
|
-
stateTypeDefinitionNode: typeNode,
|
|
58
|
-
existingValue,
|
|
59
|
-
});
|
|
60
|
-
assert.strictEqual(result, JSON.stringify({ name: "test", age: null }, null, 2));
|
|
61
|
-
});
|
|
62
|
-
it("should fill missing required list field with empty array", () => {
|
|
63
|
-
const sdl = `
|
|
64
|
-
type TestState {
|
|
65
|
-
items: [Item!]!
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type Item {
|
|
69
|
-
id: ID!
|
|
70
|
-
}
|
|
71
|
-
`;
|
|
72
|
-
const doc = parse(sdl);
|
|
73
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
74
|
-
const existingValue = JSON.stringify({});
|
|
75
|
-
const result = makeMinimalObjectForStateType({
|
|
76
|
-
sharedSchemaDocumentNode: doc,
|
|
77
|
-
stateTypeDefinitionNode: typeNode,
|
|
78
|
-
existingValue,
|
|
79
|
-
});
|
|
80
|
-
assert.strictEqual(result, JSON.stringify({ items: [] }, null, 2));
|
|
81
|
-
const errors = validateStateObject(doc, typeNode, result);
|
|
82
|
-
assert.strictEqual(errors.length, 0);
|
|
83
|
-
});
|
|
84
|
-
it("should fill missing optional list field with null", () => {
|
|
85
|
-
const sdl = `
|
|
86
|
-
type TestState {
|
|
87
|
-
items: [String]
|
|
88
|
-
}
|
|
89
|
-
`;
|
|
90
|
-
const doc = parse(sdl);
|
|
91
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
92
|
-
const existingValue = JSON.stringify({});
|
|
93
|
-
const result = makeMinimalObjectForStateType({
|
|
94
|
-
sharedSchemaDocumentNode: doc,
|
|
95
|
-
stateTypeDefinitionNode: typeNode,
|
|
96
|
-
existingValue,
|
|
97
|
-
});
|
|
98
|
-
assert.strictEqual(result, JSON.stringify({ items: null }, null, 2));
|
|
99
|
-
});
|
|
100
|
-
it("should preserve existing array values", () => {
|
|
101
|
-
const sdl = `
|
|
102
|
-
type TestState {
|
|
103
|
-
items: [String!]!
|
|
104
|
-
}
|
|
105
|
-
`;
|
|
106
|
-
const doc = parse(sdl);
|
|
107
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
108
|
-
const existingValue = JSON.stringify({ items: ["a", "b"] });
|
|
109
|
-
const result = makeMinimalObjectForStateType({
|
|
110
|
-
sharedSchemaDocumentNode: doc,
|
|
111
|
-
stateTypeDefinitionNode: typeNode,
|
|
112
|
-
existingValue,
|
|
113
|
-
});
|
|
114
|
-
assert.strictEqual(result, JSON.stringify({ items: ["a", "b"] }, null, 2));
|
|
115
|
-
});
|
|
116
|
-
it("should handle nested objects with missing fields", () => {
|
|
117
|
-
const sdl = `
|
|
118
|
-
type TestState {
|
|
119
|
-
nested: NestedType
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
type NestedType {
|
|
123
|
-
value: String
|
|
124
|
-
count: Int
|
|
125
|
-
}
|
|
126
|
-
`;
|
|
127
|
-
const doc = parse(sdl);
|
|
128
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
129
|
-
const existingValue = JSON.stringify({ nested: { value: "test" } });
|
|
130
|
-
const result = makeMinimalObjectForStateType({
|
|
131
|
-
sharedSchemaDocumentNode: doc,
|
|
132
|
-
stateTypeDefinitionNode: typeNode,
|
|
133
|
-
existingValue,
|
|
134
|
-
});
|
|
135
|
-
assert.strictEqual(result, JSON.stringify({ nested: { value: "test", count: null } }, null, 2));
|
|
136
|
-
});
|
|
137
|
-
it("should handle multiple missing fields", () => {
|
|
138
|
-
const sdl = `
|
|
139
|
-
type TestState {
|
|
140
|
-
name: String
|
|
141
|
-
age: Int
|
|
142
|
-
active: Boolean
|
|
143
|
-
}
|
|
144
|
-
`;
|
|
145
|
-
const doc = parse(sdl);
|
|
146
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
147
|
-
const existingValue = JSON.stringify({});
|
|
148
|
-
const result = makeMinimalObjectForStateType({
|
|
149
|
-
sharedSchemaDocumentNode: doc,
|
|
150
|
-
stateTypeDefinitionNode: typeNode,
|
|
151
|
-
existingValue,
|
|
152
|
-
});
|
|
153
|
-
assert.strictEqual(result, JSON.stringify({ name: null, age: null, active: null }, null, 2));
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
describe("validateStateObject", () => {
|
|
157
|
-
it("should return empty array for valid state", () => {
|
|
158
|
-
const sdl = `
|
|
159
|
-
type TestState {
|
|
160
|
-
name: String
|
|
161
|
-
}
|
|
162
|
-
`;
|
|
163
|
-
const doc = parse(sdl);
|
|
164
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
165
|
-
const value = JSON.stringify({ name: "test" });
|
|
166
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
167
|
-
assert.strictEqual(errors.length, 0);
|
|
168
|
-
});
|
|
169
|
-
it("should return error for invalid JSON", () => {
|
|
170
|
-
const sdl = `
|
|
171
|
-
type TestState {
|
|
172
|
-
name: String
|
|
173
|
-
}
|
|
174
|
-
`;
|
|
175
|
-
const doc = parse(sdl);
|
|
176
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
177
|
-
const value = "invalid json";
|
|
178
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
179
|
-
assert.strictEqual(errors.length, 1);
|
|
180
|
-
assert.ok(errors[0].message.includes("Invalid JSON"));
|
|
181
|
-
});
|
|
182
|
-
it("should return error for missing required field", () => {
|
|
183
|
-
const sdl = `
|
|
184
|
-
type TestState {
|
|
185
|
-
name: String!
|
|
186
|
-
}
|
|
187
|
-
`;
|
|
188
|
-
const doc = parse(sdl);
|
|
189
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
190
|
-
const value = JSON.stringify({});
|
|
191
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
192
|
-
assert.ok(errors.length > 0);
|
|
193
|
-
const stateError = errors[0];
|
|
194
|
-
assert.strictEqual(stateError.kind, "MISSING");
|
|
195
|
-
assert.strictEqual(stateError.field, "name");
|
|
196
|
-
});
|
|
197
|
-
it("should return error for wrong type", () => {
|
|
198
|
-
const sdl = `
|
|
199
|
-
type TestState {
|
|
200
|
-
count: Int
|
|
201
|
-
}
|
|
202
|
-
`;
|
|
203
|
-
const doc = parse(sdl);
|
|
204
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
205
|
-
const value = JSON.stringify({ count: "not a number" });
|
|
206
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
207
|
-
assert.ok(errors.length > 0);
|
|
208
|
-
const stateError = errors[0];
|
|
209
|
-
assert.strictEqual(stateError.kind, "TYPE");
|
|
210
|
-
});
|
|
211
|
-
it("should return error for unknown field", () => {
|
|
212
|
-
const sdl = `
|
|
213
|
-
type TestState {
|
|
214
|
-
name: String
|
|
215
|
-
}
|
|
216
|
-
`;
|
|
217
|
-
const doc = parse(sdl);
|
|
218
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
219
|
-
const value = JSON.stringify({ name: "test", unknownField: "value" });
|
|
220
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
221
|
-
assert.ok(errors.length > 0);
|
|
222
|
-
const stateError = errors[0];
|
|
223
|
-
assert.strictEqual(stateError.kind, "UNKNOWN_FIELD");
|
|
224
|
-
});
|
|
225
|
-
it("should detect missing optional fields", () => {
|
|
226
|
-
const sdl = `
|
|
227
|
-
type TestState {
|
|
228
|
-
name: String
|
|
229
|
-
age: Int
|
|
230
|
-
}
|
|
231
|
-
`;
|
|
232
|
-
const doc = parse(sdl);
|
|
233
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
234
|
-
const value = JSON.stringify({ name: "test" });
|
|
235
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
236
|
-
assert.ok(errors.length > 0);
|
|
237
|
-
const stateError = errors[0];
|
|
238
|
-
assert.strictEqual(stateError.kind, "MISSING_OPTIONAL");
|
|
239
|
-
assert.strictEqual(stateError.field, "age");
|
|
240
|
-
});
|
|
241
|
-
it("should not detect missing optional fields on empty array", () => {
|
|
242
|
-
const sdl = `
|
|
243
|
-
type TestState {
|
|
244
|
-
items: [Item!]!
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
type Item {
|
|
248
|
-
text: String
|
|
249
|
-
}
|
|
250
|
-
`;
|
|
251
|
-
const doc = parse(sdl);
|
|
252
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
253
|
-
const value = JSON.stringify({ items: [] });
|
|
254
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
255
|
-
const stateError = errors.at(0);
|
|
256
|
-
assert.ifError(stateError);
|
|
257
|
-
});
|
|
258
|
-
it("should return NON_NULL error for null value in required field", () => {
|
|
259
|
-
const sdl = `
|
|
260
|
-
type TestState {
|
|
261
|
-
name: String!
|
|
262
|
-
}
|
|
263
|
-
`;
|
|
264
|
-
const doc = parse(sdl);
|
|
265
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
266
|
-
const value = JSON.stringify({ name: null });
|
|
267
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
268
|
-
assert.ok(errors.length > 0);
|
|
269
|
-
const stateError = errors[0];
|
|
270
|
-
assert.strictEqual(stateError.kind, "NON_NULL");
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
describe("makeMinimalObjectForStateType", () => {
|
|
274
|
-
it("should preserve existing valid values", () => {
|
|
275
|
-
const sdl = `
|
|
276
|
-
type TestState {
|
|
277
|
-
name: String
|
|
278
|
-
count: Int
|
|
279
|
-
}
|
|
280
|
-
`;
|
|
281
|
-
const doc = parse(sdl);
|
|
282
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
283
|
-
const result = makeMinimalObjectForStateType({
|
|
284
|
-
sharedSchemaDocumentNode: doc,
|
|
285
|
-
stateTypeDefinitionNode: typeNode,
|
|
286
|
-
existingValue: JSON.stringify({ name: "test", count: 42 }),
|
|
287
|
-
});
|
|
288
|
-
assert.strictEqual(result, JSON.stringify({ name: "test", count: 42 }, null, 2));
|
|
289
|
-
});
|
|
290
|
-
it("should fill missing fields with default values", () => {
|
|
291
|
-
const sdl = `
|
|
292
|
-
type TestState {
|
|
293
|
-
name: String!
|
|
294
|
-
count: Int!
|
|
295
|
-
active: Boolean!
|
|
296
|
-
}
|
|
297
|
-
`;
|
|
298
|
-
const doc = parse(sdl);
|
|
299
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
300
|
-
const result = makeMinimalObjectForStateType({
|
|
301
|
-
sharedSchemaDocumentNode: doc,
|
|
302
|
-
stateTypeDefinitionNode: typeNode,
|
|
303
|
-
existingValue: JSON.stringify({}),
|
|
304
|
-
});
|
|
305
|
-
assert.strictEqual(result, JSON.stringify({ name: "", count: 0, active: false }, null, 2));
|
|
306
|
-
});
|
|
307
|
-
it("should set required list fields to empty array", () => {
|
|
308
|
-
const sdl = `
|
|
309
|
-
type TestState {
|
|
310
|
-
items: [Item!]!
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
type Item {
|
|
314
|
-
id: ID!
|
|
315
|
-
}
|
|
316
|
-
`;
|
|
317
|
-
const doc = parse(sdl);
|
|
318
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
319
|
-
const result = makeMinimalObjectForStateType({
|
|
320
|
-
sharedSchemaDocumentNode: doc,
|
|
321
|
-
stateTypeDefinitionNode: typeNode,
|
|
322
|
-
existingValue: JSON.stringify({}),
|
|
323
|
-
});
|
|
324
|
-
assert.strictEqual(result, JSON.stringify({ items: [] }, null, 2));
|
|
325
|
-
});
|
|
326
|
-
it("should set optional list fields to null when missing", () => {
|
|
327
|
-
const sdl = `
|
|
328
|
-
type TestState {
|
|
329
|
-
items: [String]
|
|
330
|
-
}
|
|
331
|
-
`;
|
|
332
|
-
const doc = parse(sdl);
|
|
333
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
334
|
-
const result = makeMinimalObjectForStateType({
|
|
335
|
-
sharedSchemaDocumentNode: doc,
|
|
336
|
-
stateTypeDefinitionNode: typeNode,
|
|
337
|
-
existingValue: JSON.stringify({}),
|
|
338
|
-
});
|
|
339
|
-
assert.strictEqual(result, JSON.stringify({ items: null }, null, 2));
|
|
340
|
-
});
|
|
341
|
-
it("should preserve existing array values", () => {
|
|
342
|
-
const sdl = `
|
|
343
|
-
type TestState {
|
|
344
|
-
items: [String!]!
|
|
345
|
-
}
|
|
346
|
-
`;
|
|
347
|
-
const doc = parse(sdl);
|
|
348
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
349
|
-
const result = makeMinimalObjectForStateType({
|
|
350
|
-
sharedSchemaDocumentNode: doc,
|
|
351
|
-
stateTypeDefinitionNode: typeNode,
|
|
352
|
-
existingValue: JSON.stringify({ items: ["a", "b", "c"] }),
|
|
353
|
-
});
|
|
354
|
-
assert.strictEqual(result, JSON.stringify({ items: ["a", "b", "c"] }, null, 2));
|
|
355
|
-
});
|
|
356
|
-
it("should handle enum fields with default value", () => {
|
|
357
|
-
const sdl = `
|
|
358
|
-
type TestState {
|
|
359
|
-
status: Status!
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
enum Status {
|
|
363
|
-
ACTIVE
|
|
364
|
-
INACTIVE
|
|
365
|
-
PENDING
|
|
366
|
-
}
|
|
367
|
-
`;
|
|
368
|
-
const doc = parse(sdl);
|
|
369
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
370
|
-
const result = makeMinimalObjectForStateType({
|
|
371
|
-
sharedSchemaDocumentNode: doc,
|
|
372
|
-
stateTypeDefinitionNode: typeNode,
|
|
373
|
-
existingValue: JSON.stringify({}),
|
|
374
|
-
});
|
|
375
|
-
assert.strictEqual(result, JSON.stringify({ status: "ACTIVE" }, null, 2));
|
|
376
|
-
});
|
|
377
|
-
it("should preserve valid enum values", () => {
|
|
378
|
-
const sdl = `
|
|
379
|
-
type TestState {
|
|
380
|
-
status: Status!
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
enum Status {
|
|
384
|
-
ACTIVE
|
|
385
|
-
INACTIVE
|
|
386
|
-
PENDING
|
|
387
|
-
}
|
|
388
|
-
`;
|
|
389
|
-
const doc = parse(sdl);
|
|
390
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
391
|
-
const result = makeMinimalObjectForStateType({
|
|
392
|
-
sharedSchemaDocumentNode: doc,
|
|
393
|
-
stateTypeDefinitionNode: typeNode,
|
|
394
|
-
existingValue: JSON.stringify({ status: "PENDING" }),
|
|
395
|
-
});
|
|
396
|
-
assert.strictEqual(result, JSON.stringify({ status: "PENDING" }, null, 2));
|
|
397
|
-
});
|
|
398
|
-
it("should handle nested object types", () => {
|
|
399
|
-
const sdl = `
|
|
400
|
-
type TestState {
|
|
401
|
-
nested: NestedType!
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
type NestedType {
|
|
405
|
-
value: String!
|
|
406
|
-
count: Int!
|
|
407
|
-
}
|
|
408
|
-
`;
|
|
409
|
-
const doc = parse(sdl);
|
|
410
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
411
|
-
const result = makeMinimalObjectForStateType({
|
|
412
|
-
sharedSchemaDocumentNode: doc,
|
|
413
|
-
stateTypeDefinitionNode: typeNode,
|
|
414
|
-
existingValue: JSON.stringify({}),
|
|
415
|
-
});
|
|
416
|
-
assert.strictEqual(result, JSON.stringify({ nested: { value: "", count: 0 } }, null, 2));
|
|
417
|
-
});
|
|
418
|
-
it("should return original value for invalid JSON", () => {
|
|
419
|
-
const sdl = `
|
|
420
|
-
type TestState {
|
|
421
|
-
name: String
|
|
422
|
-
}
|
|
423
|
-
`;
|
|
424
|
-
const doc = parse(sdl);
|
|
425
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
426
|
-
const invalidJson = "not valid json";
|
|
427
|
-
const result = makeMinimalObjectForStateType({
|
|
428
|
-
sharedSchemaDocumentNode: doc,
|
|
429
|
-
stateTypeDefinitionNode: typeNode,
|
|
430
|
-
existingValue: invalidJson,
|
|
431
|
-
});
|
|
432
|
-
assert.strictEqual(result, invalidJson);
|
|
433
|
-
});
|
|
434
|
-
it("should handle ID fields with placeholder", () => {
|
|
435
|
-
const sdl = `
|
|
436
|
-
type TestState {
|
|
437
|
-
id: ID!
|
|
438
|
-
}
|
|
439
|
-
`;
|
|
440
|
-
const doc = parse(sdl);
|
|
441
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
442
|
-
const result = makeMinimalObjectForStateType({
|
|
443
|
-
sharedSchemaDocumentNode: doc,
|
|
444
|
-
stateTypeDefinitionNode: typeNode,
|
|
445
|
-
existingValue: JSON.stringify({}),
|
|
446
|
-
});
|
|
447
|
-
assert.strictEqual(result, JSON.stringify({ id: "placeholder-id" }, null, 2));
|
|
448
|
-
});
|
|
449
|
-
it("should handle Float fields", () => {
|
|
450
|
-
const sdl = `
|
|
451
|
-
type TestState {
|
|
452
|
-
price: Float!
|
|
453
|
-
}
|
|
454
|
-
`;
|
|
455
|
-
const doc = parse(sdl);
|
|
456
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
457
|
-
const result = makeMinimalObjectForStateType({
|
|
458
|
-
sharedSchemaDocumentNode: doc,
|
|
459
|
-
stateTypeDefinitionNode: typeNode,
|
|
460
|
-
existingValue: JSON.stringify({}),
|
|
461
|
-
});
|
|
462
|
-
assert.strictEqual(result, JSON.stringify({ price: 0 }, null, 2));
|
|
463
|
-
});
|
|
464
|
-
it("should replace invalid type values with defaults", () => {
|
|
465
|
-
const sdl = `
|
|
466
|
-
type TestState {
|
|
467
|
-
count: Int!
|
|
468
|
-
name: String!
|
|
469
|
-
}
|
|
470
|
-
`;
|
|
471
|
-
const doc = parse(sdl);
|
|
472
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
473
|
-
const result = makeMinimalObjectForStateType({
|
|
474
|
-
sharedSchemaDocumentNode: doc,
|
|
475
|
-
stateTypeDefinitionNode: typeNode,
|
|
476
|
-
existingValue: JSON.stringify({ count: "not a number", name: 123 }),
|
|
477
|
-
});
|
|
478
|
-
assert.strictEqual(result, JSON.stringify({ count: 0, name: "" }, null, 2));
|
|
479
|
-
});
|
|
480
|
-
});
|
|
481
|
-
describe("Integration: Component flow simulation", () => {
|
|
482
|
-
it("should handle the syncWithSchema=true flow (validate -> makeMinimal)", () => {
|
|
483
|
-
const sdl = `
|
|
484
|
-
type TestModelState {
|
|
485
|
-
name: String!
|
|
486
|
-
items: [String!]!
|
|
487
|
-
count: Int
|
|
488
|
-
}
|
|
489
|
-
`;
|
|
490
|
-
const doc = parse(sdl);
|
|
491
|
-
const typeNode = getStateTypeFromSharedSchema(doc, "TestModel", "global");
|
|
492
|
-
assert.ok(typeNode, "State type should be found");
|
|
493
|
-
const existingValue = JSON.stringify({ name: "test" });
|
|
494
|
-
// Step 1: validateStateObject with checkMissingOptionalFields
|
|
495
|
-
const errors = validateStateObject(doc, typeNode, existingValue);
|
|
496
|
-
// Should have errors for missing fields (items is required, count is optional)
|
|
497
|
-
assert.ok(errors.length > 0, "Should have validation errors");
|
|
498
|
-
// Step 2: makeMinimalObjectForStateType to fix (when syncWithSchema=true)
|
|
499
|
-
const fixedState = makeMinimalObjectForStateType({
|
|
500
|
-
sharedSchemaDocumentNode: doc,
|
|
501
|
-
stateTypeDefinitionNode: typeNode,
|
|
502
|
-
existingValue,
|
|
503
|
-
});
|
|
504
|
-
assert.strictEqual(fixedState, JSON.stringify({ name: "test", items: [], count: null }, null, 2));
|
|
505
|
-
});
|
|
506
|
-
it("should handle the syncWithSchema=false flow (validate with checkMissingOptionalFields)", () => {
|
|
507
|
-
const sdl = `
|
|
508
|
-
type TestModelState {
|
|
509
|
-
name: String!
|
|
510
|
-
description: String
|
|
511
|
-
tags: [String]
|
|
512
|
-
}
|
|
513
|
-
`;
|
|
514
|
-
const doc = parse(sdl);
|
|
515
|
-
const typeNode = getStateTypeFromSharedSchema(doc, "TestModel", "global");
|
|
516
|
-
assert.ok(typeNode, "State type should be found");
|
|
517
|
-
const existingValue = JSON.stringify({ name: "test" });
|
|
518
|
-
// When syncWithSchema is false, we check for missing optional fields
|
|
519
|
-
const errors = validateStateObject(doc, typeNode, existingValue);
|
|
520
|
-
// Should have 2 MISSING_OPTIONAL errors (description and tags)
|
|
521
|
-
const missingOptionalErrors = errors.filter((e) => e instanceof StateValidationError && e.kind === "MISSING_OPTIONAL");
|
|
522
|
-
assert.strictEqual(missingOptionalErrors.length, 2);
|
|
523
|
-
});
|
|
524
|
-
it("should use makeMinimalObjectForStateType as fallback when validation fails", () => {
|
|
525
|
-
const sdl = `
|
|
526
|
-
type TestModelState {
|
|
527
|
-
count: Int!
|
|
528
|
-
active: Boolean!
|
|
529
|
-
}
|
|
530
|
-
`;
|
|
531
|
-
const doc = parse(sdl);
|
|
532
|
-
const typeNode = getStateTypeFromSharedSchema(doc, "TestModel", "global");
|
|
533
|
-
assert.ok(typeNode, "State type should be found");
|
|
534
|
-
// Invalid state with wrong types
|
|
535
|
-
const existingValue = JSON.stringify({
|
|
536
|
-
count: "not a number",
|
|
537
|
-
active: "not a boolean",
|
|
538
|
-
});
|
|
539
|
-
// Validation should fail
|
|
540
|
-
const errors = validateStateObject(doc, typeNode, existingValue);
|
|
541
|
-
assert.ok(errors.length > 0, "Should have validation errors");
|
|
542
|
-
// makeMinimalObjectForStateType should fix it
|
|
543
|
-
const fixedState = makeMinimalObjectForStateType({
|
|
544
|
-
sharedSchemaDocumentNode: doc,
|
|
545
|
-
stateTypeDefinitionNode: typeNode,
|
|
546
|
-
existingValue,
|
|
547
|
-
});
|
|
548
|
-
assert.strictEqual(fixedState, JSON.stringify({ count: 0, active: false }, null, 2));
|
|
549
|
-
});
|
|
550
|
-
it("should handle empty string initial value (converted to '{}')", () => {
|
|
551
|
-
const sdl = `
|
|
552
|
-
type TestModelState {
|
|
553
|
-
name: String!
|
|
554
|
-
items: [Item!]!
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
type Item {
|
|
558
|
-
id: ID!
|
|
559
|
-
}
|
|
560
|
-
`;
|
|
561
|
-
const doc = parse(sdl);
|
|
562
|
-
const typeNode = getStateTypeFromSharedSchema(doc, "TestModel", "global");
|
|
563
|
-
assert.ok(typeNode, "State type should be found");
|
|
564
|
-
// Component converts empty string to "{}" (line 80)
|
|
565
|
-
const existingValue = "{}";
|
|
566
|
-
// Validate should find errors for missing required fields
|
|
567
|
-
const errors = validateStateObject(doc, typeNode, existingValue);
|
|
568
|
-
assert.ok(errors.length > 0, "Should have validation errors");
|
|
569
|
-
// makeMinimalObjectForStateType fills in defaults
|
|
570
|
-
const fixedState = makeMinimalObjectForStateType({
|
|
571
|
-
sharedSchemaDocumentNode: doc,
|
|
572
|
-
stateTypeDefinitionNode: typeNode,
|
|
573
|
-
existingValue,
|
|
574
|
-
});
|
|
575
|
-
assert.strictEqual(fixedState, JSON.stringify({ name: "", items: [] }, null, 2));
|
|
576
|
-
});
|
|
577
|
-
it("should handle local state with LocalState suffix", () => {
|
|
578
|
-
const sdl = `
|
|
579
|
-
type TestModelLocalState {
|
|
580
|
-
localData: String
|
|
581
|
-
}
|
|
582
|
-
`;
|
|
583
|
-
const doc = parse(sdl);
|
|
584
|
-
const typeNode = getStateTypeFromSharedSchema(doc, "TestModel", "local");
|
|
585
|
-
assert.ok(typeNode, "Local state type should be found");
|
|
586
|
-
const existingValue = JSON.stringify({});
|
|
587
|
-
// Validate should find missing optional field
|
|
588
|
-
const errors = validateStateObject(doc, typeNode, existingValue);
|
|
589
|
-
assert.strictEqual(errors.length, 1, "Should have 1 missing optional field");
|
|
590
|
-
// makeMinimalObjectForStateType fills in defaults
|
|
591
|
-
const fixedState = makeMinimalObjectForStateType({
|
|
592
|
-
sharedSchemaDocumentNode: doc,
|
|
593
|
-
stateTypeDefinitionNode: typeNode,
|
|
594
|
-
existingValue,
|
|
595
|
-
});
|
|
596
|
-
assert.strictEqual(fixedState, JSON.stringify({ localData: null }, null, 2));
|
|
597
|
-
});
|
|
598
|
-
});
|
|
599
|
-
describe("Edge cases", () => {
|
|
600
|
-
it("makeMinimalObjectForStateType should handle deeply nested objects", () => {
|
|
601
|
-
const sdl = `
|
|
602
|
-
type TestState {
|
|
603
|
-
level1: Level1!
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
type Level1 {
|
|
607
|
-
level2: Level2!
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
type Level2 {
|
|
611
|
-
value: String
|
|
612
|
-
}
|
|
613
|
-
`;
|
|
614
|
-
const doc = parse(sdl);
|
|
615
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
616
|
-
const existingValue = JSON.stringify({
|
|
617
|
-
level1: {
|
|
618
|
-
level2: {},
|
|
619
|
-
},
|
|
620
|
-
});
|
|
621
|
-
const result = makeMinimalObjectForStateType({
|
|
622
|
-
sharedSchemaDocumentNode: doc,
|
|
623
|
-
stateTypeDefinitionNode: typeNode,
|
|
624
|
-
existingValue,
|
|
625
|
-
});
|
|
626
|
-
assert.strictEqual(result, JSON.stringify({ level1: { level2: { value: null } } }, null, 2));
|
|
627
|
-
});
|
|
628
|
-
it("validateStateObject should handle nested object type errors", () => {
|
|
629
|
-
const sdl = `
|
|
630
|
-
type TestState {
|
|
631
|
-
nested: NestedType!
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
type NestedType {
|
|
635
|
-
count: Int!
|
|
636
|
-
}
|
|
637
|
-
`;
|
|
638
|
-
const doc = parse(sdl);
|
|
639
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
640
|
-
const value = JSON.stringify({
|
|
641
|
-
nested: { count: "not a number" },
|
|
642
|
-
});
|
|
643
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
644
|
-
assert.ok(errors.length > 0);
|
|
645
|
-
const stateError = errors[0];
|
|
646
|
-
assert.strictEqual(stateError.kind, "TYPE");
|
|
647
|
-
});
|
|
648
|
-
it("should handle state with all field types", () => {
|
|
649
|
-
const sdl = `
|
|
650
|
-
type TestState {
|
|
651
|
-
id: ID!
|
|
652
|
-
name: String!
|
|
653
|
-
count: Int!
|
|
654
|
-
price: Float!
|
|
655
|
-
active: Boolean!
|
|
656
|
-
status: Status!
|
|
657
|
-
items: [String!]!
|
|
658
|
-
optionalName: String
|
|
659
|
-
optionalItems: [String]
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
enum Status {
|
|
663
|
-
ACTIVE
|
|
664
|
-
INACTIVE
|
|
665
|
-
}
|
|
666
|
-
`;
|
|
667
|
-
const doc = parse(sdl);
|
|
668
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
669
|
-
const result = makeMinimalObjectForStateType({
|
|
670
|
-
sharedSchemaDocumentNode: doc,
|
|
671
|
-
stateTypeDefinitionNode: typeNode,
|
|
672
|
-
existingValue: JSON.stringify({}),
|
|
673
|
-
});
|
|
674
|
-
assert.strictEqual(result, JSON.stringify({
|
|
675
|
-
id: "placeholder-id",
|
|
676
|
-
name: "",
|
|
677
|
-
count: 0,
|
|
678
|
-
price: 0,
|
|
679
|
-
active: false,
|
|
680
|
-
status: "ACTIVE",
|
|
681
|
-
items: [],
|
|
682
|
-
optionalName: null,
|
|
683
|
-
optionalItems: null,
|
|
684
|
-
}, null, 2));
|
|
685
|
-
});
|
|
686
|
-
it("makeMinimalObjectForStateType should add missing fields to array items", () => {
|
|
687
|
-
const sdl = `
|
|
688
|
-
type TestState {
|
|
689
|
-
items: [Item!]!
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
type Item {
|
|
693
|
-
id: ID!
|
|
694
|
-
name: String
|
|
695
|
-
}
|
|
696
|
-
`;
|
|
697
|
-
const doc = parse(sdl);
|
|
698
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
699
|
-
const existingValue = JSON.stringify({
|
|
700
|
-
items: [{ id: "1" }, { id: "2", name: "test" }],
|
|
701
|
-
}, null, 2);
|
|
702
|
-
const result = makeMinimalObjectForStateType({
|
|
703
|
-
sharedSchemaDocumentNode: doc,
|
|
704
|
-
stateTypeDefinitionNode: typeNode,
|
|
705
|
-
existingValue,
|
|
706
|
-
});
|
|
707
|
-
// makeMinimalObjectForStateType preserves array items as-is
|
|
708
|
-
assert.strictEqual(result, JSON.stringify({
|
|
709
|
-
items: [
|
|
710
|
-
{ id: "1", name: null },
|
|
711
|
-
{ id: "2", name: "test" },
|
|
712
|
-
],
|
|
713
|
-
}, null, 2));
|
|
714
|
-
});
|
|
715
|
-
it("validateStateObject should accept valid complex state", () => {
|
|
716
|
-
const sdl = `
|
|
717
|
-
type TestState {
|
|
718
|
-
user: User!
|
|
719
|
-
settings: Settings
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
type User {
|
|
723
|
-
id: ID!
|
|
724
|
-
name: String!
|
|
725
|
-
email: String
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
type Settings {
|
|
729
|
-
theme: String
|
|
730
|
-
notifications: Boolean
|
|
731
|
-
}
|
|
732
|
-
`;
|
|
733
|
-
const doc = parse(sdl);
|
|
734
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
735
|
-
const value = JSON.stringify({
|
|
736
|
-
user: {
|
|
737
|
-
id: "user-1",
|
|
738
|
-
name: "John Doe",
|
|
739
|
-
email: null,
|
|
740
|
-
},
|
|
741
|
-
settings: {
|
|
742
|
-
theme: "dark",
|
|
743
|
-
notifications: true,
|
|
744
|
-
},
|
|
745
|
-
});
|
|
746
|
-
const errors = validateStateObject(doc, typeNode, value);
|
|
747
|
-
assert.strictEqual(errors.length, 0);
|
|
748
|
-
});
|
|
749
|
-
it("should handle state with optional recursive field types", () => {
|
|
750
|
-
const sdl = `
|
|
751
|
-
type TestState {
|
|
752
|
-
item: Item!
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
type Item {
|
|
756
|
-
name: String!
|
|
757
|
-
child: Item
|
|
758
|
-
}
|
|
759
|
-
`;
|
|
760
|
-
const doc = parse(sdl);
|
|
761
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
762
|
-
const result = makeMinimalObjectForStateType({
|
|
763
|
-
sharedSchemaDocumentNode: doc,
|
|
764
|
-
stateTypeDefinitionNode: typeNode,
|
|
765
|
-
existingValue: JSON.stringify({}),
|
|
766
|
-
});
|
|
767
|
-
assert.strictEqual(result, JSON.stringify({
|
|
768
|
-
item: {
|
|
769
|
-
name: "",
|
|
770
|
-
child: null,
|
|
771
|
-
},
|
|
772
|
-
}, null, 2));
|
|
773
|
-
});
|
|
774
|
-
it("should handle state with required recursive field types", () => {
|
|
775
|
-
const sdl = `
|
|
776
|
-
type TestState {
|
|
777
|
-
item: Item!
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
type Item {
|
|
781
|
-
name: String!
|
|
782
|
-
child: Item!
|
|
783
|
-
}
|
|
784
|
-
`;
|
|
785
|
-
const doc = parse(sdl);
|
|
786
|
-
const typeNode = getStateTypeNode(sdl, "TestState");
|
|
787
|
-
const result = makeMinimalObjectForStateType({
|
|
788
|
-
sharedSchemaDocumentNode: doc,
|
|
789
|
-
stateTypeDefinitionNode: typeNode,
|
|
790
|
-
existingValue: JSON.stringify({}),
|
|
791
|
-
});
|
|
792
|
-
assert.strictEqual(result, JSON.stringify({
|
|
793
|
-
item: {
|
|
794
|
-
name: "",
|
|
795
|
-
child: null,
|
|
796
|
-
},
|
|
797
|
-
}, null, 2));
|
|
798
|
-
const errors = validateStateObject(doc, typeNode, result);
|
|
799
|
-
assert.equal(errors.length, 1);
|
|
800
|
-
const error = errors[0];
|
|
801
|
-
assert.equal(error.kind, "RECURSIVE_TYPE");
|
|
802
|
-
assert.equal(error.field, "item");
|
|
803
|
-
});
|
|
804
|
-
});
|
|
805
|
-
//# sourceMappingURL=helpers.test.js.map
|