@@ -21,10 +21,12 @@ import {
21
21
defineNamingConventionTest_RenamedTypeFactory ,
22
22
defineNullableTest_TypeFactory ,
23
23
defineInputTest_InputFactory ,
24
+ defineNonOptionalFields_OptionalFieldsTypeFactory ,
24
25
} from './__generated__/1-basic/fabbrica.js' ;
25
26
import { oneOf } from './test/util.js' ;
26
27
import { definePrefixTypeFactory } from './__generated__/2-typesPrefix/fabbrica.js' ;
27
28
import { defineTypeSuffixFactory } from './__generated__/3-typesSuffix/fabbrica.js' ;
29
+ import { defineNonOptionalFields_NonOptionalFieldsTypeFactory } from './__generated__/4-non-optional-fields/fabbrica.js' ;
28
30
29
31
describe ( 'integration test' , ( ) => {
30
32
it ( 'circular dependent type' , async ( ) => {
@@ -408,6 +410,34 @@ describe('defineTypeFactory', () => {
408
410
expect ( firstNameResolver ) . toHaveBeenCalledTimes ( 1 ) ;
409
411
expect ( lastNameResolver ) . toHaveBeenCalledTimes ( 1 ) ;
410
412
} ) ;
413
+ describe ( 'nonOptionalFields' , ( ) => {
414
+ it ( 'requires to pass all fields if nonOptionalFields is false' , async ( ) => {
415
+ defineNonOptionalFields_NonOptionalFieldsTypeFactory ( {
416
+ // @ts -expect-error -- expects error
417
+ defaultFields : {
418
+ field1 : 'field1' ,
419
+ // field2: 'field2',
420
+ } ,
421
+ } ) ;
422
+ } ) ;
423
+ it ( 'requires to pass all fields if nonOptionalFields is true' , async ( ) => {
424
+ const TypeFactory = defineNonOptionalFields_OptionalFieldsTypeFactory ( {
425
+ defaultFields : {
426
+ field1 : 'field1' ,
427
+ // field2: 'field2',
428
+ } ,
429
+ } ) ;
430
+ // field2 is not included if it is not passed to `defaultFields` or `build`.
431
+ const type1 = await TypeFactory . build ( ) ;
432
+ expect ( type1 ) . toStrictEqual ( { field1 : 'field1' } ) ;
433
+ expectTypeOf ( type1 ) . toEqualTypeOf < { field1 : string } > ( ) ;
434
+
435
+ // field2 is included if it is passed to `defaultFields` or `build`.
436
+ const type2 = await TypeFactory . build ( { field2 : 'field2' } ) ;
437
+ expect ( type2 ) . toStrictEqual ( { field1 : 'field1' , field2 : 'field2' } ) ;
438
+ expectTypeOf ( type2 ) . toEqualTypeOf < { field1 : string ; field2 : string } > ( ) ;
439
+ } ) ;
440
+ } ) ;
411
441
} ) ;
412
442
describe ( 'traits' , ( ) => {
413
443
it ( 'overrides defaultFields' , async ( ) => {
0 commit comments