diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ed37139..4892e91 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7211e8a..124f60e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -25,7 +25,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 061e856..dc43611 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 340e208..2826479 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: - name: Get yarn cache directory path id: yarn-cache-dir-path run: echo "::set-output name=dir::$(yarn cache dir)" - - uses: actions/cache@v2 + - uses: actions/cache@v4 with: path: ${{ steps.yarn-cache-dir-path.outputs.dir }} key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} diff --git a/README.md b/README.md index e366dd3..1d6f757 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ keep all GraphQL names as-is. Available case functions in `change-case-all` are `localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst` [See more](https://github.com/btxtiger/change-case-all) -### scalars (`{ [Scalar: string]: GeneratorOptions }`, defaultValue: `undefined`) +### scalars (`{ [Scalar: string]: GeneratorOptions | InputOutputGeneratorOptions }`, defaultValue: `undefined`) Allows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a [casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or @@ -371,6 +371,24 @@ fieldName: # gets translated to casual.integer.toFixed(3) arguments: 3 ``` +### `InputOutputGeneratorOptions` type + +This type is used in the `scalars` option. It allows you to specify different `GeneratorOptions` for `input` and `output` types for +your scalars, in the same way the [typescript-operations](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations#scalars) plugin does. + +So, using the first example of the previous section, you can specify a `string` for your input and a `Date` for your `output`: + +```yaml +plugins: + - typescript-mock-data: + scalars: + Date: + input: date.weekday # Date fields in input objects will be mocked as strings + output: + generator: date.past # Date fields in other GraphQL types will be mocked as JS Dates + arguments: 10 +``` + ## Examples of usage **codegen.yml** diff --git a/src/index.ts b/src/index.ts index b46a01a..4811c19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ type NamingConvention = 'change-case-all#pascalCase' | 'keep' | string; type Options = { typeName: string; fieldName: string; + generatorMode: 'input' | 'output'; types: TypeItem[]; typeNamesConvention: NamingConvention; enumValuesConvention: NamingConvention; @@ -90,14 +91,26 @@ const hashedString = (value: string) => { return hash; }; -const getGeneratorDefinition = (value: GeneratorDefinition | GeneratorName): GeneratorDefinition => { - if (typeof value === 'string') { +const getGeneratorDefinition = ( + opts: GeneratorOptions | InputOutputGeneratorOptions, + generatorMode: Options['generatorMode'], +): GeneratorDefinition => { + if (isAnInputOutputGeneratorOptions(opts)) { + return buildGeneratorDefinition(opts[generatorMode]); + } + + return buildGeneratorDefinition(opts); +}; + +const buildGeneratorDefinition = (opts: GeneratorOptions) => { + if (typeof opts === 'string') { return { - generator: value, + generator: opts, arguments: [], }; } - return value; + + return opts; }; const getCasualCustomValue = ( @@ -246,12 +259,18 @@ const handleValueGeneration = ( if (opts.fieldGeneration) { // Check for a specific generation for the type & field if (opts.typeName in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration[opts.typeName]) { - const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration[opts.typeName][opts.fieldName]); + const generatorDefinition = getGeneratorDefinition( + opts.fieldGeneration[opts.typeName][opts.fieldName], + opts.generatorMode, + ); return getCustomValue(generatorDefinition, opts); } // Check for a general field generation definition if ('_all' in opts.fieldGeneration && opts.fieldName in opts.fieldGeneration['_all']) { - const generatorDefinition = getGeneratorDefinition(opts.fieldGeneration['_all'][opts.fieldName]); + const generatorDefinition = getGeneratorDefinition( + opts.fieldGeneration['_all'][opts.fieldName], + opts.generatorMode, + ); return getCustomValue(generatorDefinition, opts); } } @@ -290,25 +309,36 @@ const getNamedType = (opts: Options): if (!opts.dynamicValues) mockValueGenerator.seed(hashedString(opts.typeName + opts.fieldName)); const name = opts.currentType.name.value; const casedName = createNameConverter(opts.typeNamesConvention, opts.transformUnderscore)(name); + switch (name) { case 'String': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['String']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['String'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.word); } case 'Float': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Float']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Float'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.float); } case 'ID': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['ID']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['ID'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.uuid); } case 'Boolean': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Boolean']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Boolean'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.boolean); } case 'Int': { - const customScalar = opts.customScalars ? getGeneratorDefinition(opts.customScalars['Int']) : null; + const customScalar = opts.customScalars + ? getGeneratorDefinition(opts.customScalars['Int'], opts.generatorMode) + : null; return handleValueGeneration(opts, customScalar, mockValueGenerator.integer); } default: { @@ -345,7 +375,7 @@ const getNamedType = (opts: Options): }); case 'scalar': { const customScalar = opts.customScalars - ? getGeneratorDefinition(opts.customScalars[foundType.name]) + ? getGeneratorDefinition(opts.customScalars[foundType.name], opts.generatorMode) : null; // it's a scalar, let's use a string as a value if there is no custom @@ -559,9 +589,18 @@ type GeneratorDefinition = { }; }; type GeneratorOptions = GeneratorName | GeneratorDefinition; +type InputOutputGeneratorOptions = { + input: GeneratorOptions; + output: GeneratorOptions; +}; + +const isAnInputOutputGeneratorOptions = ( + opts: GeneratorOptions | InputOutputGeneratorOptions, +): opts is InputOutputGeneratorOptions => + opts !== undefined && typeof opts !== 'string' && 'input' in opts && 'output' in opts; type ScalarMap = { - [name: string]: GeneratorOptions; + [name: string]: GeneratorOptions | InputOutputGeneratorOptions; }; type TypeFieldMap = { @@ -728,6 +767,7 @@ export const plugin: PluginFunction = (schema, docu const value = generateMockValue({ typeName, fieldName, + generatorMode: 'output', currentType: node.type, ...sharedGenerateMockOpts, }); @@ -753,6 +793,7 @@ export const plugin: PluginFunction = (schema, docu typeName: fieldName, fieldName: field.name.value, currentType: field.type, + generatorMode: 'input', ...sharedGenerateMockOpts, }); @@ -764,6 +805,7 @@ export const plugin: PluginFunction = (schema, docu typeName: fieldName, fieldName: field.name.value, currentType: field.type, + generatorMode: 'input', ...sharedGenerateMockOpts, }); diff --git a/tests/scalars/__snapshots__/spec.ts.snap b/tests/scalars/__snapshots__/spec.ts.snap index 4a01a65..e8b9083 100644 --- a/tests/scalars/__snapshots__/spec.ts.snap +++ b/tests/scalars/__snapshots__/spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should generate custom scalars for native and custom types using casual 1`] = ` +exports[`Custom scalar generation using casual should generate custom scalars for native and custom types 1`] = ` " export const anA = (overrides?: Partial): A => { return { @@ -18,31 +18,75 @@ export const aB = (overrides?: Partial): B => { bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, }; }; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Gianni_Kutch@hotmail.com', + }; +}; +" +`; + +exports[`Custom scalar generation using casual should generate dynamic custom scalars for native and custom types 1`] = ` +"import casual from 'casual'; + +casual.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'], + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'], + }; +}; + +export const seedMocks = (seed: number) => casual.seed(seed); " `; -exports[`should generate custom scalars for native and custom types using faker 1`] = ` +exports[`Custom scalar generation using casual with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = ` " export const anA = (overrides?: Partial): A => { return { - id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, - str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis', obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), - anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com', + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Kelly_Cremin@Turcotte.biz', }; }; export const aB = (overrides?: Partial): B => { return { int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, - flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51, + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564, bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, }; }; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos', + }; +}; " `; -exports[`should generate dynamic custom scalars for native and custom types using casual 1`] = ` +exports[`Custom scalar generation using casual with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 1`] = ` "import casual from 'casual'; casual.seed(0); @@ -64,11 +108,103 @@ export const aB = (overrides?: Partial): B => { }; }; +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'], + }; +}; + export const seedMocks = (seed: number) => casual.seed(seed); " `; -exports[`should generate dynamic custom scalars for native and custom types using faker 1`] = ` +exports[`custom scalar generation using faker should generate custom scalars for native and custom types 1`] = ` +" +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com', + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51, + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Maia49@hotmail.com', + }; +}; +" +`; + +exports[`custom scalar generation using faker should generate dynamic custom scalars for native and custom types 1`] = ` +"import { fakerEN as faker } from '@faker-js/faker'; + +faker.seed(0); + +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\\"min\\":1,\\"max\\":100}]), + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](), + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](), + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\\"min\\":-100,\\"max\\":0}]), + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\\"min\\":-100,\\"max\\":0}]), + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](), + }; +}; + +export const seedMocks = (seed: number) => faker.seed(seed); +" +`; + +exports[`custom scalar generation using faker with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = ` +" +export const anA = (overrides?: Partial): A => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83, + str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.', + obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(), + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com', + }; +}; + +export const aB = (overrides?: Partial): B => { + return { + int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93, + flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51, + bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false, + }; +}; + +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus', + }; +}; +" +`; + +exports[`custom scalar generation using faker with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 1`] = ` "import { fakerEN as faker } from '@faker-js/faker'; faker.seed(0); @@ -90,6 +226,12 @@ export const aB = (overrides?: Partial): B => { }; }; +export const aC = (overrides?: Partial): C => { + return { + anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](), + }; +}; + export const seedMocks = (seed: number) => faker.seed(seed); " `; diff --git a/tests/scalars/schema.ts b/tests/scalars/schema.ts index c470b74..6c4469f 100644 --- a/tests/scalars/schema.ts +++ b/tests/scalars/schema.ts @@ -15,4 +15,8 @@ export default buildSchema(/* GraphQL */ ` flt: Float! bool: Boolean! } + + input C { + anyObject: AnyObject! + } `); diff --git a/tests/scalars/spec.ts b/tests/scalars/spec.ts index ce9f4dd..4047d93 100644 --- a/tests/scalars/spec.ts +++ b/tests/scalars/spec.ts @@ -1,192 +1,446 @@ import { plugin } from '../../src'; import testSchema from './schema'; -it('should generate custom scalars for native and custom types using casual', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'casual', - scalars: { - String: 'string', - Float: { - generator: 'double', - arguments: [-100, 0], +describe('Custom scalar generation using casual', () => { + it('should generate custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: 'email', }, - ID: { - generator: 'integer', - arguments: [1, 100], - }, - Boolean: 'false', - Int: { - generator: 'integer', - arguments: [-100, 0], - }, - AnyObject: 'email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", - ); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", + ); - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", - ); + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", + ); - // ID - expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); - expect(result).toMatchSnapshot(); -}); + expect(result).toMatchSnapshot(); + }); -it('should generate dynamic custom scalars for native and custom types using casual', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'casual', - dynamicValues: true, - scalars: { - String: 'string', - Float: { - generator: 'double', - arguments: [-100, 0], - }, - ID: { - generator: 'integer', - arguments: [1, 100], + it('should generate dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + dynamicValues: true, + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: 'email', }, - Boolean: 'false', - Int: { - generator: 'integer', - arguments: [-100, 0], - }, - AnyObject: 'email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain("str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],"); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],", + ); - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", - ); + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", + ); - // ID - expect(result).toContain( - "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),", - ); + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),", + ); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain( - "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),", - ); + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),", + ); - expect(result).toMatchSnapshot(); + expect(result).toMatchSnapshot(); + }); + + describe('with different input/output configurations', () => { + it('should generate distinct custom scalars for native and custom input/output types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: { + input: 'string', + output: 'email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,", + ); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Kelly_Cremin@Turcotte.biz',", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos',", + ); + + expect(result).toMatchSnapshot(); + }); + + it('should generate distinct dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'casual', + dynamicValues: true, + scalars: { + String: 'string', + Float: { + generator: 'double', + arguments: [-100, 0], + }, + ID: { + generator: 'integer', + arguments: [1, 100], + }, + Boolean: 'false', + Int: { + generator: 'integer', + arguments: [-100, 0], + }, + AnyObject: { + input: 'string', + output: 'email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),", + ); + + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),", + ); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),", + ); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'],", + ); + + // AnyObject in input C (an string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'],", + ); + + expect(result).toMatchSnapshot(); + }); + }); }); -it('should generate custom scalars for native and custom types using faker', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'faker', - scalars: { - String: 'lorem.sentence', - Float: { - generator: 'number.float', - arguments: [{ min: -100, max: 0, fractionDigits: 2 }], +describe('custom scalar generation using faker', () => { + it('should generate custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0, fractionDigits: 2 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: 'internet.email', }, - ID: { - generator: 'number.int', - arguments: [{ min: 1, max: 100 }], - }, - Boolean: 'false', - Int: { - generator: 'number.int', - arguments: [{ min: -100, max: 0 }], - }, - AnyObject: 'internet.email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", - ); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", + ); - // Float - expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,"); + // Float + expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,"); - // ID - expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,"); + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,"); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); - expect(result).toMatchSnapshot(); -}); + expect(result).toMatchSnapshot(); + }); -it('should generate dynamic custom scalars for native and custom types using faker', async () => { - const result = await plugin(testSchema, [], { - generateLibrary: 'faker', - dynamicValues: true, - scalars: { - String: 'lorem.sentence', - Float: { - generator: 'number.float', - arguments: [{ min: -100, max: 0 }], - }, - ID: { - generator: 'number.int', - arguments: [{ min: 1, max: 100 }], + it('should generate dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: 'internet.email', }, - Boolean: 'false', - Int: { - generator: 'number.int', - arguments: [{ min: -100, max: 0 }], - }, - AnyObject: 'internet.email', - }, - }); + }); - expect(result).toBeDefined(); + expect(result).toBeDefined(); - // String - expect(result).toContain( - "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),", - ); + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),", + ); - // Float - expect(result).toContain( - "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\"min\":-100,\"max\":0}]),", - ); + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\"min\":-100,\"max\":0}]),", + ); - // ID - expect(result).toContain( - "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),", - ); + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),", + ); - // Boolean - expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); - // Int - expect(result).toContain( - "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),", - ); + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),", + ); - expect(result).toMatchSnapshot(); + expect(result).toMatchSnapshot(); + }); + + describe('with different input/output configurations', () => { + it('should generate distinct custom scalars for native and custom input/output types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0, fractionDigits: 2 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: { + input: 'lorem.word', + output: 'internet.email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',", + ); + + // Float + expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,"); + + // ID + expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,"); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,"); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'Orlando_Cremin@gmail.com',", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus',", + ); + + expect(result).toMatchSnapshot(); + }); + + it('should generate distinct dynamic custom scalars for native and custom types', async () => { + const result = await plugin(testSchema, [], { + generateLibrary: 'faker', + dynamicValues: true, + scalars: { + String: 'lorem.sentence', + Float: { + generator: 'number.float', + arguments: [{ min: -100, max: 0 }], + }, + ID: { + generator: 'number.int', + arguments: [{ min: 1, max: 100 }], + }, + Boolean: 'false', + Int: { + generator: 'number.int', + arguments: [{ min: -100, max: 0 }], + }, + AnyObject: { + input: 'lorem.word', + output: 'internet.email', + }, + }, + }); + + expect(result).toBeDefined(); + + // String + expect(result).toContain( + "str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),", + ); + + // Float + expect(result).toContain( + "flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\"min\":-100,\"max\":0}]),", + ); + + // ID + expect(result).toContain( + "id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),", + ); + + // Boolean + expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false"); + + // Int + expect(result).toContain( + "int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),", + ); + + // AnyObject in type A (an email) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](),", + ); + + // AnyObject in input C (a string) + expect(result).toContain( + "anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](),", + ); + + expect(result).toMatchSnapshot(); + }); + }); });