@voidhash/mimic 1.0.0-beta.15 → 1.0.0-beta.16

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.
@@ -1036,6 +1036,116 @@ describe("StructPrimitive", () => {
1036
1036
  expect(operations[0]!.kind).toBe("string.set");
1037
1037
  expect(operations[0]!.payload).toBe("Jane");
1038
1038
  });
1039
+
1040
+ it("partial({ stripDefaults: true }) clears field defaults", () => {
1041
+ const structPrimitive = Primitive.Struct({
1042
+ paddingTop: Primitive.Number().default(0),
1043
+ name: Primitive.String().default("Anonymous"),
1044
+ age: Primitive.Number(),
1045
+ });
1046
+
1047
+ const partialPrimitive = structPrimitive.partial({ stripDefaults: true });
1048
+
1049
+ // getInitialState should return undefined since all defaults are stripped
1050
+ expect(partialPrimitive._internal.getInitialState()).toBeUndefined();
1051
+ });
1052
+
1053
+ it("partial({ stripDefaults: true }) toSnapshot returns undefined for unset fields", () => {
1054
+ const env = ProxyEnvironment.make(() => {});
1055
+
1056
+ const structPrimitive = Primitive.Struct({
1057
+ paddingTop: Primitive.Number().default(0),
1058
+ name: Primitive.String().default("test"),
1059
+ });
1060
+
1061
+ const partialPrimitive = structPrimitive.partial({ stripDefaults: true });
1062
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
1063
+
1064
+ // With no state set, toSnapshot should return undefined (no defaults to fall back on)
1065
+ expect(proxy.toSnapshot()).toBeUndefined();
1066
+ });
1067
+
1068
+ it("partial({ stripDefaults: true }) strips nested struct defaults", () => {
1069
+ const structPrimitive = Primitive.Struct({
1070
+ profile: Primitive.Struct({
1071
+ name: Primitive.String().default("Anonymous"),
1072
+ age: Primitive.Number().default(0),
1073
+ }),
1074
+ });
1075
+
1076
+ const partialPrimitive = structPrimitive.partial({ stripDefaults: true });
1077
+ expect(partialPrimitive._internal.getInitialState()).toBeUndefined();
1078
+
1079
+ const env = ProxyEnvironment.make(() => {});
1080
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
1081
+ expect(proxy.toSnapshot()).toBeUndefined();
1082
+ });
1083
+
1084
+ it("partial({ stripDefaults: true }) does not re-apply nested defaults during set()", () => {
1085
+ const operations: Operation.Operation<any, any, any>[] = [];
1086
+ const env = ProxyEnvironment.make((op) => {
1087
+ operations.push(op);
1088
+ });
1089
+
1090
+ const structPrimitive = Primitive.Struct({
1091
+ profile: Primitive.Struct({
1092
+ name: Primitive.String().default("Anonymous"),
1093
+ age: Primitive.Number().default(0),
1094
+ }),
1095
+ });
1096
+
1097
+ const partialPrimitive = structPrimitive.partial({ stripDefaults: true });
1098
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
1099
+
1100
+ proxy.set({ profile: {} });
1101
+
1102
+ expect(operations).toHaveLength(1);
1103
+ expect(operations[0]!.kind).toBe("struct.set");
1104
+ expect(operations[0]!.payload).toEqual({ profile: {} });
1105
+ });
1106
+
1107
+ it("partial({ stripDefaults: true }) keeps field proxies typed as possibly undefined", () => {
1108
+ const structPrimitive = Primitive.Struct({
1109
+ name: Primitive.String().default("Anonymous"),
1110
+ });
1111
+
1112
+ const partialPrimitive = structPrimitive.partial({ stripDefaults: true });
1113
+ const env = ProxyEnvironment.make(() => {});
1114
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
1115
+
1116
+ // Compile-time assertion: with defaults stripped, field access is optional.
1117
+ const value: string | undefined = proxy.name.get();
1118
+ expect(value).toBeUndefined();
1119
+ });
1120
+
1121
+ it("partial() without stripDefaults preserves field defaults", () => {
1122
+ const structPrimitive = Primitive.Struct({
1123
+ paddingTop: Primitive.Number().default(0),
1124
+ name: Primitive.String().default("Anonymous"),
1125
+ });
1126
+
1127
+ const partialPrimitive = structPrimitive.partial();
1128
+
1129
+ // getInitialState should still return the defaults
1130
+ expect(partialPrimitive._internal.getInitialState()).toEqual({
1131
+ paddingTop: 0,
1132
+ name: "Anonymous",
1133
+ });
1134
+ });
1135
+
1136
+ it("partial() without stripDefaults keeps field proxies typed as defined when defaults exist", () => {
1137
+ const structPrimitive = Primitive.Struct({
1138
+ name: Primitive.String().default("Anonymous"),
1139
+ });
1140
+
1141
+ const partialPrimitive = structPrimitive.partial();
1142
+ const env = ProxyEnvironment.make(() => {});
1143
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
1144
+
1145
+ // Compile-time assertion: without stripping defaults, field access remains defined.
1146
+ const value: string = proxy.name.get();
1147
+ expect(value).toBe("Anonymous");
1148
+ });
1039
1149
  });
1040
1150
  });
1041
1151