Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update type case in import #43

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
const result: any = visit(astNode, { leave: visitor });
const definitions = result.definitions.filter((definition: any) => !!definition);
const typesFile = config.typesFile ? config.typesFile.replace(/\.[\w]+$/, '') : null;
const typenameConverter = createNameConverter(typenamesConvention);
const typeImports = definitions
.map(({ typeName }: { typeName: string }) => typeName)
.map(({ typeName }: { typeName: string }) => typenameConverter(typeName))
.filter((typeName: string) => !!typeName);
typeImports.push(...types.filter(({ type }) => type !== 'scalar').map(({ name }) => name));
typeImports.push(...types.filter(({ type }) => type !== 'scalar').map(({ name }) => typenameConverter(name)));
// List of function that will generate the mock.
// We generate it after having visited because we need to distinct types from enums
const mockFns = definitions.map(({ mockFn }: any) => mockFn).filter((mockFn: Function) => !!mockFn);
Expand Down
6 changes: 4 additions & 2 deletions tests/__snapshots__/typescript-mock-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const aUser = (overrides?: Partial<User>): User => {

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, UpdateUserInput, User, ABCStatus, Status } from './types/graphql';
import { AbcType, Avatar, UpdateUserInput, User, AbcStatus, Status } from './types/graphql';

export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
return {
Expand Down Expand Up @@ -558,7 +558,9 @@ export const aUser = (overrides?: Partial<User>): User => {
`;

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, UpdateUserInput, User, AbcStatus, Status } from './types/graphql';

export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
return {
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
Expand Down
4 changes: 2 additions & 2 deletions tests/typescript-mock-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ it('should generate mock data functions with external types file import', async

expect(result).toBeDefined();
expect(result).toContain(
"import { ABCType, Avatar, UpdateUserInput, User, ABCStatus, Status } from './types/graphql';",
"import { AbcType, Avatar, UpdateUserInput, User, AbcStatus, Status } from './types/graphql';",
);
expect(result).toMatchSnapshot();
});
Expand Down Expand Up @@ -130,7 +130,7 @@ it('should generate mock data with as-is enum values if enumValues is "keep"', a
});

it('should generate mock data with pascalCase types and enums by default', async () => {
const result = await plugin(testSchema, [], {});
const result = await plugin(testSchema, [], { typesFile: './types/graphql.ts' });

expect(result).toBeDefined();
expect(result).toMatch(/Abc(Type|Status)/);
Expand Down