Skip to content

Commit

Permalink
test: validate price schema
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 27, 2023
1 parent e34cfb2 commit 92466c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/validation/CampaignSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const validCampaign = {
currency: "GBP",
dailyBudget: 100,
billingType: "cpm",
price: 6,
price: "6",
dailyCap: 1,
startAt: parseISO("2030-07-18"),
endAt: parseISO("2030-07-20"),
Expand All @@ -46,6 +46,16 @@ it("should fail if the campaign start date is in past", () => {
expect(() =>
CampaignSchema(prices).validateSync(c),
).toThrowErrorMatchingInlineSnapshot(
`"Start Date must be minimum of 2 days from today"`,
'"Start Date must be minimum of 2 days from today"',
);
});

it("should fail if the campaign price is less than allowed price", () => {
const c = produce(validCampaign, (draft) => {
draft.price = "5";
});

expect(() =>
CampaignSchema(prices).validateSync(c),
).toThrowErrorMatchingInlineSnapshot('"CPM price must be 6 or higher"');
});
15 changes: 8 additions & 7 deletions src/validation/CampaignSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
boolean,
date,
number,
NumberSchema,
object,
ref,
string,
StringSchema,
} from "yup";
import { startOfDay } from "date-fns";
import { twoDaysOut } from "form/DateFieldHelpers";
Expand Down Expand Up @@ -73,7 +73,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
)
.min(1, "At least one country must be targeted")
.default([]),
price: number()
price: string()
.label("Price")
.when("billingType", {
is: (b: string) => b === "cpc",
Expand All @@ -94,7 +94,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
prices,
CampaignFormat.PushNotification,
BillingType.Cpm,
"5",
"6",
schema,
),
})
Expand All @@ -106,7 +106,7 @@ export const CampaignSchema = (prices: AdvertiserPriceFragment[]) =>
prices,
CampaignFormat.NewsDisplayAd,
BillingType.Cpm,
"9",
"10",
schema,
),
})
Expand Down Expand Up @@ -180,14 +180,15 @@ export function findPrice(
format: CampaignFormat,
billingType: BillingType,
defaultPrice: string,
schema: NumberSchema<number | undefined, AnyObject, undefined, "">,
schema: StringSchema<string | undefined, AnyObject, undefined, "">,
) {
const found = prices.find(
(p) => p.format === format && p.billingType === billingType,
);
const price = BigNumber(found?.price ?? defaultPrice);
return schema.min(
price.toNumber(),
return schema.test(
"is-lte-price",
`${billingType} price must be ${price} or higher`,
(value) => (value ? price.isLessThanOrEqualTo(value) : true),
);
}

0 comments on commit 92466c0

Please sign in to comment.