|
1 | 1 | import { createSchemaUtils, Experimental_DefaultFormStateBehavior, getDefaultFormState, RJSFSchema } from '../../src';
|
2 | 2 | import {
|
3 | 3 | AdditionalItemsHandling,
|
| 4 | + computeDefaultBasedOnSchemaTypeAndDefaults, |
4 | 5 | computeDefaults,
|
5 | 6 | getArrayDefaults,
|
6 | 7 | getDefaultBasedOnSchemaType,
|
@@ -1969,7 +1970,119 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
|
1969 | 1970 | ]);
|
1970 | 1971 | });
|
1971 | 1972 | });
|
1972 |
| - |
| 1973 | + describe('computeDefaultBasedOnSchemaTypeAndDefaults()', () => { |
| 1974 | + let schema: RJSFSchema; |
| 1975 | + describe('Object', () => { |
| 1976 | + beforeAll(() => { |
| 1977 | + schema = { |
| 1978 | + type: 'object', |
| 1979 | + default: null, |
| 1980 | + }; |
| 1981 | + }); |
| 1982 | + it('computedDefaults is undefined', () => { |
| 1983 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeUndefined(); |
| 1984 | + }); |
| 1985 | + it('computedDefaults is empty object', () => { |
| 1986 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, {})).toEqual({}); |
| 1987 | + }); |
| 1988 | + it('computedDefaults is non-empty object', () => { |
| 1989 | + const computedDefault = { foo: 'bar' }; |
| 1990 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 1991 | + }); |
| 1992 | + }); |
| 1993 | + describe('Nullable Object', () => { |
| 1994 | + beforeAll(() => { |
| 1995 | + schema = { |
| 1996 | + type: ['null', 'object'], |
| 1997 | + default: null, |
| 1998 | + }; |
| 1999 | + }); |
| 2000 | + it('computedDefaults is undefined', () => { |
| 2001 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeNull(); |
| 2002 | + }); |
| 2003 | + it('computedDefaults is empty object', () => { |
| 2004 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, {})).toBeNull(); |
| 2005 | + }); |
| 2006 | + it('computedDefaults is non-empty object', () => { |
| 2007 | + const computedDefault = { foo: 'bar' }; |
| 2008 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 2009 | + }); |
| 2010 | + }); |
| 2011 | + describe('Array', () => { |
| 2012 | + beforeAll(() => { |
| 2013 | + schema = { |
| 2014 | + type: 'array', |
| 2015 | + default: null, |
| 2016 | + items: { type: 'string' }, |
| 2017 | + }; |
| 2018 | + }); |
| 2019 | + it('computedDefaults is undefined', () => { |
| 2020 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeUndefined(); |
| 2021 | + }); |
| 2022 | + it('computedDefaults is empty object', () => { |
| 2023 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, [])).toEqual([]); |
| 2024 | + }); |
| 2025 | + it('computedDefaults is non-empty object', () => { |
| 2026 | + const computedDefault = ['bar']; |
| 2027 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 2028 | + }); |
| 2029 | + }); |
| 2030 | + describe('Nullable Array', () => { |
| 2031 | + beforeAll(() => { |
| 2032 | + schema = { |
| 2033 | + type: ['null', 'array'], |
| 2034 | + default: null, |
| 2035 | + items: { type: 'string' }, |
| 2036 | + }; |
| 2037 | + }); |
| 2038 | + it('computedDefaults is undefined', () => { |
| 2039 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeNull(); |
| 2040 | + }); |
| 2041 | + it('computedDefaults is empty object', () => { |
| 2042 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, [])).toBeNull(); |
| 2043 | + }); |
| 2044 | + it('computedDefaults is non-empty object', () => { |
| 2045 | + const computedDefault = ['bar']; |
| 2046 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 2047 | + }); |
| 2048 | + }); |
| 2049 | + describe('Nullable String', () => { |
| 2050 | + beforeAll(() => { |
| 2051 | + schema = { |
| 2052 | + type: 'string', |
| 2053 | + default: null, |
| 2054 | + }; |
| 2055 | + }); |
| 2056 | + it('computedDefaults is undefined', () => { |
| 2057 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeUndefined(); |
| 2058 | + }); |
| 2059 | + it('computedDefaults is empty object', () => { |
| 2060 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, '')).toEqual(''); |
| 2061 | + }); |
| 2062 | + it('computedDefaults is non-empty object', () => { |
| 2063 | + const computedDefault = 'bar'; |
| 2064 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 2065 | + }); |
| 2066 | + }); |
| 2067 | + describe('Nullable String', () => { |
| 2068 | + beforeAll(() => { |
| 2069 | + schema = { |
| 2070 | + type: ['null', 'string'], |
| 2071 | + default: null, |
| 2072 | + }; |
| 2073 | + }); |
| 2074 | + it('computedDefaults is undefined', () => { |
| 2075 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, undefined)).toBeNull(); |
| 2076 | + }); |
| 2077 | + it('computedDefaults is empty object', () => { |
| 2078 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, '')).toBeNull(); |
| 2079 | + }); |
| 2080 | + it('computedDefaults is non-empty object', () => { |
| 2081 | + const computedDefault = 'bar'; |
| 2082 | + expect(computeDefaultBasedOnSchemaTypeAndDefaults(schema, computedDefault)).toEqual(computedDefault); |
| 2083 | + }); |
| 2084 | + }); |
| 2085 | + }); |
1973 | 2086 | describe('getValidFormData', () => {
|
1974 | 2087 | let schema: RJSFSchema;
|
1975 | 2088 | it('Test schema with non valid formData for enum property', () => {
|
@@ -5091,12 +5204,12 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
|
5091 | 5204 | expect(Array.isArray(result)).toBe(true);
|
5092 | 5205 |
|
5093 | 5206 | // Verify objects are independent instances
|
5094 |
| - (result[0] as any).field = 'test-value-1'; |
5095 |
| - (result[1] as any).field = 'test-value-2'; |
5096 |
| - expect((result[2] as any).field).toBeUndefined(); |
5097 |
| - expect(result[0]).not.toBe(result[1]); |
5098 |
| - expect(result[1]).not.toBe(result[2]); |
5099 |
| - expect(result[0]).not.toBe(result[2]); |
| 5207 | + (result![0] as any).field = 'test-value-1'; |
| 5208 | + (result![1] as any).field = 'test-value-2'; |
| 5209 | + expect((result![2] as any).field).toBeUndefined(); |
| 5210 | + expect(result![0]).not.toBe(result![1]); |
| 5211 | + expect(result![1]).not.toBe(result![2]); |
| 5212 | + expect(result![0]).not.toBe(result![2]); |
5100 | 5213 | });
|
5101 | 5214 |
|
5102 | 5215 | it('should ensure array items with default values are independent instances', () => {
|
@@ -5125,9 +5238,9 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
|
5125 | 5238 | expect(Array.isArray(result)).toBe(true);
|
5126 | 5239 |
|
5127 | 5240 | // Verify objects are independent instances - modifying one shouldn't affect the other
|
5128 |
| - (result[0] as any).field = 'modified-value'; |
5129 |
| - expect((result[1] as any).field).toBe('default-value'); |
5130 |
| - expect(result[0]).not.toBe(result[1]); |
| 5241 | + (result![0] as any).field = 'modified-value'; |
| 5242 | + expect((result![1] as any).field).toBe('default-value'); |
| 5243 | + expect(result![0]).not.toBe(result![1]); |
5131 | 5244 | });
|
5132 | 5245 |
|
5133 | 5246 | it('should ensure nested objects in arrays are independent instances', () => {
|
@@ -5164,10 +5277,10 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
|
5164 | 5277 | expect(Array.isArray(result)).toBe(true);
|
5165 | 5278 |
|
5166 | 5279 | // Verify nested objects are independent instances
|
5167 |
| - (result[0] as any).nested.value = 'modified-nested-value'; |
5168 |
| - expect((result[1] as any).nested.value).toBe('nested-default'); |
5169 |
| - expect(result[0]).not.toBe(result[1]); |
5170 |
| - expect((result[0] as any).nested).not.toBe((result[1] as any).nested); |
| 5280 | + (result![0] as any).nested.value = 'modified-nested-value'; |
| 5281 | + expect((result![1] as any).nested.value).toBe('nested-default'); |
| 5282 | + expect(result![0]).not.toBe(result![1]); |
| 5283 | + expect((result![0] as any).nested).not.toBe((result![1] as any).nested); |
5171 | 5284 | });
|
5172 | 5285 | });
|
5173 | 5286 | });
|
|
0 commit comments