|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { LoroDoc } from "loro-crdt"; |
| 3 | +import { Mirror, schema } from "../src/index.js"; |
| 4 | + |
| 5 | +describe("undefined values in Map should be treated as non-existent fields", () => { |
| 6 | + it("should ignore undefined fields when setting state", async () => { |
| 7 | + const testSchema = schema({ |
| 8 | + root: schema.LoroMap({ |
| 9 | + name: schema.String(), |
| 10 | + age: schema.Number(), |
| 11 | + }), |
| 12 | + }); |
| 13 | + |
| 14 | + const doc = new LoroDoc(); |
| 15 | + const mirror = new Mirror({ |
| 16 | + doc, |
| 17 | + schema: testSchema, |
| 18 | + checkStateConsistency: true, |
| 19 | + }); |
| 20 | + |
| 21 | + // Try to set a field to undefined - this should be treated as if the field doesn't exist |
| 22 | + expect(() => { |
| 23 | + mirror.setState({ |
| 24 | + root: { |
| 25 | + name: "test", |
| 26 | + age: undefined, // This undefined should be ignored |
| 27 | + }, |
| 28 | + } as any); |
| 29 | + }).not.toThrow(); |
| 30 | + |
| 31 | + const state = mirror.getState() as any; |
| 32 | + |
| 33 | + // The name field should be set |
| 34 | + expect(state.root.name).toBe("test"); |
| 35 | + // The age field should not exist in the state (key should not be present) |
| 36 | + expect("age" in state.root).toBe(false); |
| 37 | + expect(state.root.age).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should ignore undefined fields in nested maps", async () => { |
| 41 | + const testSchema = schema({ |
| 42 | + root: schema.LoroMap({ |
| 43 | + user: schema.LoroMap({ |
| 44 | + name: schema.String(), |
| 45 | + email: schema.String(), |
| 46 | + }), |
| 47 | + }), |
| 48 | + }); |
| 49 | + |
| 50 | + const doc = new LoroDoc(); |
| 51 | + const mirror = new Mirror({ |
| 52 | + doc, |
| 53 | + schema: testSchema, |
| 54 | + checkStateConsistency: true, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(() => { |
| 58 | + mirror.setState({ |
| 59 | + root: { |
| 60 | + user: { |
| 61 | + name: "John", |
| 62 | + email: undefined, // Should be ignored |
| 63 | + }, |
| 64 | + }, |
| 65 | + } as any); |
| 66 | + }).not.toThrow(); |
| 67 | + |
| 68 | + const state = mirror.getState() as any; |
| 69 | + expect(state.root.user.name).toBe("John"); |
| 70 | + // The email field should not exist in the state |
| 71 | + expect("email" in state.root.user).toBe(false); |
| 72 | + expect(state.root.user.email).toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should handle undefined in schema.Any fields", async () => { |
| 76 | + const testSchema = schema({ |
| 77 | + root: schema.LoroMap({ |
| 78 | + data: schema.Any(), |
| 79 | + }), |
| 80 | + }); |
| 81 | + |
| 82 | + const doc = new LoroDoc(); |
| 83 | + const mirror = new Mirror({ |
| 84 | + doc, |
| 85 | + schema: testSchema, |
| 86 | + checkStateConsistency: true, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(() => { |
| 90 | + mirror.setState({ |
| 91 | + root: { |
| 92 | + data: { |
| 93 | + field1: "value", |
| 94 | + field2: undefined, // Should be ignored |
| 95 | + }, |
| 96 | + }, |
| 97 | + } as any); |
| 98 | + }).not.toThrow(); |
| 99 | + |
| 100 | + const state = mirror.getState() as any; |
| 101 | + expect(state.root.data.field1).toBe("value"); |
| 102 | + // The field2 should not exist in the state |
| 103 | + expect("field2" in state.root.data).toBe(false); |
| 104 | + expect(state.root.data.field2).toBeUndefined(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should delete existing field when set to undefined", async () => { |
| 108 | + const testSchema = schema({ |
| 109 | + root: schema.LoroMap({ |
| 110 | + name: schema.String(), |
| 111 | + age: schema.Number(), |
| 112 | + }), |
| 113 | + }); |
| 114 | + |
| 115 | + const doc = new LoroDoc(); |
| 116 | + const mirror = new Mirror({ |
| 117 | + doc, |
| 118 | + schema: testSchema, |
| 119 | + checkStateConsistency: true, |
| 120 | + }); |
| 121 | + |
| 122 | + // First set both fields |
| 123 | + mirror.setState({ |
| 124 | + root: { |
| 125 | + name: "test", |
| 126 | + age: 25, |
| 127 | + }, |
| 128 | + } as any); |
| 129 | + |
| 130 | + let state = mirror.getState() as any; |
| 131 | + expect(state.root.name).toBe("test"); |
| 132 | + expect(state.root.age).toBe(25); |
| 133 | + expect("age" in state.root).toBe(true); |
| 134 | + |
| 135 | + // Now set age to undefined - it should be deleted |
| 136 | + mirror.setState({ |
| 137 | + root: { |
| 138 | + name: "test", |
| 139 | + age: undefined, |
| 140 | + }, |
| 141 | + } as any); |
| 142 | + |
| 143 | + state = mirror.getState() as any; |
| 144 | + expect(state.root.name).toBe("test"); |
| 145 | + // The age field should be deleted |
| 146 | + expect("age" in state.root).toBe(false); |
| 147 | + expect(state.root.age).toBeUndefined(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments