Skip to content

Use spread operator to override values (fixes #114) #131

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ export const ${toMockName(
relationshipsToOmit.add('${casedName}');
return {${typename}
${fields}
...overrides,
};
};`;
} else {
Expand All @@ -440,6 +441,7 @@ export const ${toMockName(
)} = (overrides?: Partial<${casedNameWithPrefix}>): ${typenameReturnType}${casedNameWithPrefix} => {
return {${typename}
${fields}
...overrides,
};
};`;
}
Expand Down Expand Up @@ -656,7 +658,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
nonNull: false,
});

return ` ${fieldName}: overrides && overrides.hasOwnProperty('${fieldName}') ? overrides.${fieldName}! : ${value},`;
return ` ${fieldName}: ${value},`;
},
};
},
Expand Down Expand Up @@ -692,7 +694,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
nonNull: false,
});

return ` ${field.name.value}: overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value},`;
return ` ${field.name.value}: ${value},`;
})
.join('\n')
: '';
Expand Down
2,400 changes: 1,400 additions & 1,000 deletions tests/__snapshots__/typescript-mock-data.spec.ts.snap

Large diffs are not rendered by default.

156 changes: 84 additions & 72 deletions tests/enumValues/__snapshots__/spec.ts.snap

Large diffs are not rendered by default.

1,332 changes: 740 additions & 592 deletions tests/perTypeFieldGeneration/__snapshots__/spec.ts.snap

Large diffs are not rendered by default.

186 changes: 46 additions & 140 deletions tests/perTypeFieldGeneration/spec.ts

Large diffs are not rendered by default.

64 changes: 36 additions & 28 deletions tests/scalars/__snapshots__/spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ exports[`should generate custom scalars for native and custom types using casual
"
export const anA = (overrides?: Partial<A>): A => {
return {
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! : '[email protected]',
id: 82,
str: 'ea corrupti qui incidunt eius consequatur blanditiis',
obj: aB(),
anyObject: '[email protected]',
...overrides,
};
};

export const aB = (overrides?: Partial<B>): B => {
return {
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
int: -93,
flt: -24.509902694262564,
bool: false,
...overrides,
};
};
"
Expand All @@ -25,18 +27,20 @@ exports[`should generate custom scalars for native and custom types using faker
"
export const anA = (overrides?: Partial<A>): A => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Corrupti qui incidunt eius consequatur qui.',
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(),
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : '[email protected]',
id: 83,
str: 'Corrupti qui incidunt eius consequatur qui.',
obj: aB(),
anyObject: '[email protected]',
...overrides,
};
};

export const aB = (overrides?: Partial<B>): 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,
int: -93,
flt: -24.51,
bool: false,
...overrides,
};
};
"
Expand All @@ -49,18 +53,20 @@ casual.seed(0);

export const anA = (overrides?: Partial<A>): 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'],
id: casual['integer'](...[1,100]),
str: casual['string'],
obj: aB(),
anyObject: casual['email'],
...overrides,
};
};

export const aB = (overrides?: Partial<B>): 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,
int: casual['integer'](...[-100,0]),
flt: casual['double'](...[-100,0]),
bool: false,
...overrides,
};
};

Expand All @@ -75,18 +81,20 @@ faker.seed(0);

export const anA = (overrides?: Partial<A>): A => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\\"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'](),
id: faker['datatype']['number'](...[{\\"min\\":1,\\"max\\":100}]),
str: faker['lorem']['sentence'](),
obj: aB(),
anyObject: faker['internet']['email'](),
...overrides,
};
};

export const aB = (overrides?: Partial<B>): B => {
return {
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\\"min\\":-100,\\"max\\":0}]),
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\\"min\\":-100,\\"max\\":0}]),
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
int: faker['datatype']['number'](...[{\\"min\\":-100,\\"max\\":0}]),
flt: faker['datatype']['float'](...[{\\"min\\":-100,\\"max\\":0}]),
bool: false,
...overrides,
};
};

Expand Down
60 changes: 20 additions & 40 deletions tests/scalars/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@ it('should generate custom scalars for native and custom types using casual', as
expect(result).toBeDefined();

// String
expect(result).toContain(
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',",
);
expect(result).toContain("str: 'ea corrupti qui incidunt eius consequatur blanditiis',");

// Float
expect(result).toContain(
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,",
);
expect(result).toContain('flt: -24.509902694262564,');

// ID
expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,");
expect(result).toContain('id: 82,');

// Boolean
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
expect(result).toContain('bool: false');

// Int
expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,");
expect(result).toContain('int: -93,');

expect(result).toMatchSnapshot();
});
Expand Down Expand Up @@ -71,25 +67,19 @@ it('should generate dynamic custom scalars for native and custom types using cas
expect(result).toBeDefined();

// String
expect(result).toContain("str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],");
expect(result).toContain("str: casual['string'],");

// Float
expect(result).toContain(
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),",
);
expect(result).toContain("flt: casual['double'](...[-100,0]),");

// ID
expect(result).toContain(
"id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),",
);
expect(result).toContain("id: casual['integer'](...[1,100]),");

// Boolean
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
expect(result).toContain('bool: false');

// Int
expect(result).toContain(
"int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),",
);
expect(result).toContain("int: casual['integer'](...[-100,0]),");

