From 7a0ad299a62e5f3d9d4efedd04c939eed717e6b2 Mon Sep 17 00:00:00 2001 From: Mina Smart Date: Tue, 12 Sep 2017 13:05:56 -0400 Subject: [PATCH] move helpers into resources --- src/client.js | 26 +++----------------------- src/image-resource.js | 8 ++++++++ src/product-resource.js | 4 ++++ src/resource.js | 3 +-- test/client-test.js | 28 ++++++++++------------------ 5 files changed, 26 insertions(+), 43 deletions(-) create mode 100644 src/image-resource.js diff --git a/src/client.js b/src/client.js index 28bdd8b61..85281e8b3 100644 --- a/src/client.js +++ b/src/client.js @@ -1,11 +1,10 @@ import GraphQLJSClient from './graphql-client'; import Config from './config'; -import productHelpers from './product-helpers'; -import imageHelpers from './image-helpers'; import ProductResource from './product-resource'; import CollectionResource from './collection-resource'; import ShopResource from './shop-resource'; import CheckoutResource from './checkout-resource'; +import ImageResource from './image-resource'; import {version} from '../package.json'; // GraphQL @@ -17,26 +16,6 @@ import types from '../schema.json'; */ class Client { - /** - * A namespace providing utilities for product resources. - * @namespace - */ - static get Product() { - return { - Helpers: productHelpers - }; - } - - /** - * A namespace providing utilities for image resources. - * @namespace - */ - static get Image() { - return { - Helpers: imageHelpers - }; - } - /** * Primary entry point for building a new Client. */ @@ -64,10 +43,11 @@ class Client { } }); - this.product = new ProductResource(this.graphQLClient, productHelpers); + this.product = new ProductResource(this.graphQLClient); this.collection = new CollectionResource(this.graphQLClient); this.shop = new ShopResource(this.graphQLClient); this.checkout = new CheckoutResource(this.graphQLClient); + this.image = new ImageResource(this.graphQLClient); } } diff --git a/src/image-resource.js b/src/image-resource.js new file mode 100644 index 000000000..1a1d2fd46 --- /dev/null +++ b/src/image-resource.js @@ -0,0 +1,8 @@ +import Resource from './resource'; +import imageHelpers from './image-helpers'; + +export default class ImageResource extends Resource { + get helpers() { + return imageHelpers; + } +} diff --git a/src/product-resource.js b/src/product-resource.js index 54932d0e3..a748844bf 100644 --- a/src/product-resource.js +++ b/src/product-resource.js @@ -1,6 +1,7 @@ import Resource from './resource'; import defaultResolver from './default-resolver'; import {paginateProductConnectionsAndResolve} from './paginators'; +import productHelpers from './product-helpers'; // GraphQL import productNodeQuery from './graphql/productNodeQuery.graphql'; @@ -8,6 +9,9 @@ import productConnectionQuery from './graphql/productConnectionQuery.graphql'; import productByHandleQuery from './graphql/productByHandleQuery.graphql'; export default class ProductResource extends Resource { + get helpers() { + return productHelpers; + } /** * Fetches all products on the shop. diff --git a/src/resource.js b/src/resource.js index df334b479..45eea2851 100644 --- a/src/resource.js +++ b/src/resource.js @@ -1,6 +1,5 @@ export default class Resource { - constructor(client, helpers) { + constructor(client) { this.graphQLClient = client; - this.helpers = helpers; } } diff --git a/test/client-test.js b/test/client-test.js index c64487c0f..d7e06185d 100644 --- a/test/client-test.js +++ b/test/client-test.js @@ -3,13 +3,13 @@ import GraphQLJSClient from '../src/graphql-client'; import Config from '../src/config'; import Client from '../src/client'; import types from '../schema.json'; -import fetchMock from './isomorphic-fetch-mock'; // eslint-disable-line import/no-unresolved import {version} from '../package.json'; suite('client-test', () => { - teardown(() => { - fetchMock.restore(); - }); + const config = { + domain: 'sendmecats.myshopify.com', + storefrontAccessToken: 'abc123' + }; test('it instantiates a GraphQL client with the given config', () => { let passedTypeBundle; @@ -24,12 +24,7 @@ suite('client-test', () => { } } - const config = new Config({ - domain: 'sendmecats.myshopify.com', - storefrontAccessToken: 'abc123' - }); - - new Client(config, FakeGraphQLJSClient); // eslint-disable-line no-new + new Client(new Config(config), FakeGraphQLJSClient); // eslint-disable-line no-new assert.deepEqual(passedTypeBundle, types); assert.equal(passedUrl, 'https://sendmecats.myshopify.com/api/graphql'); @@ -43,18 +38,15 @@ suite('client-test', () => { }); test('it creates an instance of the GraphQLJSClient by default', () => { - const config = new Config({ - domain: 'sendmecats.myshopify.com', - storefrontAccessToken: 'abc123' - }); - - const client = new Client(config); + const client = Client.buildClient(config); assert.ok(GraphQLJSClient.prototype.isPrototypeOf(client.graphQLClient)); }); test('it has static helpers', () => { - assert.ok(Client.Product.Helpers); - assert.ok(Client.Image.Helpers); + const client = Client.buildClient(config); + + assert.ok(client.product.helpers); + assert.ok(client.image.helpers); }); });