diff --git a/README.md b/README.md index 8558e68..4d4a926 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,10 @@ export const aUser = (overrides?: Partial): User => { } ``` +### transformUnderscore (`boolean`, defaultValue: `true`) + +When disabled, underscores will be retained for type names when the case is changed. It has no effect if `typenames` is set to `keep`. + ## Example of usage **codegen.yml** diff --git a/src/index.ts b/src/index.ts index 00146cd..c9dee22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,21 +20,34 @@ type Options = { enumsPrefix: string; currentType: T; customScalars?: ScalarMap; + transformUnderscore: boolean; +}; + +const convertName = (value: string, fn: (v: string) => string, transformUnderscore: boolean): string => { + if (transformUnderscore) { + return fn(value); + } + + return value + .split('_') + .map((s) => fn(s)) + .join('_'); }; const createNameConverter = - (convention: NamingConvention) => + (convention: NamingConvention, transformUnderscore: boolean) => (value: string, prefix = '') => { switch (convention) { - case 'upper-case#upperCase': - return `${prefix}${upperCase(value || '')}`; + case 'upper-case#upperCase': { + return `${prefix}${convertName(value, (s) => upperCase(s || ''), transformUnderscore)}`; + } case 'keep': return `${prefix}${value}`; case 'pascal-case#pascalCase': // fallthrough default: // default to pascal case in case of unknown values - return `${prefix}${pascalCase(value || '')}`; + return `${prefix}${convertName(value, (s) => pascalCase(s || ''), transformUnderscore)}`; } }; @@ -46,8 +59,8 @@ const toMockName = (typedName: string, casedName: string, prefix?: string) => { return `${a(firstWord, { articleOnly: true })}${casedName}`; }; -const updateTextCase = (str: string, enumValuesConvention: NamingConvention) => { - const convert = createNameConverter(enumValuesConvention); +const updateTextCase = (str: string, enumValuesConvention: NamingConvention, transformUnderscore: boolean) => { + const convert = createNameConverter(enumValuesConvention, transformUnderscore); if (str.charAt(0) === '_') { return str.replace( @@ -91,7 +104,7 @@ const getNamedType = (opts: Options): string | number | boolean = casual.seed(hashedString(opts.typeName + opts.fieldName)); const name = opts.currentType.name.value; - const casedName = createNameConverter(opts.typenamesConvention)(name); + const casedName = createNameConverter(opts.typenamesConvention, opts.transformUnderscore)(name); switch (name) { case 'String': return `'${casual.word}'`; @@ -109,11 +122,15 @@ const getNamedType = (opts: Options): string | number | boolean = switch (foundType.type) { case 'enum': { // It's an enum - const typenameConverter = createNameConverter(opts.typenamesConvention); + const typenameConverter = createNameConverter( + opts.typenamesConvention, + opts.transformUnderscore, + ); const value = foundType.values ? foundType.values[0] : ''; return `${typenameConverter(foundType.name, opts.enumsPrefix)}.${updateTextCase( value, opts.enumValuesConvention, + opts.transformUnderscore, )}`; } case 'union': @@ -206,8 +223,9 @@ const getMockString = ( addTypename = false, prefix, typesPrefix = '', + transformUnderscore: boolean, ) => { - const typenameConverter = createNameConverter(typenamesConvention); + const typenameConverter = createNameConverter(typenamesConvention, transformUnderscore); const casedName = typenameConverter(typeName); const casedNameWithPrefix = typenameConverter(typeName, typesPrefix); const typename = addTypename ? `\n __typename: '${casedName}',` : ''; @@ -246,6 +264,7 @@ const getImportTypes = ({ typesFile, typesPrefix, enumsPrefix, + transformUnderscore, }: { typenamesConvention: NamingConvention; definitions: any; @@ -253,8 +272,9 @@ const getImportTypes = ({ typesFile: string; typesPrefix: string; enumsPrefix: string; + transformUnderscore: boolean; }) => { - const typenameConverter = createNameConverter(typenamesConvention); + const typenameConverter = createNameConverter(typenamesConvention, transformUnderscore); const typeImports = typesPrefix?.endsWith('.') ? [typesPrefix.slice(0, -1)] : definitions @@ -296,6 +316,7 @@ export interface TypescriptMocksPluginConfig { terminateCircularRelationships?: boolean; typesPrefix?: string; enumsPrefix?: string; + transformUnderscore?: boolean; } interface TypeItem { @@ -316,6 +337,7 @@ export const plugin: PluginFunction = (schema, docu const enumValuesConvention = config.enumValues || 'pascal-case#pascalCase'; const typenamesConvention = config.typenames || 'pascal-case#pascalCase'; + const transformUnderscore = config.transformUnderscore ?? true; // List of types that are enums const types: TypeItem[] = []; const visitor: VisitorType = { @@ -357,6 +379,7 @@ export const plugin: PluginFunction = (schema, docu enumsPrefix: config.enumsPrefix, currentType: node.type, customScalars: config.scalars, + transformUnderscore, }); return ` ${fieldName}: overrides && overrides.hasOwnProperty('${fieldName}') ? overrides.${fieldName}! : ${value},`; @@ -384,6 +407,7 @@ export const plugin: PluginFunction = (schema, docu enumsPrefix: config.enumsPrefix, currentType: field.type, customScalars: config.scalars, + transformUnderscore, }); return ` ${field.name.value}: overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value},`; @@ -399,6 +423,7 @@ export const plugin: PluginFunction = (schema, docu false, config.prefix, config.typesPrefix, + transformUnderscore, ); }, }; @@ -421,6 +446,7 @@ export const plugin: PluginFunction = (schema, docu !!config.addTypename, config.prefix, config.typesPrefix, + transformUnderscore, ); }, }; @@ -441,6 +467,7 @@ export const plugin: PluginFunction = (schema, docu !!config.addTypename, config.prefix, config.typesPrefix, + transformUnderscore, ); }, }; @@ -467,6 +494,7 @@ export const plugin: PluginFunction = (schema, docu typesFile, typesPrefix: config.typesPrefix, enumsPrefix: config.enumsPrefix, + transformUnderscore: transformUnderscore, }); // List of function that will generate the mock. // We generate it after having visited because we need to distinct types from enums diff --git a/tests/__snapshots__/typescript-mock-data.spec.ts.snap b/tests/__snapshots__/typescript-mock-data.spec.ts.snap index c4dda14..3a09665 100644 --- a/tests/__snapshots__/typescript-mock-data.spec.ts.snap +++ b/tests/__snapshots__/typescript-mock-data.spec.ts.snap @@ -27,9 +27,16 @@ export const mockMutation = (overrides?: Partial): Mutation => { }; }; +export const mockPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const mockQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : mockUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : mockPrefixedResponse(), }; }; @@ -91,9 +98,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -130,7 +144,7 @@ export const aWithAvatar = (overrides?: Partial): WithAvatar => { exports[`should add enumsPrefix to imports 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, Api } from './types/graphql'; +import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, Api } from './types/graphql'; export const anAbcType = (overrides?: Partial): AbcType => { return { @@ -157,9 +171,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -223,9 +244,16 @@ export const aMutation = (overrides?: Partial): Api.Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): Api.PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Api.Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -287,9 +315,16 @@ export const aMutation = (overrides?: Partial): Api.Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): Api.PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Api.Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -353,9 +388,16 @@ export const aMutation = (overrides?: Partial): Api.Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): Api.PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Api.Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -417,9 +459,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -481,9 +530,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -545,9 +601,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -609,9 +672,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -673,9 +743,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -737,9 +814,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -776,7 +860,7 @@ export const aWithAvatar = (overrides?: Partial): WithAvatar => { exports[`should generate mock data functions with external types file import 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql'; +import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql'; export const anAbcType = (overrides?: Partial): AbcType => { return { @@ -803,9 +887,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -867,9 +958,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -931,9 +1029,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -995,9 +1100,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1059,9 +1171,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1098,7 +1217,7 @@ export const aWithAvatar = (overrides?: Partial): WithAvatar => { exports[`should generate mock data with PascalCase types and enums by default 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql'; +import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql'; export const anAbcType = (overrides?: Partial): AbcType => { return { @@ -1125,9 +1244,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1189,9 +1315,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1253,9 +1386,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixed_Response = (overrides?: Partial): Prefixed_Response => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixed_Response(), }; }; @@ -1321,10 +1461,18 @@ export const aMutation = (overrides?: Partial): { __typename: 'Mutatio }; }; +export const aPrefixedResponse = (overrides?: Partial): { __typename: 'PrefixedResponse' } & PrefixedResponse => { + return { + __typename: 'PrefixedResponse', + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): { __typename: 'Query' } & Query => { return { __typename: 'Query', user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1388,9 +1536,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1452,9 +1607,16 @@ export const aMUTATION = (overrides?: Partial): MUTATION => { }; }; +export const aPREFIXED_RESPONSE = (overrides?: Partial): PREFIXED_RESPONSE => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQUERY = (overrides?: Partial): QUERY => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUSER(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPREFIXED_RESPONSE(), }; }; @@ -1491,7 +1653,7 @@ export const aWITHAVATAR = (overrides?: Partial): WITHAVATAR => { exports[`should generate mock data with upperCase types and imports if typenames is "upper-case#upperCase" 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { ABCTYPE, AVATAR, CAMELCASETHING, MUTATION, QUERY, UPDATEUSERINPUT, USER, WITHAVATAR, ABCSTATUS, STATUS } from './types/graphql'; +import { ABCTYPE, AVATAR, CAMELCASETHING, MUTATION, PREFIXED_RESPONSE, QUERY, UPDATEUSERINPUT, USER, WITHAVATAR, ABCSTATUS, STATUS } from './types/graphql'; export const anABCTYPE = (overrides?: Partial): ABCTYPE => { return { @@ -1518,9 +1680,16 @@ export const aMUTATION = (overrides?: Partial): MUTATION => { }; }; +export const aPREFIXED_RESPONSE = (overrides?: Partial): PREFIXED_RESPONSE => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQUERY = (overrides?: Partial): QUERY => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUSER(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPREFIXED_RESPONSE(), }; }; @@ -1557,7 +1726,7 @@ export const aWITHAVATAR = (overrides?: Partial): WITHAVATAR => { exports[`should not merge imports into one if enumsPrefix does not contain dots 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, ApiAbcStatus, ApiStatus } from './types/graphql'; +import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, ApiAbcStatus, ApiStatus } from './types/graphql'; export const anAbcType = (overrides?: Partial): AbcType => { return { @@ -1584,9 +1753,16 @@ export const aMutation = (overrides?: Partial): Mutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): PrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): Query => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1623,7 +1799,7 @@ export const aWithAvatar = (overrides?: Partial): WithAvatar => { exports[`should not merge imports into one if typesPrefix does not contain dots 1`] = ` "/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ -import { ApiAbcType, ApiAvatar, ApiCamelCaseThing, ApiMutation, ApiQuery, ApiUpdateUserInput, ApiUser, ApiWithAvatar, AbcStatus, Status } from './types/graphql'; +import { ApiAbcType, ApiAvatar, ApiCamelCaseThing, ApiMutation, ApiPrefixedResponse, ApiQuery, ApiUpdateUserInput, ApiUser, ApiWithAvatar, AbcStatus, Status } from './types/graphql'; export const anAbcType = (overrides?: Partial): ApiAbcType => { return { @@ -1650,9 +1826,16 @@ export const aMutation = (overrides?: Partial): ApiMutation => { }; }; +export const aPrefixedResponse = (overrides?: Partial): ApiPrefixedResponse => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial): ApiQuery => { return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(), }; }; @@ -1687,6 +1870,79 @@ export const aWithAvatar = (overrides?: Partial): ApiWithAvatar = " `; +exports[`should preserve underscores if transformUnderscore is false 1`] = ` +"/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */ +import { AbcType, Avatar, CamelCaseThing, Mutation, Prefixed_Response, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql'; + +export const anAbcType = (overrides?: Partial): AbcType => { + return { + abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit', + }; +}; + +export const anAvatar = (overrides?: Partial): Avatar => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38', + url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid', + }; +}; + +export const aCamelCaseThing = (overrides?: Partial): CamelCaseThing => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '345b9cf9-00fa-4974-800f-aeee5ee7fd42', + }; +}; + +export const aMutation = (overrides?: Partial): Mutation => { + return { + updateUser: overrides && overrides.hasOwnProperty('updateUser') ? overrides.updateUser! : aUser(), + }; +}; + +export const aPrefixed_Response = (overrides?: Partial): Prefixed_Response => { + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + +export const aQuery = (overrides?: Partial): Query => { + return { + user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixed_Response(), + }; +}; + +export const anUpdateUserInput = (overrides?: Partial): UpdateUserInput => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc', + login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui', + avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(), + }; +}; + +export const aUser = (overrides?: Partial): User => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750', + creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '1970-01-09T16:33:21.532Z', + login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'libero', + avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(), + status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online, + customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus, + scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque', + camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : aCamelCaseThing(), + unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : anAvatar(), + }; +}; + +export const aWithAvatar = (overrides?: Partial): WithAvatar => { + return { + id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '89f515e7-31e0-461d-a230-c4c7f4dafc5c', + avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(), + }; +}; +" +`; + exports[`should use relationshipsToOmit argument to terminate circular relationships with terminateCircularRelationships enabled 1`] = ` " export const anAbcType = (overrides?: Partial, relationshipsToOmit: Set = new Set()): AbcType => { @@ -1718,10 +1974,18 @@ export const aMutation = (overrides?: Partial, relationshipsToOmit: Se }; }; +export const aPrefixedResponse = (overrides?: Partial, relationshipsToOmit: Set = new Set()): PrefixedResponse => { + relationshipsToOmit.add('PrefixedResponse'); + return { + ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt', + }; +}; + export const aQuery = (overrides?: Partial, relationshipsToOmit: Set = new Set()): Query => { relationshipsToOmit.add('Query'); return { user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : relationshipsToOmit.has('User') ? {} as User : aUser({}, relationshipsToOmit), + prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : relationshipsToOmit.has('Prefixed_Response') ? {} as Prefixed_Response : aPrefixedResponse({}, relationshipsToOmit), }; }; diff --git a/tests/typescript-mock-data.spec.ts b/tests/typescript-mock-data.spec.ts index f166245..f03e3a5 100644 --- a/tests/typescript-mock-data.spec.ts +++ b/tests/typescript-mock-data.spec.ts @@ -35,6 +35,11 @@ const testSchema = buildSchema(/* GraphQL */ ` type Query { user: User! + prefixed_query: Prefixed_Response! + } + + type Prefixed_Response { + ping: String! } type ABCType { @@ -89,7 +94,7 @@ it('should generate mock data functions with external types file import', async expect(result).toBeDefined(); expect(result).toContain( - "import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql';", + "import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql';", ); expect(result).toMatchSnapshot(); }); @@ -305,7 +310,7 @@ it('should add enumsPrefix to imports', async () => { expect(result).toBeDefined(); expect(result).toContain( - "import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, Api } from './types/graphql';", + "import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, Api } from './types/graphql';", ); expect(result).toMatchSnapshot(); }); @@ -330,7 +335,7 @@ it('should not merge imports into one if typesPrefix does not contain dots', asy expect(result).toBeDefined(); expect(result).toContain( - "import { ApiAbcType, ApiAvatar, ApiCamelCaseThing, ApiMutation, ApiQuery, ApiUpdateUserInput, ApiUser, ApiWithAvatar, AbcStatus, Status } from './types/graphql';", + "import { ApiAbcType, ApiAvatar, ApiCamelCaseThing, ApiMutation, ApiPrefixedResponse, ApiQuery, ApiUpdateUserInput, ApiUser, ApiWithAvatar, AbcStatus, Status } from './types/graphql';", ); expect(result).toMatchSnapshot(); }); @@ -343,7 +348,7 @@ it('should not merge imports into one if enumsPrefix does not contain dots', asy expect(result).toBeDefined(); expect(result).toContain( - "import { AbcType, Avatar, CamelCaseThing, Mutation, Query, UpdateUserInput, User, WithAvatar, ApiAbcStatus, ApiStatus } from './types/graphql';", + "import { AbcType, Avatar, CamelCaseThing, Mutation, PrefixedResponse, Query, UpdateUserInput, User, WithAvatar, ApiAbcStatus, ApiStatus } from './types/graphql';", ); expect(result).toMatchSnapshot(); }); @@ -357,3 +362,19 @@ it('should use relationshipsToOmit argument to terminate circular relationships expect(result).not.toMatch(/: anAvatar\(\)/); expect(result).toMatchSnapshot(); }); + +it('should preserve underscores if transformUnderscore is false', async () => { + const result = await plugin(testSchema, [], { + transformUnderscore: false, + typesFile: './types/graphql.ts', + }); + + expect(result).toBeDefined(); + expect(result).toContain( + "import { AbcType, Avatar, CamelCaseThing, Mutation, Prefixed_Response, Query, UpdateUserInput, User, WithAvatar, AbcStatus, Status } from './types/graphql';", + ); + expect(result).toContain( + 'export const aPrefixed_Response = (overrides?: Partial): Prefixed_Response => {', + ); + expect(result).toMatchSnapshot(); +});