expect(result).toMatchSnapshot();
});
Expand Down Expand Up @@ -119,21 +109,19 @@ it('should generate custom scalars for native and custom types using faker', asy
expect(result).toBeDefined();

// String
expect(result).toContain(
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Corrupti qui incidunt eius consequatur qui.',",
);
expect(result).toContain("str: 'Corrupti qui incidunt eius consequatur qui.',");

// Float
expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,");
expect(result).toContain('flt: -24.51,');

// ID
expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,");
expect(result).toContain('id: 83,');

// Boolean
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
expect(result).toContain('bool: false');

// Int
expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,");
expect(result).toContain('int: -93,');

expect(result).toMatchSnapshot();
});
Expand Down Expand Up @@ -164,27 +152,19 @@ it('should generate dynamic custom scalars for native and custom types using fak
expect(result).toBeDefined();

// String
expect(result).toContain(
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),",
);
expect(result).toContain("str: faker['lorem']['sentence'](),");

// Float
expect(result).toContain(
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['datatype']['float'](...[{\"min\":-100,\"max\":0}]),",
);
expect(result).toContain('flt: faker[\'datatype\'][\'float\'](...[{"min":-100,"max":0}]),');

// ID
expect(result).toContain(
"id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['datatype']['number'](...[{\"min\":1,\"max\":100}]),",
);
expect(result).toContain('id: faker[\'datatype\'][\'number\'](...[{"min":1,"max":100}]),');

// Boolean
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
expect(result).toContain('bool: false');

// Int
expect(result).toContain(
"int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['datatype']['number'](...[{\"min\":-100,\"max\":0}]),",
);
expect(result).toContain('int: faker[\'datatype\'][\'number\'](...[{"min":-100,"max":0}]),');

expect(result).toMatchSnapshot();
});
60 changes: 16 additions & 44 deletions tests/typescript-mock-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ it('should generate mock data functions with scalars', async () => {
const result = await plugin(testSchema, [], {});

expect(result).toBeDefined();
expect(result).toContain(
"scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque',",
);
expect(result).toContain("scalarValue: 'neque',");
expect(result).toMatchSnapshot();
});

Expand Down Expand Up @@ -431,9 +429,7 @@ it('should preserve underscores if transformUnderscore is false', async () => {
expect(result).toContain(
'export const aPrefixed_Response = (overrides?: Partial<Prefixed_Response>): Prefixed_Response => {',
);
expect(result).toContain(
"prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : Prefixed_Enum.PrefixedValue,",
);
expect(result).toContain('prefixedEnum: Prefixed_Enum.PrefixedValue,');
expect(result).toMatchSnapshot();
});

Expand All @@ -451,9 +447,7 @@ it('should preserve underscores if transformUnderscore is false and enumsAsTypes
expect(result).toContain(
'export const aPrefixed_Response = (overrides?: Partial<Prefixed_Response>): Prefixed_Response => {',
);
expect(result).toContain(
"prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : 'PREFIXED_VALUE',",
);
expect(result).toContain("prefixedEnum: 'PREFIXED_VALUE',");
expect(result).toMatchSnapshot();
});

Expand All @@ -463,9 +457,7 @@ it('should generate single list element', async () => {
});

expect(result).toBeDefined();
expect(result).toContain(
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem']",
);
expect(result).toContain("stringList: ['voluptatem']");
expect(result).toMatchSnapshot();
});

Expand All @@ -476,9 +468,7 @@ it('should generate multiple list elements', async () => {
});

expect(result).toBeDefined();
expect(result).toContain(
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id', 'soluta', 'quis']",
);
expect(result).toContain("stringList: ['id', 'soluta', 'quis']");
expect(result).toMatchSnapshot();
});

Expand All @@ -489,9 +479,7 @@ it('should generate no list elements when listElementCount is 0', async () => {
});

expect(result).toBeDefined();
expect(result).toContain(
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : []",
);
expect(result).toContain('stringList: []');
expect(result).toMatchSnapshot();
});

Expand All @@ -513,23 +501,13 @@ it('defaults all nullable fields to null when defaultNullableToNull is set', asy
const result = await plugin(testSchema, [], { defaultNullableToNull: true });

expect(result).toBeDefined();
expect(result).toContain(
"customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : null",
);
expect(result).toContain(
"camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : null",
);
expect(result).toContain(
"unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : null",
);
expect(result).toContain(
"prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : null",
);
expect(result).toContain("avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : null");
expect(result).toContain("login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : null");
expect(result).toContain(
"nullableStringList: overrides && overrides.hasOwnProperty('nullableStringList') ? overrides.nullableStringList! : null",
);
expect(result).toContain('customStatus: null');
expect(result).toContain('camelCaseThing: null');
expect(result).toContain('unionThing: null');
expect(result).toContain('prefixedEnum: null');
expect(result).toContain('avatar: null');
expect(result).toContain('login: null');
expect(result).toContain('nullableStringList: null');

expect(result).toMatchSnapshot();
});
Expand All @@ -549,15 +527,9 @@ it('overriding works as expected when defaultNullableToNull is true', async () =
});

expect(result).toBeDefined();
expect(result).toContain(
"customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : 'abc'",
);
expect(result).toContain(
"avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : someAvatar",
);
expect(result).toContain(
"nullableStringList: overrides && overrides.hasOwnProperty('nullableStringList') ? overrides.nullableStringList! : ['abc']",
);
expect(result).toContain("customStatus: 'abc'");
expect(result).toContain('avatar: someAvatar');
expect(result).toContain("nullableStringList: ['abc']");

expect(result).toMatchSnapshot();
});
Loading