Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix the unit test

Kiran K823938886155
diff --git a/apps/web/tests/partners/evaluate-reward-rules.test.ts b/apps/web/tests/rewards/reward-conditions.test.ts
similarity index 68%
rename from apps/web/tests/partners/evaluate-reward-rules.test.ts
rename to apps/web/tests/rewards/reward-conditions.test.ts
index 12d2919621..6dce205e87 100644
--- a/apps/web/tests/partners/evaluate-reward-rules.test.ts
+++ b/apps/web/tests/rewards/reward-conditions.test.ts
@@ -1,27 +1,30 @@
-import { evaluateRewardConditions, RewardContext } from "@/lib/partners/evaluate-reward-conditions";
+import { evaluateRewardConditions } from "@/lib/partners/evaluate-reward-conditions";
+import { RewardContext } from "@/lib/types";
import { describe, expect, test } from "vitest";
describe("evaluateRewardConditions", () => {
describe("AND operator", () => {
test("should return matching condition when all conditions are met", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -41,24 +44,26 @@ describe("evaluateRewardConditions", () => {
});
test("should return null when one condition is not met", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -78,24 +83,26 @@ describe("evaluateRewardConditions", () => {
});
test("should return null when all conditions are not met", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -117,24 +124,26 @@ describe("evaluateRewardConditions", () => {
describe("OR operator", () => {
test("should return matching condition when one condition is met", () => {
- const conditions = [{
- operator: "OR" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "OR" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -154,24 +163,26 @@ describe("evaluateRewardConditions", () => {
});
test("should return matching condition when all conditions are met", () => {
- const conditions = [{
- operator: "OR" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "OR" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -191,24 +202,26 @@ describe("evaluateRewardConditions", () => {
});
test("should return null when no conditions are met", () => {
- const conditions = [{
- operator: "OR" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "equals_to" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "OR" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "equals_to" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -329,18 +342,20 @@ describe("evaluateRewardConditions", () => {
describe("condition operators", () => {
describe("equals_to", () => {
test("should match exact string values", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -357,18 +372,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match different string values", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -387,18 +404,20 @@ describe("evaluateRewardConditions", () => {
describe("not_equals", () => {
test("should match when values are different", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "not_equals" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "not_equals" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -415,18 +434,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match when values are the same", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "not_equals" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "not_equals" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -445,18 +466,20 @@ describe("evaluateRewardConditions", () => {
describe("in", () => {
test("should match when value is in array", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "in" as const,
- value: ["US", "CA", "UK"],
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "in" as const,
+ value: ["US", "CA", "UK"],
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -473,18 +496,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match when value is not in array", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "in" as const,
- value: ["US", "CA", "UK"],
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "in" as const,
+ value: ["US", "CA", "UK"],
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -503,18 +528,20 @@ describe("evaluateRewardConditions", () => {
describe("not_in", () => {
test("should match when value is not in array", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "not_in" as const,
- value: ["US", "CA", "UK"],
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "not_in" as const,
+ value: ["US", "CA", "UK"],
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -531,18 +558,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match when value is in array", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "not_in" as const,
- value: ["US", "CA", "UK"],
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "not_in" as const,
+ value: ["US", "CA", "UK"],
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {
@@ -561,18 +590,20 @@ describe("evaluateRewardConditions", () => {
describe("starts_with", () => {
test("should match when string starts with value", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "starts_with" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "starts_with" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
sale: {
@@ -589,18 +620,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match when string does not start with value", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "starts_with" as const,
- value: "premium",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "starts_with" as const,
+ value: "premium",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
sale: {
@@ -619,18 +652,20 @@ describe("evaluateRewardConditions", () => {
describe("ends_with", () => {
test("should match when string ends with value", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "ends_with" as const,
- value: "plus",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "ends_with" as const,
+ value: "plus",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
sale: {
@@ -647,18 +682,20 @@ describe("evaluateRewardConditions", () => {
});
test("should not match when string does not end with value", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "sale" as const,
- attribute: "productId" as const,
- operator: "ends_with" as const,
- value: "plus",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "sale" as const,
+ attribute: "productId" as const,
+ operator: "ends_with" as const,
+ value: "plus",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
sale: {
@@ -695,18 +732,20 @@ describe("evaluateRewardConditions", () => {
});
test("should return null when context is null", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const result = evaluateRewardConditions({
conditions,
@@ -717,18 +756,20 @@ describe("evaluateRewardConditions", () => {
});
test("should return null when field value is undefined", () => {
- const conditions = [{
- operator: "AND" as const,
- amount: 5000,
- conditions: [
- {
- entity: "customer" as const,
- attribute: "country" as const,
- operator: "equals_to" as const,
- value: "US",
- },
- ],
- }];
+ const conditions = [
+ {
+ operator: "AND" as const,
+ amount: 5000,
+ conditions: [
+ {
+ entity: "customer" as const,
+ attribute: "country" as const,
+ operator: "equals_to" as const,
+ value: "US",
+ },
+ ],
+ },
+ ];
const context: RewardContext = {
customer: {