Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 26, 2023
1 parent 17a430f commit 7172476
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/validation/CampaignSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { parseISO } from "date-fns";
import { produce } from "immer";
import { CampaignFormat, CampaignPacingStrategies } from "../graphql/types";
import {
BillingType,
CampaignFormat,
CampaignPacingStrategies,
} from "graphql/types";
import { CampaignSchema } from "./CampaignSchema";
import { AdvertiserPriceFragment } from "graphql/advertiser.generated";

const prices: AdvertiserPriceFragment[] = [
{
format: CampaignFormat.PushNotification,
price: "6",
billingType: BillingType.Cpm,
},
];

const validCampaign = {
name: "some campaign",
Expand All @@ -22,7 +35,7 @@ const validCampaign = {
};

it("should pass on a valid object", () => {
CampaignSchema.validateSync(validCampaign);
CampaignSchema(prices).validateSync(validCampaign);
});

it("should fail if the campaign start date is in past", () => {
Expand All @@ -31,7 +44,7 @@ it("should fail if the campaign start date is in past", () => {
});

expect(() =>
CampaignSchema.validateSync(c),
CampaignSchema(prices).validateSync(c),
).toThrowErrorMatchingInlineSnapshot(
`"Start Date must be minimum of 2 days from today"`,
);
Expand Down
16 changes: 10 additions & 6 deletions src/validation/CampaignSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TrailingAsteriskRegex } from "validation/regex";
import { CreativeSchema } from "validation/CreativeSchema";
import { BillingType, CampaignFormat } from "graphql/types";
import { AdvertiserPriceFragment } from "graphql/advertiser.generated";
import BigNumber from "bignumber.js";

export const MIN_PER_DAY = 33;
export const MIN_PER_CAMPAIGN = 100;
Expand Down Expand Up @@ -81,7 +82,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
prices,
CampaignFormat.PushNotification,
BillingType.Cpc,
0.1,
"0.1",
schema,
),
})
Expand All @@ -93,7 +94,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
prices,
CampaignFormat.PushNotification,
BillingType.Cpm,
5,
"5",
schema,
),
})
Expand All @@ -105,7 +106,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
prices,
CampaignFormat.NewsDisplayAd,
BillingType.Cpm,
9,
"9",
schema,
),
})
Expand Down Expand Up @@ -178,12 +179,15 @@ export function findPrice(
prices: AdvertiserPriceFragment[],
format: CampaignFormat,
billingType: BillingType,
defaultPrice: number,
defaultPrice: string,
schema: NumberSchema<number | undefined, AnyObject, undefined, "">,
) {
const found = prices.find(
(p) => p.format === format && p.billingType === billingType,
);
const price = Number(found?.price) ?? defaultPrice;
return schema.min(price, `${billingType} price must be ${price} or higher`);
const price = BigNumber(found?.price ?? defaultPrice);
return schema.min(
price.toNumber(),
`${billingType} price must be ${price} or higher`,
);
}

0 comments on commit 7172476

Please sign in to comment.