Skip to content

Commit

Permalink
test: improve and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brankoconjic committed Jun 14, 2024
1 parent 445a14e commit 9ca1f75
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 deletions.
7 changes: 5 additions & 2 deletions test/checkouts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,9 @@ describe("Create a checkout", () => {
});

it("Failed to create a new checkout with 404 status code", async () => {
const { error, data, statusCode } = await createCheckout(storeId, "123");
const { error, statusCode } = await createCheckout(storeId, "123");
expect(error).toBeDefined();
expect(statusCode).toEqual(404);
expect(data).toBeNull();
});
});

Expand Down Expand Up @@ -590,6 +589,7 @@ describe("Retrieve a checkout", () => {
quantity,
dark,
subscription_preview,
skip_trial,
button_color,
} = checkout_options;
const checkoutOptionItems = [
Expand All @@ -599,6 +599,7 @@ describe("Retrieve a checkout", () => {
desc,
discount,
quantity,
skip_trial,
dark,
subscription_preview,
button_color,
Expand Down Expand Up @@ -775,6 +776,7 @@ describe("Retrieve a checkout", () => {
desc,
discount,
quantity,
skip_trial,
dark,
subscription_preview,
button_color,
Expand All @@ -785,6 +787,7 @@ describe("Retrieve a checkout", () => {
logo,
desc,
discount,
skip_trial,
quantity,
dark,
subscription_preview,
Expand Down
7 changes: 1 addition & 6 deletions test/customers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,19 +379,14 @@ describe("Update a customer", () => {
});

it("An error and a statusCode of 422 should be returned", async () => {
const {
error,
data: _data,
statusCode,
} = await updateCustomer(newCustomerId, {
const { error, statusCode } = await updateCustomer(newCustomerId, {
status: "unsubscribed" as any,
});

expect(error).toBeDefined();
expect(error?.message).toMatch("Unprocessable Entity");
expect(error?.cause).toBeArray();
expect(statusCode).toEqual(422);
expect(_data).toBeNull();
});
});

Expand Down
6 changes: 0 additions & 6 deletions test/discounts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
getDiscount,
lemonSqueezySetup,
listDiscounts,
listVariants,
} from "../../src";
import type { NewDiscount } from "../../src/discounts/types";
import { API_BASE_URL, generateDiscount } from "../../src/internal";
Expand All @@ -14,12 +13,9 @@ const DATA_TYPE = "discounts";
const PATH = "/v1/discounts/";
const storeId = import.meta.env.LEMON_SQUEEZY_STORE_ID!;
let discountId: number | string;
let variantIds: Array<number | string>;

beforeAll(async () => {
lemonSqueezySetup({ apiKey: import.meta.env.LEMON_SQUEEZY_API_KEY });
const { data } = await listVariants();
variantIds = data?.data.map((item) => item.id) ?? [];
});

describe("Create a discount", () => {
Expand All @@ -29,7 +25,6 @@ describe("Create a discount", () => {
amount: 50,
amountType: "percent",
storeId,
variantIds,
};
const {
statusCode,
Expand Down Expand Up @@ -115,7 +110,6 @@ describe("Create a discount", () => {
amount: 20,
amountType: "percent",
storeId,
variantIds,
};
const {
statusCode,
Expand Down
6 changes: 2 additions & 4 deletions test/internal/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ describe("$fetch test", () => {

it("Should return an error that the Lemon Squeezy API key was not provided", async () => {
lemonSqueezySetup({ apiKey: "" });
const { error, data, statusCode } = await $fetch({ path: "/v1/user/me" });
expect(data).toBeNull();
const { error, statusCode } = await $fetch({ path: "/v1/user/me" });
expect(statusCode).toBeNull();
expect(error?.message).toMatch("Lemon Squeezy API");
});
Expand All @@ -40,8 +39,7 @@ describe("$fetch test", () => {

it("Should return a Lemon Squeezy API error", async () => {
lemonSqueezySetup({ apiKey: "0123456789" });
const { error, data, statusCode } = await $fetch({ path: "/v1/user/me" });
expect(data).toBeNull();
const { error, statusCode } = await $fetch({ path: "/v1/user/me" });
expect(statusCode).toEqual(404);
expect(error).toBeDefined();
expect(error?.cause).toBeArray();
Expand Down
23 changes: 10 additions & 13 deletions test/subscriptions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ describe("List all subscriptions", () => {
expect(data).toBeArray();
expect(data[0]).toBeDefined();

const { id, attributes } = data[0];
// Get active subscription
const activeSubscription = data.find((d) => {
d.attributes.status === "active";
});

const { id, attributes } = activeSubscription ?? data[0];
const {
store_id,
order_id,
Expand All @@ -46,6 +51,7 @@ describe("List all subscriptions", () => {
variant_id,
user_email,
} = attributes;

subscriptionId = id;
storeId = store_id;
orderId = order_id;
Expand Down Expand Up @@ -565,9 +571,7 @@ describe("Update a subscription", () => {
statusCode,
error,
data: _data,
} = await updateSubscription(subscriptionId, {
cancelled: true,
});
} = await cancelSubscription(subscriptionId);
expect(statusCode).toEqual(200);
expect(error).toBeNull();
expect(_data).toBeDefined();
Expand Down Expand Up @@ -613,16 +617,9 @@ describe("Update a subscription", () => {
it("The subscription should be changed to trial_ends_at", async () => {
function formatISO8601(isoString: string) {
const date = new Date(isoString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return date.toISOString().replace("T", " ").substring(0, 19);
}
const trialEndsAt = new Date("2024-04-25").toISOString();
const trialEndsAt = new Date("3/4/2025").toISOString();
const {
statusCode,
error,
Expand Down

0 comments on commit 9ca1f75

Please sign in to comment.