Просмотр исходного кода

refactor(testing): Remove duplication from createErrorResultGuard calls

Michael Bromley 5 лет назад
Родитель
Сommit
46094e3faf

+ 7 - 8
packages/core/e2e/authentication-strategy.e2e-spec.ts

@@ -54,11 +54,10 @@ describe('AuthenticationStrategy', () => {
         await server.destroy();
     });
 
-    const currentUserGuard: ErrorResultGuard<CurrentUserFragment> = createErrorResultGuard<
-        CurrentUserFragment
-    >(input => input.identifier != null);
-
-    const customerGuard: ErrorResultGuard<CustomerFragment> = createErrorResultGuard<CustomerFragment>(
+    const currentUserGuard: ErrorResultGuard<CurrentUserFragment> = createErrorResultGuard(
+        input => input.identifier != null,
+    );
+    const customerGuard: ErrorResultGuard<CustomerFragment> = createErrorResultGuard(
         input => input.emailAddress != null,
     );
 
@@ -193,9 +192,9 @@ describe('AuthenticationStrategy', () => {
         });
 
         it('registerCustomerAccount with external email', async () => {
-            const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard<{
-                success: boolean;
-            }>(input => input.success != null);
+            const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard(
+                input => input.success != null,
+            );
 
             const { registerCustomerAccount } = await shopClient.query<Register.Mutation, Register.Variables>(
                 REGISTER_ACCOUNT,

+ 5 - 5
packages/core/e2e/customer.e2e-spec.ts

@@ -14,7 +14,7 @@ import gql from 'graphql-tag';
 import path from 'path';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import { CUSTOMER_FRAGMENT } from './graphql/fragments';
 import {
@@ -80,7 +80,7 @@ describe('Customer resolver', () => {
     let secondCustomer: GetCustomerList.Items;
     let thirdCustomer: GetCustomerList.Items;
 
-    const customerErrorGuard: ErrorResultGuard<CustomerFragment> = createErrorResultGuard<CustomerFragment>(
+    const customerErrorGuard: ErrorResultGuard<CustomerFragment> = createErrorResultGuard(
         input => !!input.emailAddress,
     );
 
@@ -366,9 +366,9 @@ describe('Customer resolver', () => {
     });
 
     describe('orders', () => {
-        const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard<
-            UpdatedOrderFragment
-        >(input => !!input.lines);
+        const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard(
+            input => !!input.lines,
+        );
 
         it(`lists that user\'s orders`, async () => {
             // log in as first customer

+ 3 - 3
packages/core/e2e/global-settings.e2e-spec.ts

@@ -25,9 +25,9 @@ describe('GlobalSettings resolver', () => {
     });
     let globalSettings: GlobalSettingsFragment;
 
-    const globalSettingsGuard: ErrorResultGuard<GlobalSettingsFragment> = createErrorResultGuard<
-        GlobalSettingsFragment
-    >(input => !!input.availableLanguages);
+    const globalSettingsGuard: ErrorResultGuard<GlobalSettingsFragment> = createErrorResultGuard(
+        input => !!input.availableLanguages,
+    );
 
     beforeAll(async () => {
         await server.init({

+ 4 - 4
packages/core/e2e/order-channel.e2e-spec.ts

@@ -8,7 +8,7 @@ import {
 import path from 'path';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import {
     AssignProductsToChannel,
@@ -125,9 +125,9 @@ describe('Channelaware orders', () => {
         await server.destroy();
     });
 
-    const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard<
-        UpdatedOrderFragment
-    >(input => !!input.lines);
+    const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard(
+        input => !!input.lines,
+    );
 
     it('creates order on current channel', async () => {
         shopClient.setChannelToken(SECOND_CHANNEL_TOKEN);

+ 6 - 6
packages/core/e2e/order-fulfillment.e2e-spec.ts

@@ -60,12 +60,12 @@ const testFulfillmentHandler = new FulfillmentHandler({
 });
 
 describe('Order fulfillments', () => {
-    const orderGuard: ErrorResultGuard<TestOrderWithPaymentsFragment> = createErrorResultGuard<
-        TestOrderWithPaymentsFragment
-    >(input => !!input.lines);
-    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard<
-        FulfillmentFragment
-    >(input => !!input.id);
+    const orderGuard: ErrorResultGuard<TestOrderWithPaymentsFragment> = createErrorResultGuard(
+        input => !!input.lines,
+    );
+    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard(
+        input => !!input.id,
+    );
 
     let order: TestOrderWithPaymentsFragment;
     let f1Id: string;

+ 1 - 1
packages/core/e2e/order-process.e2e-spec.ts

@@ -85,7 +85,7 @@ describe('Order process', () => {
 
     const orderErrorGuard: ErrorResultGuard<
         TestOrderFragmentFragment | OrderFragment
-    > = createErrorResultGuard<TestOrderFragmentFragment | OrderFragment>(input => !!input.total);
+    > = createErrorResultGuard(input => !!input.total);
 
     const { server, adminClient, shopClient } = createTestEnvironment(
         mergeConfig(testConfig, {

+ 4 - 4
packages/core/e2e/order-promotion.e2e-spec.ts

@@ -82,7 +82,7 @@ describe('Promotions applied to Orders', () => {
     });
 
     type OrderSuccessResult = UpdatedOrderFragment | TestOrderFragmentFragment;
-    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard<OrderSuccessResult>(
+    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
         input => !!input.lines,
     );
 
@@ -719,9 +719,9 @@ describe('Promotions applied to Orders', () => {
 
     describe('per-customer usage limit', () => {
         const TEST_COUPON_CODE = 'TESTCOUPON';
-        const orderGuard: ErrorResultGuard<TestOrderWithPaymentsFragment> = createErrorResultGuard<
-            TestOrderWithPaymentsFragment
-        >(input => !!input.lines);
+        const orderGuard: ErrorResultGuard<TestOrderWithPaymentsFragment> = createErrorResultGuard(
+            input => !!input.lines,
+        );
         let promoWithUsageLimit: PromotionFragment;
 
         beforeAll(async () => {

+ 1 - 1
packages/core/e2e/order-taxes.e2e-spec.ts

@@ -27,7 +27,7 @@ describe('Order taxes', () => {
     });
 
     type OrderSuccessResult = UpdatedOrderFragment | TestOrderFragmentFragment;
-    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard<OrderSuccessResult>(
+    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
         input => !!input.lines,
     );
     let products: GetProductsWithVariantPrices.Items[];

+ 5 - 9
packages/core/e2e/order.e2e-spec.ts

@@ -97,16 +97,12 @@ describe('Orders resolver', () => {
 
     const orderGuard: ErrorResultGuard<
         TestOrderFragmentFragment | CanceledOrderFragment
-    > = createErrorResultGuard<TestOrderFragmentFragment | CanceledOrderFragment>(input => !!input.lines);
-    const paymentGuard: ErrorResultGuard<PaymentFragment> = createErrorResultGuard<PaymentFragment>(
-        input => !!input.state,
-    );
-    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard<
-        FulfillmentFragment
-    >(input => !!input.method);
-    const refundGuard: ErrorResultGuard<RefundFragment> = createErrorResultGuard<RefundFragment>(
-        input => !!input.items,
+    > = createErrorResultGuard(input => !!input.lines);
+    const paymentGuard: ErrorResultGuard<PaymentFragment> = createErrorResultGuard(input => !!input.state);
+    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard(
+        input => !!input.method,
     );
+    const refundGuard: ErrorResultGuard<RefundFragment> = createErrorResultGuard(input => !!input.items);
 
     beforeAll(async () => {
         await server.init({

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

@@ -52,9 +52,9 @@ import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 describe('Product resolver', () => {
     const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
 
-    const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard<
-        ProductWithOptionsFragment
-    >(input => !!input.optionGroups);
+    const removeOptionGuard: ErrorResultGuard<ProductWithOptionsFragment> = createErrorResultGuard(
+        input => !!input.optionGroups,
+    );
 
     beforeAll(async () => {
         await server.init({

+ 2 - 2
packages/core/e2e/promotion.e2e-spec.ts

@@ -5,7 +5,7 @@ import gql from 'graphql-tag';
 import path from 'path';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import { PROMOTION_FRAGMENT } from './graphql/fragments';
 import {
@@ -50,7 +50,7 @@ describe('Promotion resolver', () => {
     ];
     let promotion: Promotion.Fragment;
 
-    const promotionGuard: ErrorResultGuard<PromotionFragment> = createErrorResultGuard<PromotionFragment>(
+    const promotionGuard: ErrorResultGuard<PromotionFragment> = createErrorResultGuard(
         input => !!input.couponCode,
     );
 

+ 1 - 1
packages/core/e2e/shipping-method-eligibility.e2e-spec.ts

@@ -98,7 +98,7 @@ describe('ShippingMethod resolver', () => {
 
     const orderGuard: ErrorResultGuard<
         UpdatedOrderFragment | TestOrderFragmentFragment
-    > = createErrorResultGuard<UpdatedOrderFragment>(input => !!input.lines);
+    > = createErrorResultGuard(input => !!input.lines);
 
     let singleLineShippingMethod: ShippingMethodFragment;
     let multiLineShippingMethod: ShippingMethodFragment;

+ 6 - 6
packages/core/e2e/shop-auth.e2e-spec.ts

@@ -86,13 +86,13 @@ class TestEmailPlugin implements OnModuleInit {
     }
 }
 
-const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard<{
-    success: boolean;
-}>(input => input.success != null);
+const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard(
+    input => input.success != null,
+);
 
-const currentUserErrorGuard: ErrorResultGuard<CurrentUserShopFragment> = createErrorResultGuard<
-    CurrentUserShopFragment
->(input => input.identifier != null);
+const currentUserErrorGuard: ErrorResultGuard<CurrentUserShopFragment> = createErrorResultGuard(
+    input => input.identifier != null,
+);
 
 describe('Shop auth & accounts', () => {
     const { server, adminClient, shopClient } = createTestEnvironment(

+ 4 - 4
packages/core/e2e/shop-customer.e2e-spec.ts

@@ -6,7 +6,7 @@ import path from 'path';
 import { skip } from 'rxjs/operators';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import {
     AttemptLogin,
@@ -40,9 +40,9 @@ describe('Shop customers', () => {
     const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
     let customer: GetCustomer.Customer;
 
-    const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard<{
-        success: boolean;
-    }>(input => input.success != null);
+    const successErrorGuard: ErrorResultGuard<{ success: boolean }> = createErrorResultGuard(
+        input => input.success != null,
+    );
 
     beforeAll(async () => {
         await server.init({

+ 1 - 1
packages/core/e2e/shop-order.e2e-spec.ts

@@ -103,7 +103,7 @@ describe('Shop orders', () => {
         | TestOrderFragmentFragment
         | TestOrderWithPaymentsFragment
         | ActiveOrderCustomerFragment;
-    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard<OrderSuccessResult>(
+    const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
         input => !!input.lines,
     );
 

+ 4 - 4
packages/core/e2e/stock-control.e2e-spec.ts

@@ -66,11 +66,11 @@ describe('Stock control', () => {
 
     const orderGuard: ErrorResultGuard<
         TestOrderFragmentFragment | UpdatedOrderFragment
-    > = createErrorResultGuard<TestOrderFragmentFragment>(input => !!input.lines);
+    > = createErrorResultGuard(input => !!input.lines);
 
-    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard<
-        FulfillmentFragment
-    >(input => !!input.state);
+    const fulfillmentGuard: ErrorResultGuard<FulfillmentFragment> = createErrorResultGuard(
+        input => !!input.state,
+    );
 
     async function getProductWithStockMovement(productId: string) {
         const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(

+ 3 - 2
packages/testing/src/error-result-guard.ts

@@ -1,6 +1,7 @@
 declare function fail(error?: any): never;
 
 /**
+ * @description
  * Convenience method for creating an {@link ErrorResultGuard}. Takes a predicate function which
  * tests whether the input is considered successful (true) or an error result (false).
  *
@@ -10,7 +11,7 @@ declare function fail(error?: any): never;
  * @example
  * ```TypeScript
  * const orderResultGuard: ErrorResultGuard<AddItemToOrderResult>
- *   = createErrorResultGuard<AddItemToOrderResult>(order => !!order.lines);
+ *   = createErrorResultGuard(order => !!order.lines);
  * ```
  * @docsCategory testing
  */
@@ -29,7 +30,7 @@ export function createErrorResultGuard<T>(testFn: (input: T) => boolean): ErrorR
  * @example
  * ```TypeScript
  * const orderResultGuard: ErrorResultGuard<AddItemToOrderResult>
- *   = createErrorResultGuard<AddItemToOrderResult>(order => !!order.lines);
+ *   = createErrorResultGuard(order => !!order.lines);
  *
  * it('errors when quantity is negative', async () => {
  *    const { addItemToOrder } = await shopClient.query<AddItemToOrder.Query, AddItemToOrder.Mutation>(ADD_ITEM_TO_ORDER, {