import chai from "chai"; import { CampaignModel } from "../../../src/models/CampaignModel"; import { DEFAULT_CAMPAIGN } from "../fake/CampaignModelFake"; chai.should(); const expect = chai.expect; describe("CampaignModel", () => { let model: CampaignModel; beforeEach(() => { model = new CampaignModel({} as any); }); it("should test campaign isProgrammatic", () => { let isProgrammatic = model.isProgrammatic({ ...DEFAULT_CAMPAIGN, }); isProgrammatic.should.be.equal(false); isProgrammatic = model.isProgrammatic({ ...DEFAULT_CAMPAIGN, prog_deal_id: "dealId", }); isProgrammatic.should.be.equal(true); isProgrammatic = model.isProgrammatic({ ...DEFAULT_CAMPAIGN, prog_deal_id: undefined, }); isProgrammatic.should.be.equal(false); }); it("should handle multi envs/formats campaigns", () => { const isWeb = model.isMultiEnvironmentTypesWithWeb({ ...DEFAULT_CAMPAIGN, environment_type: "inapp,web", }); isWeb.should.be.equal(true); const isNotWeb = model.isMultiEnvironmentTypesWithWeb({ ...DEFAULT_CAMPAIGN, environment_type: "inapp", }); isNotWeb.should.be.equal(false); const isThumbnail = model.isMultiFormatWithThumbnail({ ...DEFAULT_CAMPAIGN, display_as: ["overlay_thumbnail", "interstitial"], }); isThumbnail.should.be.equal(true); const isNotThumbnail = model.isMultiFormatWithThumbnail({ ...DEFAULT_CAMPAIGN, display_as: ["interstitial"], }); isNotThumbnail.should.be.equal(false); }); describe("getDisplayAs", () => { it("should return display_as if ad_unit_types are not defined", () => { const result = model.getDisplayAs({ ...DEFAULT_CAMPAIGN, display_as: ["interstitial", "overlay_thumbnail"], }); expect(result).to.include("interstitial"); expect(result).to.include("overlay_thumbnail"); expect(result).to.have.lengthOf(2); }); it("should return available ad_unit_types if ad_unit_types are defined", () => { const result = model.getDisplayAs({ ...DEFAULT_CAMPAIGN, display_as: ["interstitial", "overlay_thumbnail"], ad_unit_types: { optin_video: { payback_cpm: 5, margin: 10 }, banner_320x50: { payback_cpm: 5, margin: 10 }, overlay_thumbnail: { payback_cpm: 5, margin: 10 }, }, }); expect(result).to.include("overlay_thumbnail"); expect(result).to.include("banner_320x50"); expect(result).to.include("optin_video"); expect(result).to.have.lengthOf(3); }); }); describe("getMargin", () => { it("should return margin if ad_unit_types are not defined", () => { const result = model.getMargin({ ...DEFAULT_CAMPAIGN, margin: 50, }); expect(result).to.eq(50); }); it("should return available ad_unit_types if ad_unit_types are defined", () => { const result = model.getMargin( { ...DEFAULT_CAMPAIGN, margin: 50, ad_unit_types: { optin_video: { payback_cpm: 5, margin: 10 }, banner_320x50: { payback_cpm: 5, margin: 20 }, overlay_thumbnail: { payback_cpm: 5, margin: 30 }, interstitial: { payback_cpm: 5, margin: 40 }, }, }, "banner_320x50" ); expect(result).to.eq(20); }); it("should use campaign margin if no ad unit type is provided", () => { const result = model.getMargin({ ...DEFAULT_CAMPAIGN, margin: 50, ad_unit_types: { overlay_thumbnail: { payback_cpm: 5, margin: 30 }, }, }); expect(result).to.eq(50); }); it("should fall back to default if ad unit config is empty", () => { const result = model.getMargin( { ...DEFAULT_CAMPAIGN, margin: 50, ad_unit_types: {}, }, "interstitial" ); expect(result).to.eq(30); }); it("should fall back to default if margin is undefined", () => { const result = model.getMargin({ ...DEFAULT_CAMPAIGN, }); expect(result).to.eq(30); }); }); it("hasPrebid should find prebid", () => { const campaign = Object.assign({}, DEFAULT_CAMPAIGN); campaign.prebid = [{ type: "brand-safety", provider: "dv" }]; expect(CampaignModel.hasPrebid(campaign, "brand-safety", "dv")).to.be.true; expect(CampaignModel.hasPrebid(campaign, "brand-safety", "ias")).to.be.false; }); });