Skip to content

Commit

Permalink
chore: allow passing experiences config in .formatTab() (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasstark authored Jan 13, 2025
1 parent 61f96c0 commit f5741c9
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 10 deletions.
145 changes: 141 additions & 4 deletions Supertab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const setup = ({
);
}

server.withClientConfig({
const clientConfig = {
contentKeys: [
{
productId: "product.test-product-id",
Expand Down Expand Up @@ -154,7 +154,9 @@ const setup = ({
},
] as Currency[],
suggestedCurrency: clientConfigProps?.suggestedCurrency ?? "USD",
} as ClientConfig);
} as ClientConfig;

server.withClientConfig(clientConfig);

const experience = {
id: "test-experience-1",
Expand Down Expand Up @@ -200,7 +202,7 @@ const setup = ({
],
};

server.withClientExperiencesConfig({
const clientExperiencesConfig = {
redirectUri: "",
siteName: "",
siteLogoUrl: "",
Expand Down Expand Up @@ -274,12 +276,16 @@ const setup = ({
},
] as Currency[],
suggestedCurrency: clientConfigProps?.suggestedCurrency ?? "USD",
} as ClientExperiencesConfig);
} as ClientExperiencesConfig;

server.withClientExperiencesConfig(clientExperiencesConfig);

return {
client,
windowOpen,
checkoutWindow,
clientConfig,
clientExperiencesConfig,
emitter,
};
};
Expand Down Expand Up @@ -1625,4 +1631,135 @@ describe("Supertab", () => {
});
});
});

describe(".formatTab", () => {
test("formats tab with correct currency and amounts", () => {
const { client, clientExperiencesConfig } = setup();

const tab = createTabData("USD");

const formattedTab = client.formatTab({
tab,
config: clientExperiencesConfig,
});

expect(formattedTab).toEqual({
id: "test-tab-id",
status: "open",
total: {
amount: 50,
text: "$0.50",
},
limit: {
amount: 500,
text: "$5",
},
currency: {
isoCode: "USD",
baseUnit: 100,
},
paymentModel: "pay_later",
purchases: [
{
id: "purchase.4df706b5-297a-49c5-a4cd-2a10eca12ff9",
purchaseDate: new Date("2023-11-03T15:34:44.852Z"),
summary: "test-summary",
price: {
amount: 50,
text: "$0.50",
currency: {
isoCode: "USD",
baseUnit: 100,
},
},
validTo: null,
recurringDetails: null,
},
],
});
});

test("uses client config when provided", () => {
const { client, clientConfig } = setup();

const tab = createTabData("USD");

const formattedTab = client.formatTab({
tab,
clientConfig,
});

expect(formattedTab).toEqual({
id: "test-tab-id",
status: "open",
total: {
amount: 50,
text: "$0.50",
},
limit: {
amount: 500,
text: "$5",
},
currency: {
isoCode: "USD",
baseUnit: 100,
},
paymentModel: "pay_later",
purchases: [
{
id: "purchase.4df706b5-297a-49c5-a4cd-2a10eca12ff9",
purchaseDate: new Date("2023-11-03T15:34:44.852Z"),
summary: "test-summary",
price: {
amount: 50,
text: "$0.50",
currency: {
isoCode: "USD",
baseUnit: 100,
},
},
validTo: null,
recurringDetails: null,
},
],
});
});

test("throws error when config is missing", () => {
const { client } = setup();

const tab = createTabData("USD");

// eslint-disable-next-line
expect(() => client.formatTab({ tab, config: null as any })).toThrow(
"Missing config object",
);
});

test("throws error when currency is not found", () => {
const { client, clientExperiencesConfig } = setup();

const tab = {
...createTabData("USD"),
currency: "INVALID",
};

expect(() =>
client.formatTab({ tab, config: clientExperiencesConfig }),
).toThrow("Currency details not found");
});

test("throws error on currency mismatch in purchases", () => {
const { client, clientExperiencesConfig } = setup();

const tab = {
...createTabData("EUR"),
currency: "USD",
};

expect(() =>
client.formatTab({ tab, config: clientExperiencesConfig }),
).toThrow("Currency mismatch");
});
});
});
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class Supertab {
const filterStatuses: TabStatus[] = [TabStatus.Open, TabStatus.Full];

if (filterStatuses.includes(tab?.status)) {
return this.formatTab({ tab, clientConfig });
return this.formatTab({ tab, config: clientConfig });
}
return null;
}
Expand Down Expand Up @@ -318,7 +318,7 @@ export class Supertab {

return {
status: "success",
tab: this.formatTab({ tab, clientConfig }),
tab: this.formatTab({ tab, config: clientConfig }),
};
},
});
Expand Down Expand Up @@ -358,7 +358,7 @@ export class Supertab {
return {
itemAdded: !!detail?.itemAdded,
purchaseOutcome: detail?.purchaseOutcome || null,
tab: this.formatTab({ tab, clientConfig }),
tab: this.formatTab({ tab, config: clientConfig }),
};
} catch (e) {
if (e instanceof ResponseError && e.response.status === 402) {
Expand All @@ -368,7 +368,7 @@ export class Supertab {
return {
itemAdded: !!detail?.itemAdded,
purchaseOutcome: detail?.purchaseOutcome || null,
tab: this.formatTab({ tab, clientConfig }),
tab: this.formatTab({ tab, config: clientConfig }),
};
}

Expand Down Expand Up @@ -407,12 +407,20 @@ export class Supertab {

formatTab({
tab,
config,
clientConfig,
}: {
tab: TabResponse;
clientConfig: ClientConfig;
config?: ClientConfig | ClientExperiencesConfig;
clientConfig?: ClientConfig; // keeping for backwards compatibility
}) {
const currencyObject = clientConfig.currencies.find(
const configObject = clientConfig || config;

if (!configObject) {
throw new Error("Missing config object");
}

const currencyObject = configObject.currencies.find(
(currency) => currency.isoCode === tab.currency,
);
if (!currencyObject) {
Expand Down

0 comments on commit f5741c9

Please sign in to comment.