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

GraphQL Enhancements #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"husky": "^4.2.5",
"jest": "^26.5.2",
"lerna": "^3.22.1",
"lockfile-lint": "^4.3.7"
"lockfile-lint": "^4.3.7",
"prettier": "^2.8.8"
},
"husky": {
"hooks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ Object {
},
}
`;

exports[`Actions setQuery should have correct shape 1`] = `
Object {
"hash": "abc1234",
"query": undefined,
"type": "@fetchye/SET_QUERY",
}
`;

exports[`Actions updateDataAction should have correct shape 1`] = `
Object {
"hash": "abc1234",
"type": "@fetchye/UPDATE_DATA",
"value": Object {
"body": Object {
"fakeData": true,
},
"ok": true,
"status": 200,
},
}
`;
144 changes: 144 additions & 0 deletions packages/fetchye-core/__tests__/defaultFetcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,158 @@
* permissions and limitations under the License.
*/

import { jsonToGraphQLQuery } from 'json-to-graphql-query';
import { trimQueryBody } from '../src/trimQueryBody';
import { defaultFetcher } from '../src/defaultFetcher';

jest.mock('json-to-graphql-query', () => ({
jsonToGraphQLQuery: jest.fn(),
}));
jest.mock('../src/trimQueryBody', () => ({
trimQueryBody: jest.fn(),
}));

global.console.error = jest.fn();

describe('defaultFetcher', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('should handle graphql query', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
status: 200,
text: async () => JSON.stringify({ jsonBodyMock: 'jsonBodyMock' }),
headers: new global.Headers({
'Content-Type': 'application/json',
}),
}));
jsonToGraphQLQuery.mockImplementationOnce(() => 'jsonToGraphQLQueryMock');
trimQueryBody.mockImplementationOnce(() => 'trimQueryBodyMock');
const data = await defaultFetcher(fetchClient, 'http://example.com', {
isGraphQL: true,
body: {
query: 'queryMock',
},
});
expect(data).toMatchInlineSnapshot(`
Object {
"error": undefined,
"payload": Object {
"body": Object {
"jsonBodyMock": "jsonBodyMock",
},
"headers": Object {
"content-type": "application/json",
},
"ok": true,
"status": 200,
},
}
`);
expect(jsonToGraphQLQuery).toHaveBeenCalledWith('queryMock', { pretty: true });
});
it('should handle graphql query with missing body', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
status: 200,
text: async () => JSON.stringify({ jsonBodyMock: 'jsonBodyMock' }),
headers: new global.Headers({
'Content-Type': 'application/json',
}),
}));
jsonToGraphQLQuery.mockImplementationOnce(() => 'jsonToGraphQLQueryMock');
trimQueryBody.mockImplementationOnce(() => 'trimQueryBodyMock');
const data = await defaultFetcher(fetchClient, 'http://example.com', {
isGraphQL: true,
body: null,
});
expect(data).toMatchInlineSnapshot(`
Object {
"error": undefined,
"payload": Object {
"body": Object {
"jsonBodyMock": "jsonBodyMock",
},
"headers": Object {
"content-type": "application/json",
},
"ok": true,
"status": 200,
},
}
`);
});
it('should handle graphql query with existing query', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
status: 200,
text: async () => JSON.stringify({ jsonBodyMock: 'jsonBodyMock' }),
headers: new global.Headers({
'Content-Type': 'application/json',
}),
}));
jsonToGraphQLQuery.mockImplementationOnce(() => 'jsonToGraphQLQueryMock');
trimQueryBody.mockImplementationOnce(() => 'trimQueryBodyMock');
const data = await defaultFetcher(fetchClient, 'http://example.com', {
isGraphQL: true,
existingQuery: 'existingQueryMock',
body: {
query: 'queryMock',
},
});
expect(data).toMatchInlineSnapshot(`
Object {
"error": undefined,
"payload": Object {
"body": Object {
"jsonBodyMock": "jsonBodyMock",
},
"headers": Object {
"content-type": "application/json",
},
"ok": true,
"status": 200,
},
}
`);
expect(jsonToGraphQLQuery).toHaveBeenCalledWith('trimQueryBodyMock', { pretty: true });
expect(trimQueryBody).toHaveBeenCalledWith('queryMock', 'existingQueryMock');
});

it('should handle graphql query with existing query and no body', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
status: 200,
text: async () => JSON.stringify({ jsonBodyMock: 'jsonBodyMock' }),
headers: new global.Headers({
'Content-Type': 'application/json',
}),
}));
jsonToGraphQLQuery.mockImplementationOnce(() => 'jsonToGraphQLQueryMock');
trimQueryBody.mockImplementationOnce(() => 'trimQueryBodyMock');
const data = await defaultFetcher(fetchClient, 'http://example.com', {
isGraphQL: true,
existingQuery: 'existingQueryMock',
body: null,
});
expect(data).toMatchInlineSnapshot(`
Object {
"error": undefined,
"payload": Object {
"body": Object {
"jsonBodyMock": "jsonBodyMock",
},
"headers": Object {
"content-type": "application/json",
},
"ok": true,
"status": 200,
},
}
`);
});

it('should return payload and undefined error when status 200', async () => {
const fetchClient = jest.fn(async () => ({
ok: true,
Expand Down
38 changes: 38 additions & 0 deletions packages/fetchye-core/__tests__/trimOptions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
import { trimOptions } from '../src/trimOptions';
import { trimQueryBody } from '../src/trimQueryBody';

jest.mock('json-to-graphql-query', () => ({
jsonToGraphQLQuery: jest.fn(),
}));
jest.mock('../src/trimQueryBody', () => ({
trimQueryBody: jest.fn(),
}));

describe('trimOptions', () => {
it('should return options if isGraphQL is false', () => {
const options = { isGraphQL: false };
expect(trimOptions(options)).toEqual(options);
});
it('should return options if isGraphQL is true and existingQuery is not provided', () => {
const options = { isGraphQL: true, body: { query: 'query' } };
jsonToGraphQLQuery.mockImplementation((value) => value);
const expectedBody = JSON.stringify({
query: 'query',
});
expect(trimOptions(options)).toEqual({ ...options, body: expectedBody });
});
it('should return options with trimmed query if isGraphQL is true and existingQuery is provided', () => {
const options = { isGraphQL: true, existingQuery: 'existingQuery', body: { query: 'query' } };
const trimmedQuery = 'trimmedQuery';
trimQueryBody.mockReturnValue(trimmedQuery);
jsonToGraphQLQuery.mockImplementation((value) => value);
const expectedBody = JSON.stringify({
query: 'trimmedQuery',
});
expect(trimOptions(options)).toEqual({ ...options, body: expectedBody });
});
it('should handle if options is not provided', () => {
expect(trimOptions()).toEqual(undefined);
});
});
Loading
Loading