Skip to content

Commit

Permalink
Create validateConditionalVariants.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rodleviton committed Nov 17, 2023
1 parent 6357536 commit 1aa3a58
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions packages/lib/src/utils/__tests__/validateConditionalVariants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { validateConditionalVariants } from "../validateConditionalVariants";

describe("validateConditionalVariants", () => {
it("should return `true` if there are variant conditions are undefined", () => {
const variants = undefined;
const defaults = undefined;

const condition = {
// Variant conditons are missing
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});

it("should return `true` if there are no variant conditions", () => {
const variants = undefined;
const defaults = undefined;

const condition = {
variants: {}, // This is all that matters
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});

it("should return `true` if variant conditions are met", () => {
const variants = { intent: "default" };
const defaults = undefined;

const condition = {
variants: { intent: "default" }, // Variant conditions match above as a string
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});

it("should return `true` if variant conditions are met with default variant", () => {
const variants = undefined;
const defaults = { intent: "default" };

const condition = {
variants: { intent: "default" }, // Variant conditions match above as a string
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});

it("should return `true` if variant conditions are met with default variants as an array", () => {
const variants = undefined;
const defaults = { intent: "outline" };

const condition = {
variants: { intent: ["default", "outline"] }, // Variant conditions match defaults above
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});

it("should return `true` if variant conditions are met within an array", () => {
const variants = { intent: "outline" };
const defaults = undefined;

const condition = {
variants: { intent: ["default", "outline"] }, // Variant conditions match above
classes: {
root: "!bg-green-500",
},
};

const result = validateConditionalVariants({
condition,
variants,
defaults,
});

expect(result).toEqual(true);
});
});

0 comments on commit 1aa3a58

Please sign in to comment.