Skip to content

Commit

Permalink
order test
Browse files Browse the repository at this point in the history
  • Loading branch information
UO270157 committed May 3, 2022
1 parent 72e59c0 commit a707b11
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
29 changes: 29 additions & 0 deletions webapp/src/components/Order/Order.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { render } from "@testing-library/react";
import { BrowserRouter } from 'react-router-dom';
import Order from "./Order";
import * as api from '../Services/OrderService'
import { Item, OrderType, Producto, User } from "../../shared/sharedtypes";
import { ReactSession } from 'react-client-session';

jest.mock('../Services/OrderService');

test('check the payment form render properly', async () => {
const user: User = {
username: "user",
password: "user",
token: ""
};
const product: Producto = { category: 'cellular', name: 'IPhone', description: 'a', urlImage: 'aaaaa', basePrice: 200, units: 2, onSale: true, IVA: 0.21 };
const listProd: Item[] = [{ producto: product, num: 3 }];
const order: OrderType = {
_id: '', user: user,
products: listProd,
totalPrice: 0
};
ReactSession.set("user", user);
jest.spyOn(api, 'getOrder').mockImplementation((id: String, token: string): Promise<OrderType> => Promise.resolve(order));

const { getByText, container } = render(<BrowserRouter><Order /></BrowserRouter>);
expect(getByText("Order")).toBeInTheDocument();

});
19 changes: 10 additions & 9 deletions webapp/src/components/Order/Order.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ const Order = () => {
const params = useParams();

const loadOrder = async () => {
let user = ReactSession.get("user");
let res = await orderService.getOrder(params._id as string,user.token);
if(res.data.order){
setOrder(res.data.order)
let user = ReactSession.get("user");
let ord = await orderService.getOrder(params._id as string,user.token);
setOrder(ord);
let prods:Product[] = [];
for(var p in res.data.order.products){
var product = {name:p,num:res.data.order.products[p]};
prods.push(product);
if(order != undefined){
for(var p in order!.products){
var product = {name:p,num:order!.products[p].num};
prods.push(product);
}
setProductos(prods);
}
setProductos(prods);
}
}

const download = () => {
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/components/Order/Orders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const Orders = () => {
const [orders, setOrders] = useState<OrderType[]>([]);

const loadOrders = async () => {
let res = await orderService.getOrdersOf(ReactSession.get("user"));
setOrders(res.data.orders);
let res = await orderService.getOrdersOf(ReactSession.get("user"));
setOrders(res.data.orders);
}

useEffect(() => {
loadOrders();
}, [])


return (
<div>
<h1 className='title'>My orders</h1>
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/components/Services/OrderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const getOrdersOf = async (u: User) => {
};

export const getOrder = async (id: string, token:string) => {
return await axios.get(`${API}/order/`+id, {headers:{'Authorization': token}});
let res = await axios.get(`${API}/order/`+id, {headers:{'Authorization': token}});
return res.data.order;
};

export const getShippingDetails = async (user:string, address: AddressType, token:string) => {
Expand Down

0 comments on commit a707b11

Please sign in to comment.