Skip to content
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# Check {{ '/2.0/language-javascript/' | docs_url }} for more details
#

defaults: &defaults
working_directory: ~/repo
docker:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ plugins:
Date: date # gets translated to casual.date()
```

**Custom value generator**

```yaml
plugins:
- add: "import { arrayBufferGenerator } from '../generators';"
- typescript-mock-data:
scalars:
ArrayBuffer: arrayBufferGenerator()
```

### typesPrefix (`string`, defaultValue: '')

Useful if you have globally exported types under a certain namespace.
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ const getNamedType = (
// If there is a mapping to a `casual` type, then use it and make sure
// to call it if it's a function
const embeddedGenerator = casual[customScalar.generator];

if (!embeddedGenerator && customScalar.generator) {
return customScalar.generator;
}

const generatorArgs: unknown[] = Array.isArray(customScalar.arguments)
? customScalar.arguments
: [customScalar.arguments];
Expand Down Expand Up @@ -257,7 +262,7 @@ ${fields}
}
};

type ScalarGeneratorName = keyof Casual.Casual | keyof Casual.functions;
type ScalarGeneratorName = keyof Casual.Casual | keyof Casual.functions | string;
type ScalarDefinition = {
generator: ScalarGeneratorName;
arguments: unknown;
Expand Down
37 changes: 37 additions & 0 deletions tests/__snapshots__/typescript-mock-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,43 @@ export const aUser = (overrides?: Partial<User>): User => {
"
`;

exports[`should correctly use custom generator as default value 1`] = `
"
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
return {
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
};
};

export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',
};
};

export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): 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>): 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! : myValueGenerator(),
};
};
"
`;

exports[`should generate mock data functions 1`] = `
"
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
Expand Down
15 changes: 15 additions & 0 deletions tests/typescript-mock-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ it('should correctly generate the `casual` data for a function with one argument
expect(result).toMatchSnapshot();
});

it('should correctly use custom generator as default value', async () => {
const result = await plugin(testSchema, [], {
scalars: {
AnyObject: {
generator: 'myValueGenerator()',
arguments: [],
},
},
});

expect(result).toBeDefined();
expect(result).toContain('myValueGenerator()');
expect(result).toMatchSnapshot();
});

it('should add typesPrefix to all types when option is specified', async () => {
const result = await plugin(testSchema, [], { typesPrefix: 'Api.' });

Expand Down