Skip to content

Commit 60f4940

Browse files
committed
add test cases
1 parent 1035222 commit 60f4940

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

e2e/1-basic-schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@ type NamingConventionTest_Type {
8686
type NamingConventionTest_SubType {
8787
field: String!
8888
}
89+
90+
# NonOptionalFields
91+
type NonOptionalFields_OptionalFieldsType {
92+
field1: String!
93+
field2: String!
94+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type NonOptionalFields_NonOptionalFieldsType {
2+
field1: String!
3+
field2: String!
4+
}

e2e/codegen.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ const config: CodegenConfig = {
6969
typesSuffix: 'Suffix',
7070
},
7171
},
72+
'__generated__/4-non-optional-fields/types.ts': {
73+
schema: './4-non-optional-fields-schema.graphql',
74+
plugins: ['typescript'],
75+
config: {
76+
...defaultTypeScriptPluginConfig,
77+
},
78+
},
79+
'./__generated__/4-non-optional-fields/fabbrica.ts': {
80+
schema: './4-non-optional-fields-schema.graphql',
81+
plugins: ['@mizdra/graphql-fabbrica'],
82+
config: {
83+
...defaultFabbricaPluginConfig,
84+
nonOptionalFields: true,
85+
},
86+
},
7287
},
7388
};
7489

e2e/index.e2e.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import {
2121
defineNamingConventionTest_RenamedTypeFactory,
2222
defineNullableTest_TypeFactory,
2323
defineInputTest_InputFactory,
24+
defineNonOptionalFields_OptionalFieldsTypeFactory,
2425
} from './__generated__/1-basic/fabbrica.js';
2526
import { oneOf } from './test/util.js';
2627
import { definePrefixTypeFactory } from './__generated__/2-typesPrefix/fabbrica.js';
2728
import { defineTypeSuffixFactory } from './__generated__/3-typesSuffix/fabbrica.js';
29+
import { defineNonOptionalFields_NonOptionalFieldsTypeFactory } from './__generated__/4-non-optional-fields/fabbrica.js';
2830

2931
describe('integration test', () => {
3032
it('circular dependent type', async () => {
@@ -408,6 +410,34 @@ describe('defineTypeFactory', () => {
408410
expect(firstNameResolver).toHaveBeenCalledTimes(1);
409411
expect(lastNameResolver).toHaveBeenCalledTimes(1);
410412
});
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+
});
411441
});
412442
describe('traits', () => {
413443
it('overrides defaultFields', async () => {

0 commit comments

Comments
 (0)