Skip to content

Commit

Permalink
feat(invoices): moved waitFor util to test dir
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug committed Oct 24, 2024
1 parent 0e9edd8 commit dc524b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
26 changes: 26 additions & 0 deletions packages/test/src/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This helper keeps polling a condition function until it returns a value or times out.
* Use this instead of arbitrary `await new Promise()` calls.
*
* @example
* // This invoice will be defined, or an error is thrown after timeout
* const invoice = await waitFor(() => getInvoice(order.id));
*/
export async function waitFor<T>(
conditionFn: () => Promise<T | undefined> | T | undefined,
interval = 100,
timeout = 10000
) {
const startTime = Date.now();
let result: T | undefined;
let elapsedTime = 0;
while (elapsedTime < timeout) {
result = await conditionFn();
if (result) {
return result;
}
elapsedTime = Date.now() - startTime;
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw new Error(`'waitFor()' Failed to resolve a value after ${timeout}ms`);
}
2 changes: 1 addition & 1 deletion packages/vendure-plugin-invoices/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
} from '../src/ui/queries.graphql';
import { MockAccountingStrategy } from './mock-accounting-strategy';
import gql from 'graphql-tag';
import { waitFor } from '../../vendure-plugin-shipping-extensions/test/test-helpers';
import { waitFor } from '../../test/src/test-helpers';

let server: TestServer;
let adminClient: SimpleGraphQLClient;
Expand Down
31 changes: 1 addition & 30 deletions packages/vendure-plugin-shipping-extensions/test/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
import { defaultShippingEligibilityChecker, Promotion } from '@vendure/core';
import { SimpleGraphQLClient } from '@vendure/testing';
import { distanceBasedShippingCalculator } from '../src/config/distance-based-shipping-calculator';
import { gql } from 'graphql-tag';
import { ConfigArgInput } from '../../test/src/generated/shop-graphql';
import {
CreatePromotion,
CreatePromotionMutation,
CreatePromotionMutationVariables,
LanguageCode,
} from '../../test/src/generated/admin-graphql';

/**
* This helper keeps polling a condition function until it returns a value or times out.
* Use this instead of arbitrary `await new Promise()` calls.
*
* @example
* // This invoice will be defined, or an error is thrown after timeout
* const invoice = await waitFor(() => getInvoice(order.id));
*/
export async function waitFor<T>(
conditionFn: () => Promise<T | undefined> | T | undefined,
interval = 100,
timeout = 10000
) {
const startTime = Date.now();
let result: T | undefined;
let elapsedTime = 0;
while (elapsedTime < timeout) {
result = await conditionFn();
if (result) {
return result;
}
elapsedTime = Date.now() - startTime;
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw new Error(`'waitFor()' Failed to resolve a value after ${timeout}ms`);
}
import { distanceBasedShippingCalculator } from '../src/config/distance-based-shipping-calculator';

const CREATE_SHIPPING_METHOD = gql`
mutation CreateShippingMethod($input: CreateShippingMethodInput!) {
Expand Down

0 comments on commit dc524b7

Please sign in to comment.