Skip to content

Commit

Permalink
feat(mergeMocks): support Promises (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
MLKiiwy authored Aug 10, 2020
1 parent 758c3a4 commit f4bafe0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/mock/src/mocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ function mergeMocks(genericMockFunction: () => any, customMock: any): any {
if (Array.isArray(customMock)) {
return customMock.map((el: any) => mergeMocks(genericMockFunction, el));
}
if (customMock instanceof Promise) {
return customMock.then((res: any) => mergeObjects(genericMockFunction(), res));
}
if (isObject(customMock)) {
return mergeObjects(genericMockFunction(), customMock);
}
Expand Down
45 changes: 45 additions & 0 deletions packages/mock/tests/mocking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,51 @@ describe('Mock', () => {
expect(typeof result.data?.reviews[0]?.user?.first_name).toBe('string');
});

it('should merge resolved result of a promise with default mock if available', async () => {
const resolvedName = 'Resolved name';
const mockedSecondName = 'mocked second name';
const mocks = {
MyType: () => ({
name: 'mocked name',
secondName: mockedSecondName,
}),
Query: () => ({
pendingQuery: () => Promise.resolve({
name: 'Resolved name'
}),
}),
};

let schema = buildSchema(/* GraphQL */ `
type MyType {
name: String
secondName: String
}
type Query {
pendingQuery: MyType
}
`);

schema = addMocksToSchema({ schema, mocks });

const result = await graphql({
schema,
source: /* GraphQL */ `
{
pendingQuery {
name
secondName
}
}
`,
});

expect(result.data?.pendingQuery).toEqual({
name: resolvedName,
secondName: mockedSecondName,
});
});

// TODO add a test that checks that even when merging defaults, lists invoke
// the function for every object, not just once per list.

Expand Down

0 comments on commit f4bafe0

Please sign in to comment.