Skip to content

Commit 3394801

Browse files
authored
Merge pull request #11 from getyourguide/TP-tax-dependency
adds getTax function
2 parents 55e34d5 + ad4630a commit 3394801

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { calculateCartTotal } from "../price-calculator";
2+
// import { getTax } from "../utils/tax";
23

34
describe("calculateCartTotal()", () => {
45
it("calculates the total of a number of cart items", () => {
5-
const total = calculateCartTotal([], 0.07);
6+
const total = calculateCartTotal([]);
67
expect(total).toEqual({ status: "success", total: "0.00" });
78
});
8-
});
9+
});
10+

src/utils/price-calculator.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getTax } from "@/utils/tax";
12
import type { CartItem } from "@/types/cart-item";
23

34
/*
@@ -16,8 +17,7 @@ import type { CartItem } from "@/types/cart-item";
1617
name: "Product 4",
1718
},
1819
];
19-
const tax = 0.07;
20-
const total = calculateCartTotal(items, tax);
20+
const total = calculateCartTotal(items);
2121
cosole.log(total); // { status: "success", total: "38.49" }
2222
2323
@@ -41,18 +41,19 @@ import type { CartItem } from "@/types/cart-item";
4141
name: "Product 4",
4242
},
4343
];
44-
const tax = 0.07;
45-
const total = calculateCartTotal(items, tax);
44+
const total = calculateCartTotal(items);
4645
cosole.log(total); // { status: "error", message: "Cart has duplicate items" }
4746
*/
4847

4948
type CartTotalResult =
5049
| { status: "success"; total: string }
5150
| { status: "error"; message: string };
5251

53-
export function calculateCartTotal(cartItems: CartItem[], tax?: number): CartTotalResult {
52+
export function calculateCartTotal(cartItems: CartItem[]): CartTotalResult {
53+
const tax = getTax();
5454
return {
5555
status: "success",
5656
total: "0.00",
5757
};
5858
}
59+

src/utils/tax.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Get the tax rate
3+
* In a real application, this would be fetched from an API
4+
* as it changes frequently
5+
* */
6+
export const getTax = () => 0.07;
7+

0 commit comments

Comments
 (0)