Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 22, 2023
1 parent 9018ac3 commit 7290b2e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/user/library/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("pricing logic (read)", () => {
});
});
const campaignForm = editCampaignValues(campaign, "abc");
expect(campaignForm.price).toEqual(7);
expect(campaignForm.price).toEqual("7");
expect(campaignForm.billingType).toEqual("cpm");
});

Expand All @@ -152,7 +152,7 @@ describe("pricing logic (read)", () => {
});
});
const campaignForm = editCampaignValues(campaign, "abc");
expect(campaignForm.price).toEqual(1);
expect(campaignForm.price).toEqual("1");
expect(campaignForm.billingType).toEqual("cpc");
});

Expand All @@ -161,7 +161,7 @@ describe("pricing logic (read)", () => {
c.adSets = [];
});
const formObject = editCampaignValues(campaign, "abc");
expect(formObject.price).toEqual(100);
expect(formObject.price).toEqual("100");
expect(formObject.billingType).toEqual("cpm");
});
});
Expand All @@ -183,7 +183,7 @@ describe("pricing logic (write)", () => {
it("should convert from CPM to per-impression values when populating a CPM creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpm",
price: 9,
price: "9",
});

expect(inputObject.price).toEqual("0.009");
Expand All @@ -193,7 +193,7 @@ describe("pricing logic (write)", () => {
it("should not convert CPC to per-impression values when populating a CPC creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpc",
price: 9,
price: "9",
});

expect(inputObject.price).toEqual("9");
Expand All @@ -203,7 +203,7 @@ describe("pricing logic (write)", () => {
it("should not convert CPV to per-impression values when populating a CPV creative", () => {
const inputObject = transformCreative(creative, {
billingType: "cpv",
price: 9,
price: "9",
});

expect(inputObject.price).toEqual("9");
Expand Down Expand Up @@ -254,7 +254,7 @@ describe("new form tests", () => {
isCreating: false,
name: "Test",
paymentType: PaymentType.Radom,
price: 6,
price: "6",
startAt: dateString,
state: "draft",
type: "paid",
Expand Down Expand Up @@ -569,7 +569,7 @@ describe("edit form tests", () => {
"isCreating": false,
"name": "My first campaign",
"paymentType": "RADOM",
"price": 6000,
"price": "6000",
"startAt": undefined,
"state": "active",
"type": "paid",
Expand Down
2 changes: 1 addition & 1 deletion src/user/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function editCampaignValues(
advertiserId,
newCreative: initialCreative,
currency: campaign.currency,
price: price.toNumber(),
price: price.toString(),
billingType: billingType,
validateStart: false,
budget: campaign.budget,
Expand Down
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 7290b2e

Please sign in to comment.