Skip to content

Commit

Permalink
fix(store-mobx/cart): typecasting price and MSRP in cart for edge cas…
Browse files Browse the repository at this point in the history
…es where values are strings
  • Loading branch information
korgon committed May 20, 2024
1 parent ad413e1 commit 1627d90
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/snap-store-mobx/src/Cart/CartStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ describe('CartStore store', () => {
expect(cartStore.msrp).toStrictEqual(result.mappings.core?.msrp ? result.mappings.core?.msrp * 3 : 0);
});

it('will ensure price and MSRP are numbers', () => {
const cartStore = new CartStore();
const result = results[0] as Product;
result.quantity = 1;

// @ts-ignore - sometimes the values in variant data can be strings
result.mask.set({ mappings: { core: { msrp: '0.00' } } });

cartStore.addItems([result]);

expect(cartStore.items).toHaveLength(1);
expect(cartStore.count).toStrictEqual(1);
expect(cartStore.price).toStrictEqual(result.mappings.core?.price);
expect(cartStore.msrp).toStrictEqual(result.mappings.core?.price);
});

it('can use the eventmanager events', async () => {
const cartStore = new CartStore();

Expand Down
4 changes: 2 additions & 2 deletions packages/snap-store-mobx/src/Cart/CartStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export class CartStore {
get price(): number {
let price = 0;
this.items.forEach((item) => {
price += (item.display.mappings.core?.price || 0) * item.quantity;
price += +(item.display.mappings.core?.price || 0) * item.quantity;
});
return price;
}

get msrp(): number {
let price = 0;
this.items.forEach((item) => {
price += (item.display.mappings.core?.msrp || item.display.mappings.core?.price || 0) * item.quantity;
price += (+(item.display.mappings.core?.msrp || 0) || +(item.display.mappings.core?.price || 0) || 0) * item.quantity;
});
return price;
}
Expand Down

0 comments on commit 1627d90

Please sign in to comment.