@alloy-js/json 0.5.0 → 0.7.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.
@@ -1,4 +1,6 @@
1
+ import { reactive, renderTree } from "@alloy-js/core";
1
2
  import "@alloy-js/core/testing";
3
+ import { d, printTree } from "@alloy-js/core/testing";
2
4
  import { expect, it } from "vitest";
3
5
  import { jsonTest } from "./utils.jsx";
4
6
 
@@ -12,20 +14,48 @@ it("renders arrays", () => {
12
14
  const template = jsonTest([1, "hello"]);
13
15
 
14
16
  expect(template).toRenderTo(`
17
+ [1, "hello"]
18
+ `);
19
+ });
20
+
21
+ it("renders nested arrays", () => {
22
+ const template = jsonTest([1, ["hello there this is a long thing"]]);
23
+
24
+ expect(template).toRenderTo(
25
+ `
15
26
  [
16
27
  1,
17
- "hello"
28
+ [
29
+ "hello there this is a long thing"
30
+ ]
18
31
  ]
19
- `);
32
+ `,
33
+ { printWidth: 20 },
34
+ );
20
35
  });
21
36
 
22
- it("renders nested arrays", () => {
23
- const template = jsonTest([1, ["hello"]]);
37
+ it("works reactively", () => {
38
+ const arr = reactive([] as (number | string)[]);
24
39
 
25
- expect(template).toRenderTo(`
40
+ const template = jsonTest(arr);
41
+ const tree = renderTree(template);
42
+
43
+ expect(printTree(tree)).toEqual(`[]`);
44
+
45
+ arr.push(1);
46
+ expect(printTree(tree)).toEqual(`[1]`);
47
+
48
+ arr.push(2);
49
+ expect(printTree(tree)).toEqual(d`
50
+ [1, 2]
51
+ `);
52
+
53
+ arr.push("this is a long item that should be wrapped");
54
+ expect(printTree(tree, { printWidth: 20 })).toEqual(d`
26
55
  [
27
56
  1,
28
- ["hello"]
57
+ 2,
58
+ "this is a long item that should be wrapped"
29
59
  ]
30
60
  `);
31
61
  });
@@ -1,5 +1,6 @@
1
- import { Output } from "@alloy-js/core";
1
+ import { List, Output, reactive, renderTree } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
+ import { d, printTree } from "@alloy-js/core/testing";
3
4
  import { expect, it } from "vitest";
