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

feat(mergeMocks): support Promise #1883

Merged
merged 1 commit into from Aug 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/mock/src/mocking.ts
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
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