Browse Source

refactor(testing): Remove TestClient, use SimpleGraphQLClient directly

The TestClient was a redundant layer of indirection.
Michael Bromley 6 years ago
parent
commit
c05730c29b

+ 8 - 8
packages/core/e2e/default-search-plugin.e2e-spec.ts

@@ -2,7 +2,7 @@ import { pick } from '@vendure/common/lib/pick';
 import { mergeConfig } from '@vendure/core';
 import { DefaultSearchPlugin } from '@vendure/core';
 import { facetValueCollectionFilter } from '@vendure/core/dist/config/collection/default-collection-filters';
-import { createTestEnvironment, TestClient } from '@vendure/testing';
+import { createTestEnvironment, SimpleGraphQLClient } from '@vendure/testing';
 import gql from 'graphql-tag';
 import path from 'path';
 
@@ -51,7 +51,7 @@ describe('Default search plugin', () => {
         await server.destroy();
     });
 
-    async function testGroupByProduct(client: TestClient) {
+    async function testGroupByProduct(client: SimpleGraphQLClient) {
         const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
             SEARCH_PRODUCTS_SHOP,
             {
@@ -63,7 +63,7 @@ describe('Default search plugin', () => {
         expect(result.search.totalItems).toBe(20);
     }
 
-    async function testNoGrouping(client: TestClient) {
+    async function testNoGrouping(client: SimpleGraphQLClient) {
         const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
             SEARCH_PRODUCTS_SHOP,
             {
@@ -75,7 +75,7 @@ describe('Default search plugin', () => {
         expect(result.search.totalItems).toBe(34);
     }
 
-    async function testMatchSearchTerm(client: TestClient) {
+    async function testMatchSearchTerm(client: SimpleGraphQLClient) {
         const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
             SEARCH_PRODUCTS_SHOP,
             {
@@ -92,7 +92,7 @@ describe('Default search plugin', () => {
         ]);
     }
 
-    async function testMatchFacetIds(client: TestClient) {
+    async function testMatchFacetIds(client: SimpleGraphQLClient) {
         const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
             SEARCH_PRODUCTS_SHOP,
             {
@@ -112,7 +112,7 @@ describe('Default search plugin', () => {
         ]);
     }
 
-    async function testMatchCollectionId(client: TestClient) {
+    async function testMatchCollectionId(client: SimpleGraphQLClient) {
         const result = await client.query<SearchProductsShop.Query, SearchProductsShop.Variables>(
             SEARCH_PRODUCTS_SHOP,
             {
@@ -129,7 +129,7 @@ describe('Default search plugin', () => {
         ]);
     }
 
-    async function testSinglePrices(client: TestClient) {
+    async function testSinglePrices(client: SimpleGraphQLClient) {
         const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
             SEARCH_GET_PRICES,
             {
@@ -155,7 +155,7 @@ describe('Default search plugin', () => {
         ]);
     }
 
-    async function testPriceRanges(client: TestClient) {
+    async function testPriceRanges(client: SimpleGraphQLClient) {
         const result = await client.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
             SEARCH_GET_PRICES,
             {

+ 3 - 3
packages/core/e2e/order.e2e-spec.ts

@@ -1,6 +1,6 @@
 /* tslint:disable:no-non-null-assertion */
 import { pick } from '@vendure/common/lib/pick';
-import { createTestEnvironment, TestClient } from '@vendure/testing';
+import { createTestEnvironment, SimpleGraphQLClient } from '@vendure/testing';
 import gql from 'graphql-tag';
 import path from 'path';
 
@@ -1193,8 +1193,8 @@ describe('Orders resolver', () => {
 });
 
 async function createTestOrder(
-    adminClient: TestClient,
-    shopClient: TestClient,
+    adminClient: SimpleGraphQLClient,
+    shopClient: SimpleGraphQLClient,
     emailAddress: string,
     password: string,
 ): Promise<{

+ 2 - 2
packages/core/e2e/utils/await-running-jobs.ts

@@ -1,4 +1,4 @@
-import { TestClient } from '@vendure/testing';
+import { SimpleGraphQLClient } from '@vendure/testing';
 
 import { GetRunningJobs, JobState } from '../graphql/generated-e2e-admin-types';
 import { GET_RUNNING_JOBS } from '../graphql/shared-definitions';
@@ -7,7 +7,7 @@ import { GET_RUNNING_JOBS } from '../graphql/shared-definitions';
  * For mutation which trigger background jobs, this can be used to "pause" the execution of
  * the test until those jobs have completed;
  */
-export async function awaitRunningJobs(adminClient: TestClient, timeout: number = 5000) {
+export async function awaitRunningJobs(adminClient: SimpleGraphQLClient, timeout: number = 5000) {
     let runningJobs = 0;
     const startTime = +new Date();
     let timedOut = false;

+ 3 - 3
packages/core/e2e/utils/test-order-utils.ts

@@ -1,7 +1,7 @@
 /* tslint:disable:no-non-null-assertion */
 import { ID } from '@vendure/common/lib/shared-types';
 import { PaymentMethodHandler } from '@vendure/core';
-import { TestClient } from '@vendure/testing';
+import { SimpleGraphQLClient } from '@vendure/testing';
 
 import {
     AddPaymentToOrder,
@@ -18,7 +18,7 @@ import {
     TRANSITION_TO_STATE,
 } from '../graphql/shop-definitions';
 
-export async function proceedToArrangingPayment(shopClient: TestClient): Promise<ID> {
+export async function proceedToArrangingPayment(shopClient: SimpleGraphQLClient): Promise<ID> {
     await shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(SET_SHIPPING_ADDRESS, {
         input: {
             fullName: 'name',
@@ -46,7 +46,7 @@ export async function proceedToArrangingPayment(shopClient: TestClient): Promise
 }
 
 export async function addPaymentToOrder(
-    shopClient: TestClient,
+    shopClient: SimpleGraphQLClient,
     handler: PaymentMethodHandler,
 ): Promise<NonNullable<AddPaymentToOrder.Mutation['addPaymentToOrder']>> {
     const result = await shopClient.query<AddPaymentToOrder.Mutation, AddPaymentToOrder.Variables>(

+ 19 - 5
packages/testing/src/create-test-environment.ts

@@ -1,18 +1,32 @@
 import { VendureConfig } from '@vendure/core';
 
-import { TestClient } from './test-client';
+import { SimpleGraphQLClient } from './simple-graphql-client';
 import { TestServer } from './test-server';
 
 export interface TestEnvironment {
     server: TestServer;
-    adminClient: TestClient;
-    shopClient: TestClient;
+    /**
+     * @description
+     * A GraphQL client configured for the Admin API.
+     */
+    adminClient: SimpleGraphQLClient;
+    /**
+     * @description
+     * A GraphQL client configured for the Shop API.
+     */
+    shopClient: SimpleGraphQLClient;
 }
 
 export function createTestEnvironment(config: Required<VendureConfig>): TestEnvironment {
     const server = new TestServer(config);
-    const adminClient = new TestClient(config, config.adminApiPath);
-    const shopClient = new TestClient(config, config.shopApiPath);
+    const adminClient = new SimpleGraphQLClient(
+        config,
+        `http://localhost:${config.port}/${config.adminApiPath}`,
+    );
+    const shopClient = new SimpleGraphQLClient(
+        config,
+        `http://localhost:${config.port}/${config.shopApiPath}`,
+    );
     return {
         server,
         adminClient,

+ 0 - 1
packages/testing/src/index.ts

@@ -1,5 +1,4 @@
 export * from './simple-graphql-client';
-export * from './test-client';
 export * from './test-server';
 export * from './config/test-config';
 export * from './create-test-environment';

+ 0 - 20
packages/testing/src/test-client.ts

@@ -1,20 +0,0 @@
-import { VendureConfig } from '@vendure/core';
-
-import { testConfig } from './config/test-config';
-import { SimpleGraphQLClient } from './simple-graphql-client';
-import { getDefaultChannelToken } from './utils/get-default-channel-token';
-
-// tslint:disable:no-console
-/**
- * A GraphQL client for use in e2e tests.
- */
-export class TestClient extends SimpleGraphQLClient {
-    constructor(config: Required<VendureConfig>, apiPath: string) {
-        super(config, `http://localhost:${testConfig.port}/${apiPath}`);
-    }
-
-    async init() {
-        const token = await getDefaultChannelToken(false);
-        this.setChannelToken(token);
-    }
-}