4
5
  import {
5
6
  JsonObject,
@@ -9,10 +10,7 @@ import { SourceFile } from "../src/components/SourceFile.jsx";
9
10
  import { jsonTest } from "./utils.jsx";
10
11
 
11
12
  it("renders objects", () => {
12
- const template = jsonTest({
13
- a: 1,
14
- b: "hello",
15
- });
13
+ const template = jsonTest({ a: 1, b: "hello" });
16
14
 
17
15
  expect(template).toRenderTo(`
18
16
  {
@@ -39,17 +37,18 @@ it("renders nested objects", () => {
39
37
  });
40
38
 
41
39
  it("can can manually assemble objects", () => {
42
- const template =
40
+ const template = (
43
41
  <Output>
44
42
  <SourceFile path="test.json">
45
43
  <JsonObject>
46
- <JsonObjectProperty name="foo">12</JsonObjectProperty>,
47
- <JsonObjectProperty name="bar">
48
- 13
49
- </JsonObjectProperty>
44
+ <List comma softline>
45
+ <JsonObjectProperty name="foo">12</JsonObjectProperty>
46
+ <JsonObjectProperty name="bar">13</JsonObjectProperty>
47
+ </List>
50
48
  </JsonObject>
51
49
  </SourceFile>
52
- </Output>;
50
+ </Output>
51
+ );
53
52
 
54
53
  expect(template).toRenderTo(`
55
54
  {
@@ -58,3 +57,33 @@ it("can can manually assemble objects", () => {
58
57
  }
59
58
  `);
60
59
  });
60
+
61
+ it("works reactively", () => {
62
+ const obj = reactive({ a: 1 } as Record<string, unknown>);
63
+
64
+ const template = jsonTest(obj);
65
+ const tree = renderTree(template);
66
+
67
+ expect(printTree(tree)).toEqual(d`
68
+ {
69
+ "a": 1
70
+ }
71
+ `);
72
+
73
+ (obj.a as any)++;
74
+
75
+ expect(printTree(tree)).toEqual(d`
76
+ {
77
+ "a": 2
78
+ }
79
+ `);
80
+
81
+ obj.b = "hello";
82
+
83
+ expect(printTree(tree)).toEqual(d`
84
+ {
85
+ "a": 2,
86
+ "b": "hello"
87
+ }
88
+ `);
89
+ });
@@ -12,18 +12,21 @@ import { SourceFile } from "../src/components/SourceFile.jsx";
12
12
  it("references objects within the same file", () => {
13
13
  const objectValue = refkey();
14
14
  const nullKey = refkey();
15
- const template =
15
+ const template = (
16
16
  <Output>
17
17
  <SourceFile path="test.json">
18
18
  <JsonObject refkey={objectValue}>
19
19
  <JsonObjectProperty name="nullValue">
20
- <JsonValue jsValue={null} refkey={nullKey} />,
20
+ <JsonValue jsValue={null} refkey={nullKey} />
21
+ </JsonObjectProperty>
22
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>
23
+ <JsonObjectProperty name="refToObject">
24
+ {objectValue}
21
25
  </JsonObjectProperty>
22
- <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
23
- <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
24
26
  </JsonObject>
25
27
  </SourceFile>
26
- </Output>;
28
+ </Output>
29
+ );
27
30
 
28
31
  expect(template).toRenderTo(`
29
32
  {
@@ -37,28 +40,30 @@ it("references objects within the same file", () => {
37
40
  it("references objects across files", () => {
38
41
  const objectValue = refkey();
39
42
  const nullKey = refkey();
40
- const template =
43
+ const template = (
41
44
  <Output>
42
45
  <SourceFile path="test.json">
43
46
  <JsonObject refkey={objectValue}>
44
47
  <JsonObjectProperty name="nullValue">
45
- <JsonValue jsValue={null} refkey={nullKey} />,
48
+ <JsonValue jsValue={null} refkey={nullKey} />
46
49
  </JsonObjectProperty>
47
50
  </JsonObject>
48
51
  </SourceFile>
49
52
  <SourceFile path="test.json">
50
53
  <JsonObject>
51
- <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
52
- <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
54
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>
55
+ <JsonObjectProperty name="refToObject">
56
+ {objectValue}
57
+ </JsonObjectProperty>
53
58
  </JsonObject>
54
59
  </SourceFile>
55
- </Output>;
60
+ </Output>
61
+ );
56
62
 
57
63
  expect(template).toRenderTo(`
58
64
  {
59
- "nullValue": null,
60
- }
61
- {
65
+ "nullValue": null
66
+ }{
62
67
  "refToNull": "test.json#/nullValue",
63
68
  "refToObject": "test.json#"
64
69
  }
@@ -68,30 +73,32 @@ it("references objects across files", () => {
68
73
  it("references objects across files and directories", () => {
69
74
  const objectValue = refkey();
70
75
  const nullKey = refkey();
71
- const template =
76
+ const template = (
72
77
  <Output>
73
78
  <SourceDirectory path="subdir">
74
79
  <SourceFile path="test.json">
75
80
  <JsonObject refkey={objectValue}>
76
81
  <JsonObjectProperty name="nullValue">
77
- <JsonValue jsValue={null} refkey={nullKey} />,
82
+ <JsonValue jsValue={null} refkey={nullKey} />
78
83
  </JsonObjectProperty>
79
84
  </JsonObject>
80
- </SourceFile>
85
+ </SourceFile>
81
86
  </SourceDirectory>
82
87
  <SourceFile path="test.json">
83
88
  <JsonObject>
84
- <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
85
- <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
89
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>
90
+ <JsonObjectProperty name="refToObject">
91
+ {objectValue}
92
+ </JsonObjectProperty>
86
93
  </JsonObject>
87
94
  </SourceFile>
88
- </Output>;
95
+ </Output>
96
+ );
89
97
 
90
98
  expect(template).toRenderTo(`
91
99
  {
92
- "nullValue": null,
93
- }
94
- {
100
+ "nullValue": null
101
+ }{
95
102
  "refToNull": "subdir/test.json#/nullValue",
96
103
  "refToObject": "subdir/test.json#"
97
104
  }
@@ -101,25 +108,22 @@ it("references objects across files and directories", () => {
101
108
  it("references arrays within the same file", () => {
102
109
  const arrayValue = refkey();
103
110
  const nullKey = refkey();
104
- const template =
111
+ const template = (
105
112
  <Output>
106
113
  <SourceFile path="test.json">
107
114
  <JsonArray refkey={arrayValue}>
108
- <JsonArrayElement>{nullKey}</JsonArrayElement>,
109
- <JsonArrayElement>{arrayValue}</JsonArrayElement>,
115
+ <JsonArrayElement>{nullKey}</JsonArrayElement>
116
+ <JsonArrayElement>{arrayValue}</JsonArrayElement>
110
117
  <JsonArrayElement>
111
118
  <JsonValue jsValue={null} refkey={nullKey} />
112
119
  </JsonArrayElement>
113
120
  </JsonArray>
114
121
  </SourceFile>
115
- </Output>;
122
+ </Output>
123
+ );
116
124
 
117
125
  expect(template).toRenderTo(`
118
- [
119
- "#/2",
120
- "#",
121
- null
122
- ]
126
+ ["#/2", "#", null]
123
127
  `);
124
128
  });
125
129
 
@@ -128,19 +132,23 @@ it("does complex references", () => {
128
132
  const arrayValue = refkey();
129
133
  const nullKey = refkey();
130
134
  const nestedKey = refkey();
131
- const template =
135
+ const template = (
132
136
  <Output>
133
137
  <SourceFile path="test.json">
134
138
  <JsonObject refkey={objectValue}>
135
139
  <JsonObjectProperty name="nullValue">
136
- <JsonValue jsValue={null} refkey={nullKey} />,
140
+ <JsonValue jsValue={null} refkey={nullKey} />
141
+ </JsonObjectProperty>
142
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>
143
+ <JsonObjectProperty name="refToObject">
144
+ {objectValue}
145
+ </JsonObjectProperty>
146
+ <JsonObjectProperty name="refToNested">
147
+ {nestedKey}
137
148
  </JsonObjectProperty>
138
- <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
139
- <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
140
- <JsonObjectProperty name="refToNested">{nestedKey}</JsonObjectProperty>
141
149
  <JsonObjectProperty name="nestedArray">
142
150
  <JsonArray refkey={arrayValue}>
143
- <JsonArrayElement>{nullKey}</JsonArrayElement>,
151
+ <JsonArrayElement>{nullKey}</JsonArrayElement>
144
152
  <JsonArrayElement>{arrayValue}</JsonArrayElement>
145
153
  <JsonArrayElement>
146
154
  <JsonObject>
@@ -153,17 +161,18 @@ it("does complex references", () => {
153
161
  </JsonObjectProperty>
154
162
  </JsonObject>
155
163
  </SourceFile>
156
- </Output>;
164
+ </Output>
165
+ );
157
166
 
158
167
  expect(template).toRenderTo(`
159
168
  {
160
169
  "nullValue": null,
161
170
  "refToNull": "#/nullValue",
162
- "refToObject": "#"
163
- "refToNested": "#/nestedArray/2/nestedProp"
171
+ "refToObject": "#",
172
+ "refToNested": "#/nestedArray/2/nestedProp",
164
173
  "nestedArray": [
165
174
  "#/nullValue",
166
- "#/nestedArray"
175
+ "#/nestedArray",
167
176
  {
168
177
  "nestedProp": "hi"
169
178
  }
package/test/utils.tsx CHANGED
@@ -3,9 +3,11 @@ import { JsonValue } from "../src/components/JsonValue.jsx";
3
3
  import { SourceFile } from "../src/components/SourceFile.jsx";
4
4
 
5
5
  export function jsonTest(jsValue: unknown) {
6
- return <Output>
7
- <SourceFile path="test.json">
8
- <JsonValue jsValue={jsValue} />
9
- </SourceFile>
10
- </Output>;
6
+ return (
7
+ <Output>
8
+ <SourceFile path="test.json">
9
+ <JsonValue jsValue={jsValue} />
10
+ </SourceFile>
11
+ </Output>
12
+ );
11
13
  }
package/test/vitest.d.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import "vitest";
2
2
 
3
+ interface ToRenderToRenderOptions {
4
+ printWidth?: number;
5
+ tabWidth?: number;
6
+ useTabs?: boolean;
7
+ }
8
+
3
9
  interface CustomMatchers<R = unknown> {
4
- toRenderTo: (str: string) => R;
10
+ toRenderTo: (str: string, options?: ToRenderToRenderOptions) => R;
5
11
  }
6
12
 
7
13
  declare module "vitest" {
package/vitest.config.ts CHANGED
@@ -12,7 +12,7 @@ export default defineConfig({
12
12
  sourceMaps: "both",
13
13
  babelHelpers: "bundled",
14
14
  extensions: [".ts", ".tsx"],
15
- presets: ["@babel/preset-typescript", "@alloy-js/babel-preset"],
15
+ presets: ["@babel/preset-typescript", ["@alloy-js/babel-preset"]],
16
16
  }),
17
17
  ],
18
18
  });