Skip to content

Commit 150b4c7

Browse files
ardeoisJonghakseo
andauthored
feat: relationshipsToOmit bug when terminateCircularRelationships is true (#78)
Co-authored-by: JongHak Seo <[email protected]>
1 parent 434485a commit 150b4c7

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ const getNamedType = (opts: Options<NamedTypeNode>): string | number | boolean =
181181
}
182182
}
183183
if (opts.terminateCircularRelationships) {
184-
return `relationshipsToOmit.has('${casedName}') ? {} as ${casedName} : ${toMockName(
184+
return `relationshipsToOmit.includes('${casedName}') ? {} as ${casedName} : ${toMockName(
185185
name,
186186
casedName,
187187
opts.prefix,
@@ -239,8 +239,8 @@ export const ${toMockName(
239239
typeName,
240240
casedName,
241241
prefix,
242-
)} = (overrides?: Partial<${casedNameWithPrefix}>, relationshipsToOmit: Set<string> = new Set()): ${typenameReturnType}${casedNameWithPrefix} => {
243-
relationshipsToOmit.add('${casedName}');
242+
)} = (overrides?: Partial<${casedNameWithPrefix}>, _relationshipsToOmit: Array<string> = []): ${typenameReturnType}${casedNameWithPrefix} => {
243+
const relationshipsToOmit = ([..._relationshipsToOmit, '${casedName}']);
244244
return {${typename}
245245
${fields}
246246
};

tests/__snapshots__/typescript-mock-data.spec.ts.snap

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,80 +1963,80 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
19631963

19641964
exports[`should use relationshipsToOmit argument to terminate circular relationships with terminateCircularRelationships enabled 1`] = `
19651965
"
1966-
export const anAvatar = (overrides?: Partial<Avatar>, relationshipsToOmit: Set<string> = new Set()): Avatar => {
1967-
relationshipsToOmit.add('Avatar');
1966+
export const anAvatar = (overrides?: Partial<Avatar>, _relationshipsToOmit: Array<string> = []): Avatar => {
1967+
const relationshipsToOmit = ([..._relationshipsToOmit, 'Avatar']);
19681968
return {
19691969
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
19701970
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',
19711971
};
19721972
};
19731973

1974-
export const aUser = (overrides?: Partial<User>, relationshipsToOmit: Set<string> = new Set()): User => {
1975-
relationshipsToOmit.add('User');
1974+
export const aUser = (overrides?: Partial<User>, _relationshipsToOmit: Array<string> = []): User => {
1975+
const relationshipsToOmit = ([..._relationshipsToOmit, 'User']);
19761976
return {
19771977
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
19781978
creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '1970-01-09T16:33:21.532Z',
19791979
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'libero',
1980-
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.has('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
1980+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.includes('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
19811981
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,
19821982
customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus,
19831983
scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque',
1984-
camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : relationshipsToOmit.has('CamelCaseThing') ? {} as CamelCaseThing : aCamelCaseThing({}, relationshipsToOmit),
1985-
unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : relationshipsToOmit.has('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
1984+
camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : relationshipsToOmit.includes('CamelCaseThing') ? {} as CamelCaseThing : aCamelCaseThing({}, relationshipsToOmit),
1985+
unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : relationshipsToOmit.includes('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
19861986
prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : PrefixedEnum.PrefixedValue,
19871987
};
19881988
};
19891989

1990-
export const aWithAvatar = (overrides?: Partial<WithAvatar>, relationshipsToOmit: Set<string> = new Set()): WithAvatar => {
1991-
relationshipsToOmit.add('WithAvatar');
1990+
export const aWithAvatar = (overrides?: Partial<WithAvatar>, _relationshipsToOmit: Array<string> = []): WithAvatar => {
1991+
const relationshipsToOmit = ([..._relationshipsToOmit, 'WithAvatar']);
19921992
return {
19931993
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '89f515e7-31e0-461d-a230-c4c7f4dafc5c',
1994-
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.has('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
1994+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.includes('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
19951995
};
19961996
};
19971997

1998-
export const aCamelCaseThing = (overrides?: Partial<CamelCaseThing>, relationshipsToOmit: Set<string> = new Set()): CamelCaseThing => {
1999-
relationshipsToOmit.add('CamelCaseThing');
1998+
export const aCamelCaseThing = (overrides?: Partial<CamelCaseThing>, _relationshipsToOmit: Array<string> = []): CamelCaseThing => {
1999+
const relationshipsToOmit = ([..._relationshipsToOmit, 'CamelCaseThing']);
20002000
return {
20012001
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '345b9cf9-00fa-4974-800f-aeee5ee7fd42',
20022002
};
20032003
};
20042004

2005-
export const aPrefixedResponse = (overrides?: Partial<PrefixedResponse>, relationshipsToOmit: Set<string> = new Set()): PrefixedResponse => {
2006-
relationshipsToOmit.add('PrefixedResponse');
2005+
export const aPrefixedResponse = (overrides?: Partial<PrefixedResponse>, _relationshipsToOmit: Array<string> = []): PrefixedResponse => {
2006+
const relationshipsToOmit = ([..._relationshipsToOmit, 'PrefixedResponse']);
20072007
return {
20082008
ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt',
20092009
};
20102010
};
20112011

2012-
export const anAbcType = (overrides?: Partial<AbcType>, relationshipsToOmit: Set<string> = new Set()): AbcType => {
2013-
relationshipsToOmit.add('AbcType');
2012+
export const anAbcType = (overrides?: Partial<AbcType>, _relationshipsToOmit: Array<string> = []): AbcType => {
2013+
const relationshipsToOmit = ([..._relationshipsToOmit, 'AbcType']);
20142014
return {
20152015
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
20162016
};
20172017
};
20182018

2019-
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>, relationshipsToOmit: Set<string> = new Set()): UpdateUserInput => {
2020-
relationshipsToOmit.add('UpdateUserInput');
2019+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>, _relationshipsToOmit: Array<string> = []): UpdateUserInput => {
2020+
const relationshipsToOmit = ([..._relationshipsToOmit, 'UpdateUserInput']);
20212021
return {
20222022
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
20232023
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
2024-
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.has('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
2024+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : relationshipsToOmit.includes('Avatar') ? {} as Avatar : anAvatar({}, relationshipsToOmit),
20252025
};
20262026
};
20272027

2028-
export const aMutation = (overrides?: Partial<Mutation>, relationshipsToOmit: Set<string> = new Set()): Mutation => {
2029-
relationshipsToOmit.add('Mutation');
2028+
export const aMutation = (overrides?: Partial<Mutation>, _relationshipsToOmit: Array<string> = []): Mutation => {
2029+
const relationshipsToOmit = ([..._relationshipsToOmit, 'Mutation']);
20302030
return {
2031-
updateUser: overrides && overrides.hasOwnProperty('updateUser') ? overrides.updateUser! : relationshipsToOmit.has('User') ? {} as User : aUser({}, relationshipsToOmit),
2031+
updateUser: overrides && overrides.hasOwnProperty('updateUser') ? overrides.updateUser! : relationshipsToOmit.includes('User') ? {} as User : aUser({}, relationshipsToOmit),
20322032
};
20332033
};
20342034

2035-
export const aQuery = (overrides?: Partial<Query>, relationshipsToOmit: Set<string> = new Set()): Query => {
2036-
relationshipsToOmit.add('Query');
2035+
export const aQuery = (overrides?: Partial<Query>, _relationshipsToOmit: Array<string> = []): Query => {
2036+
const relationshipsToOmit = ([..._relationshipsToOmit, 'Query']);
20372037
return {
2038-
user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : relationshipsToOmit.has('User') ? {} as User : aUser({}, relationshipsToOmit),
2039-
prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : relationshipsToOmit.has('PrefixedResponse') ? {} as PrefixedResponse : aPrefixedResponse({}, relationshipsToOmit),
2038+
user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : relationshipsToOmit.includes('User') ? {} as User : aUser({}, relationshipsToOmit),
2039+
prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : relationshipsToOmit.includes('PrefixedResponse') ? {} as PrefixedResponse : aPrefixedResponse({}, relationshipsToOmit),
20402040
};
20412041
};
20422042
"

tests/circular-mocks/create-mocks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default async () => {
1313
type C {
1414
aCollection: [A!]!
1515
}
16+
type D {
17+
A: A!
18+
B: B!
19+
}
1620
`);
1721

1822
const output = await plugin(circularSchema, [], { typesFile: './types.ts', terminateCircularRelationships: true });

tests/circular-mocks/spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { aB, aC, anA } from './mocks';
1+
import { aB, aC, aD, anA } from './mocks';
22

33
it('should terminate circular relationships when terminateCircularRelationships is true', () => {
44
const a = anA();
@@ -9,4 +9,7 @@ it('should terminate circular relationships when terminateCircularRelationships
99

1010
const c = aC();
1111
expect(c).toEqual({ aCollection: [{ B: { A: {} }, C: {} }] });
12+
13+
const d = aD();
14+
expect(d).toEqual({ A: { B: { A: {} }, C: { aCollection: [{}] } }, B: { A: { B: {}, C: { aCollection: [{}] } } } });
1215
});

tests/circular-mocks/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ export type B = {
1010
export type C = {
1111
aCollection: A[];
1212
};
13+
14+
export type D = {
15+
A: A;
16+
B: B;
17+
};

tests/typescript-mock-data.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,10 @@ it('should use relationshipsToOmit argument to terminate circular relationships
363363
const result = await plugin(testSchema, [], { terminateCircularRelationships: true });
364364

365365
expect(result).toBeDefined();
366-
expect(result).toMatch(/relationshipsToOmit.add\('User'\)/);
367-
expect(result).toMatch(/relationshipsToOmit.has\('Avatar'\) \? {} as Avatar : anAvatar\({}, relationshipsToOmit\)/);
366+
expect(result).toMatch(/const relationshipsToOmit = \(\[..._relationshipsToOmit, 'User']\)/);
367+
expect(result).toMatch(
368+
/relationshipsToOmit.includes\('Avatar'\) \? {} as Avatar : anAvatar\({}, relationshipsToOmit\)/,
369+
);
368370
expect(result).not.toMatch(/: anAvatar\(\)/);
369371
expect(result).toMatchSnapshot();
370372
});

0 commit comments

Comments
 (0)