Skip to content

Commit

Permalink
Get tests for new args, fields manager API
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Oct 11, 2018
1 parent ea12dd9 commit 63fb1b1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 19 deletions.
4 changes: 4 additions & 0 deletions client/dist/js/bundle.js

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions client/src/lib/dependency-injection/ApolloGraphqlManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class ApolloGraphqlManager {
...configDefaults,
...config,
};
// Fields array is mutated by reference, so this has to be dereferenced from the configDefaults
mergedConfig.fields = [
...mergedConfig.fields
];
const {
apolloConfig,
...otherConfig
Expand Down Expand Up @@ -132,10 +136,14 @@ class ApolloGraphqlManager {

/**
* Adds a param to the query
* @param param
* @param string name
* @param string type
* @returns {ApolloGraphqlManager}
*/
addParam(name, type) {
if (!name || !type) {
throw new Error('addParam must be passed a name and type parameter');
}
return this.addParams({
[name]: type
});
Expand All @@ -148,22 +156,25 @@ class ApolloGraphqlManager {
*/
addParams(params = {}) {
const existing = this.config.params;
this.config.params = [
this.config.params = {
...existing,
...params,
];
};

return this;
}

/**
* Adds an arg to the query
* @param param
* @param string path The path to the field where the args are applied
* @param string name
* @param string variableName
*
* @returns {ApolloGraphqlManager}
*/
addArg(path = ROOT_FIELD, name, type) {
addArg(path = ROOT_FIELD, name, variableName) {
return this.addArgs(path, {
[name]: type
[name]: variableName
});
}

Expand Down Expand Up @@ -423,6 +434,7 @@ Tried to use template '${name}', which could not be found. Please make sure that
// process any template functions that may have been made available
return expression(config);
});

return gql(template.strings, ...expressed);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global jest, jasmine, describe, beforeEach, it, pit, expect, process */

import ApolloGraphqlManager from '../ApolloGraphqlManager';
import { getFields } from '../graphql/helpers';

const createMock = (config = {}, templates = {}, fragments = {}) => (
new ApolloGraphqlManager(config, templates, fragments)
Expand Down Expand Up @@ -51,20 +52,23 @@ describe('ApolloGraphqlManager', () => {

it('adds params', () => {
const manager = createMock();
manager.addParam('test');
manager.addParams(['rest', 'fest']);
manager.addParam('test', 'String');
manager.addParams({
foo: 'Boolean',
baz: 'Int!'
});

const params = manager.getConfig().params;
expect(Array.isArray(params)).toBe(true);
expect(params).toContain('test');
expect(params).toContain('rest');
expect(params).toContain('fest');
expect(typeof params).toBe('object');
expect(params.test).toBe('String');
expect(params.foo).toBe('Boolean');
expect(params.baz).toBe('Int!');
});

it('adds fields', () => {
const manager = createMock();
manager.addField('test');
manager.addFields(['rest', 'fest']);
manager.addField('root', 'test');
manager.addFields('root', ['rest', 'fest']);

const fields = manager.getConfig().fields;
expect(Array.isArray(fields)).toBe(true);
Expand All @@ -73,6 +77,44 @@ describe('ApolloGraphqlManager', () => {
expect(fields).toContain('fest');
});

it('adds nested fields', () => {
const manager = createMock();
manager.addFields('root', ['FirstName', 'Surname']);
manager.addFields('root', ['Avatar', []]);
manager.addField('root/Avatar', 'URL');

const fields = manager.getConfig().fields;
expect(Array.isArray(fields)).toBe(true);
expect(fields).toContain('FirstName');
expect(fields).toContain('Surname');
expect(fields).toContain('Avatar');
const i = fields.indexOf('Avatar') + 1;
expect(Array.isArray(fields[i])).toBe(true);
expect(fields[i]).toContain('URL');
expect(() => manager.addFields('root/Surname', 'Foo')).toThrow();
});

it('adds args to fields', () => {
const manager = createMock({ pagination: false });
manager.addFields('root', ['FirstName', 'Surname']);
manager.addFields('root', ['Avatar', []]);
manager.addField('root/Avatar', 'URL');

manager.addArg('root/FirstName', 'Test', 'SomeVar');
manager.addArgs('root/Avatar/URL', {
Relative: 'SomeBool',
});
// eslint-disable-next-line no-unused-expressions
manager.setTemplate`query test { ${getFields} }`;

const AST = manager.getGraphqlAST();

expect(AST.kind).toBe('Document');
const compiledQuery = AST.loc.source.body;
expect(compiledQuery).toMatch(/FirstName\s*\(Test:\s*\$SomeVar\s*\)/);
expect(compiledQuery).toMatch(/URL\s*\(Relative:\s*\$SomeBool\s*\)/);
});

it('uses fragments', () => {
const manager = createMock();
manager.useFragment('testFragment');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestReadQuery', (manager) => {
manager.addField('Plates');
manager.addField('root', 'Plates');
manager.transformApolloConfig('props', () => (previous) => ({
...previous,
qux: 'baz'
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestReadOneQuery', (manager) => {
manager.addField('Plates');
manager.addField('root', 'Plates');
});
}
);
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestUpdateQuery', (manager) => {
manager.addField('Plates');
manager.addField('root', 'Plates');
});
}
);
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestDeleteQuery', (manager) => {
manager.addField('ID');
manager.addField('root', 'ID');
});
}
);
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Dynamic graphql injection', () => {
'test-transform',
(updater) => {
updater.test('MyTestCreateQuery', (manager) => {
manager.addField('Claws');
manager.addField('root', 'Claws');
});
}
);
Expand Down

0 comments on commit 63fb1b1

Please sign in to comment.