|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { createStateProxy, createSchemaProxy } from './proxy' |
| 3 | +import type { FieldConfig } from '@ybyra/core' |
| 4 | + |
| 5 | +function makeFieldConfig(overrides: Partial<FieldConfig> = {}): FieldConfig { |
| 6 | + return { |
| 7 | + component: 'text', |
| 8 | + dataType: 'string', |
| 9 | + attrs: {}, |
| 10 | + form: { width: 100, height: 1, hidden: false, disabled: false, order: 0 }, |
| 11 | + table: { show: false, width: 'auto', sortable: false, filterable: false, order: 0 }, |
| 12 | + validations: [], |
| 13 | + scopes: null, |
| 14 | + states: [], |
| 15 | + defaultValue: undefined, |
| 16 | + ...overrides, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('createStateProxy', () => { |
| 21 | + it('reads existing values from snapshot', () => { |
| 22 | + const snapshot = { name: 'Alice', age: 30 } |
| 23 | + const { proxy } = createStateProxy(snapshot) |
| 24 | + expect(proxy.name).toBe('Alice') |
| 25 | + expect(proxy.age).toBe(30) |
| 26 | + }) |
| 27 | + |
| 28 | + it('tracks mutations via set', () => { |
| 29 | + const { proxy, getChanges } = createStateProxy({ name: 'Alice' }) |
| 30 | + proxy.name = 'Bob' |
| 31 | + expect(proxy.name).toBe('Bob') |
| 32 | + expect(getChanges()).toEqual({ name: 'Bob' }) |
| 33 | + }) |
| 34 | + |
| 35 | + it('getChanges returns only modified fields', () => { |
| 36 | + const { proxy, getChanges } = createStateProxy({ name: 'Alice', age: 30 }) |
| 37 | + proxy.name = 'Bob' |
| 38 | + const changes = getChanges() |
| 39 | + expect(changes).toEqual({ name: 'Bob' }) |
| 40 | + expect(changes).not.toHaveProperty('age') |
| 41 | + }) |
| 42 | + |
| 43 | + it('getChanges returns empty object when nothing changed', () => { |
| 44 | + const { getChanges } = createStateProxy({ name: 'Alice' }) |
| 45 | + expect(getChanges()).toEqual({}) |
| 46 | + }) |
| 47 | + |
| 48 | + it('does not mutate the original snapshot object', () => { |
| 49 | + const snapshot = { name: 'Alice', age: 30 } |
| 50 | + const { proxy } = createStateProxy(snapshot) |
| 51 | + proxy.name = 'Bob' |
| 52 | + proxy.age = 99 |
| 53 | + expect(snapshot.name).toBe('Alice') |
| 54 | + expect(snapshot.age).toBe(30) |
| 55 | + }) |
| 56 | + |
| 57 | + it('multiple sets to same key keeps latest value', () => { |
| 58 | + const { proxy, getChanges } = createStateProxy({ name: 'Alice' }) |
| 59 | + proxy.name = 'Bob' |
| 60 | + proxy.name = 'Charlie' |
| 61 | + expect(proxy.name).toBe('Charlie') |
| 62 | + expect(getChanges()).toEqual({ name: 'Charlie' }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('handles setting value to undefined', () => { |
| 66 | + const { proxy, getChanges } = createStateProxy({ name: 'Alice' }) |
| 67 | + proxy.name = undefined |
| 68 | + expect(proxy.name).toBeUndefined() |
| 69 | + expect(getChanges()).toEqual({ name: undefined }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('handles setting value to null', () => { |
| 73 | + const { proxy, getChanges } = createStateProxy({ name: 'Alice' }) |
| 74 | + proxy.name = null |
| 75 | + expect(proxy.name).toBeNull() |
| 76 | + expect(getChanges()).toEqual({ name: null }) |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +// Known issue: structuredClone does not reliably clone RegExp objects inside |
| 81 | +// ValidationRule params. If createSchemaProxy is used with structuredClone |
| 82 | +// upstream (e.g., via toConfig()), RegExp-based pattern validations may throw. |
| 83 | +// This is an upstream concern in @ybyra/core, not a proxy behavior issue — see |
| 84 | +// TextFieldDefinition.pattern() which stores a raw RegExp in validation params. |
| 85 | + |
| 86 | +describe('createSchemaProxy', () => { |
| 87 | + it('provides field proxies with defaults from config', () => { |
| 88 | + const fields = { |
| 89 | + name: makeFieldConfig({ |
| 90 | + form: { width: 50, height: 2, hidden: false, disabled: false, order: 0 }, |
| 91 | + }), |
| 92 | + } |
| 93 | + const { proxy } = createSchemaProxy(fields, {}) |
| 94 | + expect(proxy.name.width).toBe(50) |
| 95 | + expect(proxy.name.height).toBe(2) |
| 96 | + expect(proxy.name.hidden).toBe(false) |
| 97 | + expect(proxy.name.disabled).toBe(false) |
| 98 | + expect(proxy.name.state).toBe('') |
| 99 | + }) |
| 100 | + |
| 101 | + it('merges currentOverrides into field defaults', () => { |
| 102 | + const fields = { |
| 103 | + name: makeFieldConfig({ |
| 104 | + form: { width: 50, height: 2, hidden: false, disabled: false, order: 0 }, |
| 105 | + }), |
| 106 | + } |
| 107 | + const overrides = { name: { hidden: true, width: 75 } } |
| 108 | + const { proxy } = createSchemaProxy(fields, overrides) |
| 109 | + expect(proxy.name.hidden).toBe(true) |
| 110 | + expect(proxy.name.width).toBe(75) |
| 111 | + // Non-overridden properties keep config defaults |
| 112 | + expect(proxy.name.height).toBe(2) |
| 113 | + expect(proxy.name.disabled).toBe(false) |
| 114 | + }) |
| 115 | + |
| 116 | + it('tracks field property mutations', () => { |
| 117 | + const fields = { name: makeFieldConfig() } |
| 118 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 119 | + proxy.name.hidden = true |
| 120 | + expect(getOverrides()).toEqual({ name: { hidden: true } }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('getOverrides returns only modified fields', () => { |
| 124 | + const fields = { |
| 125 | + name: makeFieldConfig(), |
| 126 | + age: makeFieldConfig(), |
| 127 | + } |
| 128 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 129 | + proxy.name.hidden = true |
| 130 | + const overrides = getOverrides() |
| 131 | + expect(overrides).toHaveProperty('name') |
| 132 | + expect(overrides).not.toHaveProperty('age') |
| 133 | + }) |
| 134 | + |
| 135 | + it('getOverrides returns empty object when nothing changed', () => { |
| 136 | + const fields = { name: makeFieldConfig() } |
| 137 | + const { getOverrides } = createSchemaProxy(fields, {}) |
| 138 | + expect(getOverrides()).toEqual({}) |
| 139 | + }) |
| 140 | + |
| 141 | + it('setting hidden on a field appears in overrides', () => { |
| 142 | + const fields = { name: makeFieldConfig() } |
| 143 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 144 | + proxy.name.hidden = true |
| 145 | + expect(getOverrides().name?.hidden).toBe(true) |
| 146 | + }) |
| 147 | + |
| 148 | + it('setting width on a field appears in overrides', () => { |
| 149 | + const fields = { name: makeFieldConfig() } |
| 150 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 151 | + proxy.name.width = 200 |
| 152 | + expect(getOverrides().name?.width).toBe(200) |
| 153 | + }) |
| 154 | + |
| 155 | + it('setting disabled on a field appears in overrides', () => { |
| 156 | + const fields = { name: makeFieldConfig() } |
| 157 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 158 | + proxy.name.disabled = true |
| 159 | + expect(getOverrides().name?.disabled).toBe(true) |
| 160 | + }) |
| 161 | + |
| 162 | + it('setting state on a field appears in overrides', () => { |
| 163 | + const fields = { name: makeFieldConfig() } |
| 164 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 165 | + proxy.name.state = 'editing' |
| 166 | + expect(getOverrides().name?.state).toBe('editing') |
| 167 | + }) |
| 168 | + |
| 169 | + it('multiple fields can be overridden independently', () => { |
| 170 | + const fields = { |
| 171 | + name: makeFieldConfig(), |
| 172 | + age: makeFieldConfig(), |
| 173 | + } |
| 174 | + const { proxy, getOverrides } = createSchemaProxy(fields, {}) |
| 175 | + proxy.name.hidden = true |
| 176 | + proxy.age.disabled = true |
| 177 | + const overrides = getOverrides() |
| 178 | + expect(overrides.name).toEqual({ hidden: true }) |
| 179 | + expect(overrides.age).toEqual({ disabled: true }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments