Skip to content

Commit

Permalink
Updated tests with the new appendDate and combinedItems fields
Browse files Browse the repository at this point in the history
Still hav to test the new combinedOffers virtual field and integrate it along with the other dependencies
  • Loading branch information
zhamdi committed Dec 12, 2023
1 parent 6d59298 commit 6b5159a
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 129 deletions.
106 changes: 61 additions & 45 deletions src/impl/mongoose/model/Offer.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,72 @@
import { Types } from "mongoose";

type ObjectId = Types.ObjectId;
import { IOffer } from "@user-credits/core";
import { Document, Schema } from "mongoose";
import { Document, Schema, Types } from "mongoose";

export type IMongooseOffer = IOffer<ObjectId> & Document;

const offerSchema = new Schema<IMongooseOffer>({
appendDate: Boolean,
combinedItems: [
{
// ICombinedOffer
offerGroup: String,
offerId: Schema.ObjectId,
quantity: Number,
},
],
currency: String,
customCycle: Number,
cycle: {
enum: [
"once",
"daily",
"weekly",
"bi-weekly",
"monthly",
"trimester",
"semester",
"yearly",
"custom",
const offerSchema = new Schema<IMongooseOffer>(
{
appendDate: Boolean,
combinedItems: [
{
// ICombinedOffer
offerGroup: String,
offerId: {
ref: "offer",
required: true,
type: Schema.Types.ObjectId,
},
quantity: Number,
},
],
type: String,
currency: String,
customCycle: Number,
cycle: {
enum: [
"once",
"daily",
"weekly",
"bi-weekly",
"monthly",
"trimester",
"semester",
"yearly",
"custom",
],
type: String,
},
hasDependentOffers: { type: Boolean },
kind: {
enum: ["subscription", "tokens", "expertise"],
required: true,
type: String,
},
name: { required: true, type: String },
/**
* Detailed documentation in interface @IOffer.offerGroup
*/
offerGroup: { required: true, type: String },
overridingKey: String,
popular: { type: Number },
price: { required: true, type: Number },
quantityLimit: Number,
tags: { default: [], type: [String] },
tokenCount: { required: true, type: Number },
unlockedBy: { default: [], type: [String] },
weight: { default: 0, type: Number },
},
hasDependentOffers: { type: Boolean },
kind: {
enum: ["subscription", "tokens", "expertise"],
required: true,
type: String,
{
toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals
},
name: { required: true, type: String },
/**
* Detailed documentation in interface @IOffer.offerGroup
*/
offerGroup: { required: true, type: String },
overridingKey: String,
popular: { type: Number },
price: { required: true, type: Number },
quantityLimit: Number,
tags: { default: [], type: [String] },
tokenCount: { required: true, type: Number },
unlockedBy: { default: [], type: [String] },
weight: { default: 0, type: Number },
);

offerSchema.virtual("combinedOffers", {
foreignField: "_id",
justOne: false,
localField: "combinedItems.offerId",
// Reference the 'Offer' model
ref: "Offer",
});

// Add instance methods to your Mongoose schema
Expand Down
29 changes: 14 additions & 15 deletions test/db/dao/IOfferDao.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("OfferDao specific methods", () => {
const offers = await offerDao.loadOffers({});
// Write your Jest assertions to check if the offers were loaded correctly
expect(Array.isArray(offers)).toBe(true);
expect(offers.length).toBe(18);
expect(offers.length).toBe(19);
});
it("should load all offers if passed an empty tag list with allTags on", async () => {
const offers = await offerDao.loadOffers({
Expand All @@ -70,7 +70,7 @@ describe("OfferDao specific methods", () => {
});
// Write your Jest assertions to check if the offers were loaded correctly
expect(Array.isArray(offers)).toBe(true);
expect(offers.length).toBe(18);
expect(offers.length).toBe(19);
});
});

Expand Down Expand Up @@ -116,7 +116,7 @@ describe("OfferDao specific methods", () => {
);
// Write your Jest assertions to check if the sub-offers were loaded correctly
expect(Array.isArray(subOffers)).toBe(true);
expect(subOffers.length).toBe(5);
expect(subOffers.length).toBe(2);
}, 10000);
});

Expand All @@ -126,7 +126,7 @@ describe("OfferDao specific methods", () => {
const offers = await offerDao.loadOffers();
// Write your Jest assertions to check if the offers were loaded correctly
expect(Array.isArray(offers)).toBe(true);
expect(offers.length).toBe(18);
expect(offers.length).toBe(19);
});
it("should load 13 subscription offers as in the article", async () => {
const params = {
Expand Down Expand Up @@ -176,15 +176,12 @@ describe("OfferDao specific methods", () => {
"EbScaleUp",
"EbScaleUp",
"EbStartup",
"1-VIP-event",
"3-VIP-events",
"EbEnterprise",
"1-article-month",
"2-articles-month",
"7-VIP-events",
]),
);
expect(offers.length).toBe(11);
expect(offers.length).toBe(8);
});
it("should load no offers unlocked by (the standard) Startup offer group", async () => {
const params = {
Expand All @@ -206,24 +203,26 @@ describe("OfferDao specific methods", () => {
expect.arrayContaining([
"1-article-month",
"2-articles-month",
"1-VIP-event",
"3-VIP-events",
"7-VIP-events",
]),
);
expect(offers.length).toBe(5);
expect(offers.length).toBe(2);
});
it("should load 3 offers under the 'VipEventTalk' offerGroup", async () => {
const params = {
offerGroup: OFFER_GROUP.VipEventTalk,
offerGroup: OFFER_GROUP.AiTokens,
};
const offers = await offerDao.loadOffers(params);
// Write your Jest assertions to check if the offers were loaded correctly
expect(Array.isArray(offers)).toBe(true);
expect(offerNames(offers)).toEqual(
expect.arrayContaining(["1-VIP-event", "3-VIP-events", "7-VIP-events"]),
expect.arrayContaining([
"_20tokens",
"_100tokens",
"_300tokens",
"_700tokens",
]),
);
expect(offers.length).toBe(3);
expect(offers.length).toBe(4);
});
});
});

0 comments on commit 6b5159a

Please sign in to comment.