Skip to content

Commit

Permalink
Merge pull request #162 from Arquisoft/enol_tests
Browse files Browse the repository at this point in the history
Merge con nuevos test y validacion a la hora de pagar
  • Loading branch information
nataliariego authored May 4, 2022
2 parents 8002d2c + 8857636 commit f7cb558
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 42 deletions.
118 changes: 76 additions & 42 deletions webapp/src/components/PaymentForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { ReactHTMLElement, useEffect, useState } from 'react';
import { ProductoCarrito } from "../shared/shareddtypes";
import { useNavigate } from "react-router-dom";
import { addPedido, addProductoPedido, getCarrito, getPedidosByEmail, getShipping, vaciarCarrito } from "../api/api";
Expand All @@ -15,11 +15,11 @@ async function retrievePODAddress(webID: string): Promise<string> {
let profile = getThing(myDataSet, webID)
let urlAddress = getUrl(profile as Thing, VCARD.hasAddress) as string
let addressProfile = await getThing(myDataSet, urlAddress)
let ret= getStringNoLocale(addressProfile as Thing, VCARD.country_name) as string+" "+
getStringNoLocale(addressProfile as Thing, VCARD.region) as string+" "+
getStringNoLocale(addressProfile as Thing, VCARD.locality) as string+" "+
getStringNoLocale(addressProfile as Thing, VCARD.postal_code) as string+" "+
getStringNoLocale(addressProfile as Thing, VCARD.street_address) as string;
let ret = getStringNoLocale(addressProfile as Thing, VCARD.country_name) as string + " " +
getStringNoLocale(addressProfile as Thing, VCARD.region) as string + " " +
getStringNoLocale(addressProfile as Thing, VCARD.locality) as string + " " +
getStringNoLocale(addressProfile as Thing, VCARD.postal_code) as string + " " +
getStringNoLocale(addressProfile as Thing, VCARD.street_address) as string;
return ret
}

Expand All @@ -41,15 +41,15 @@ async function retirevePODName(webID: string): Promise<string> {

function PaymentForm() {

const [products,setProducts] = useState<ProductoCarrito[]>([]);
const [products, setProducts] = useState<ProductoCarrito[]>([]);

const refreshProducts = () => {
setProducts(getCarrito());
}

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

const { session } = useSession();
const { webId } = session.info;
Expand All @@ -58,29 +58,36 @@ function PaymentForm() {

const [address, setAddress] = React.useState("");

const getPODAddress = async () => {setAddress(await retrievePODAddress(webId!))
}
;
const getPODAddress = async () => {
setAddress(await retrievePODAddress(webId!))
}
;

const [email, setEmail] = React.useState("");

const getPODEmail = async () => {setEmail(await retirevePODEmail(webId!))
}
;
const [cardNumber, setCardNumber] = React.useState("");
const [cardCode, setCardCode] = React.useState("");
const [cardExpDate, setCardExpDate] = React.useState("");

const getPODEmail = async () => {
setEmail(await retirevePODEmail(webId!))
}
;

const [name, setName] = React.useState("");

const getPODName = async () => {setName(await retirevePODName(webId!))
}
;
const getPODName = async () => {
setName(await retirevePODName(webId!))
}
;

const [gastoEnvio, setGastoEnvio] = React.useState<number>();

const getGastoEnvio = async () => {
console.log('ey');
setGastoEnvio(await getShipping(address));
}
;
console.log('ey');
setGastoEnvio(await getShipping(address));
}
;


useEffect(() => {
Expand All @@ -90,26 +97,53 @@ function PaymentForm() {
console.log(email);
console.log(address);
getGastoEnvio();
console.log("gastos "+gastoEnvio);
console.log("gastos " + gastoEnvio);

})



const pagar = async (emailu: string, gasto: number) => {
var precio = 0;
products.map((product: ProductoCarrito) =>{
precio += product.amount * product.price;
})
addPedido(emailu, precio + gasto);
console.log("ey:" + emailu)
var pedidos = getPedidosByEmail(emailu);
var pedido = (await pedidos).pop();
products.map((product: ProductoCarrito) => {
addProductoPedido(product.amount, product, pedido!);
});
vaciarCarrito();
navigate("/pedidos")
if (cardCode.length != 3) {
alert("El código debe tener 3 digitos");
}
else if (!cardCode.match("[0-9]*")) {
alert("Introduce dígitos");
}
else if (!cardExpDate.match("[0-9]{2}/[0-9]{2}" )) {
alert("Introduce una fecha valida");
}
else if(cardNumber.length != 6) {
alert("La longitud de la tarjeta debe ser 6 digitos");

}
else {
var precio = 0;
products.map((product: ProductoCarrito) => {
precio += product.amount * product.price;
})
addPedido(emailu, precio + gasto);
console.log("ey:" + emailu)
var pedidos = getPedidosByEmail(emailu);
var pedido = (await pedidos).pop();
products.map((product: ProductoCarrito) => {
addProductoPedido(product.amount, product, pedido!);
});
navigate("/pedidos")
}

}

const validateCVC = (element: React.FocusEvent<HTMLInputElement>) => {
if (element.target.value.length < 3 || element.target.value.length > 3) {
alert("El código debe tener 3 digitos");
element.target.value = "";
}
else if (!element.target.value.match("[0-9]*")) {
alert("Introduce un dígito");
element.target.value = "";
}

}

console.log(webId);
Expand All @@ -134,12 +168,12 @@ function PaymentForm() {

{products.map((product: ProductoCarrito) => {
console.log(product.name)
return(
return (

<tr>
<td>

<img src = {product?.url.toString()} alt={product?.name.toString()} width="100" height="100" />
<img src={product?.url.toString()} alt={product?.name.toString()} width="100" height="100" />
</td>
<td>{product.name}</td>
<td>{product.description}</td>
Expand Down Expand Up @@ -186,7 +220,7 @@ function PaymentForm() {
<div>
<h6>Número de tarjeta</h6>
<div>
<div> <label ><input type="text" name="cardNumber" className="form-control" placeholder=" " /></label> </div>
<div> <label ><input type="text" name="cardNumber" className="form-control" placeholder=" " onChange={(e) => setCardNumber(e.target.value)} /></label> </div>
</div>
</div>

Expand All @@ -195,7 +229,7 @@ function PaymentForm() {
<div>
<h6>Fecha en formato MM/YY</h6>
<div>
<div> <label ><input type="text" name="expDate" className="form-control" placeholder=" " /></label> </div>
<div> <label ><input type="text" name="expDate" className="form-control" placeholder=" " onChange={(e) => setCardExpDate(e.target.value)} /></label> </div>
</div>
</div>

Expand All @@ -204,14 +238,14 @@ function PaymentForm() {
<div>
<h6>Código de seguridad</h6>
<div>
<div> <label ><input type="text" name="cvc" className="form-control" placeholder=" " /></label> </div>
<div> <label ><input type="text" name="cvc" className="form-control" placeholder=" " onChange={(e) => setCardCode(e.target.value)} /></label> </div>
</div>
</div>
</div>
</div>

</div>
<br/>
<br />
<Button onClick={() => pagar(email, gastoEnvio!)}>Pagar</Button>
</div>
</div>
Expand Down
10 changes: 10 additions & 0 deletions webapp/src/components/login/Login.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { getByDisplayValue, render } from "@testing-library/react";
import Login from "./Login";
import { BrowserRouter } from 'react-router-dom';

test('check that everything is rendering propertly', async () => {

const { getByText } = render(<BrowserRouter><Login/></BrowserRouter>);
expect(getByText('Login')).toBeInTheDocument();
});
30 changes: 30 additions & 0 deletions webapp/src/components/pedidos/PedidosList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import { render, screen } from "@testing-library/react";
import PedidosList from "./PedidosList";
import * as api from '../../api/api';
import { Pedido, Product } from "../../shared/shareddtypes";
import { act } from 'react-dom/test-utils';
import { BrowserRouter } from 'react-router-dom';
import * as solid from "@inrupt/solid-ui-react";

test('check that everything is rendering propertly', async () => {

function miGetPedidos() {
return Promise.resolve([{
id: "1",
estado: "En camino",
nombre_dest: "Enol",
email: "Email",
precio_final: 20
} as Pedido

]);
}

jest.spyOn(api, 'getPedidosByEmail').mockImplementation(miGetPedidos);
//jest.spyOn(solid, 'useSession').mockImplementation(()=> ({session:{info:{webId:"abc#abc"}}} as any));
jest.mock("@inrupt/solid-client");
await act(async () => {
render(<BrowserRouter><PedidosList/></BrowserRouter>)});
expect(screen.getByText('En camino')).toBeInTheDocument();
});
1 change: 1 addition & 0 deletions webapp/src/components/pedidos/PedidosList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@inrupt/solid-client";

async function retirevePODEmail(webID: string): Promise<string> {
if (!webID) {return ""};
let profileDocumentURI = webID.split("#")[0]
let myDataSet = await getSolidDataset(profileDocumentURI)
let profile = getThing(myDataSet, webID)
Expand Down

0 comments on commit f7cb558

Please sign in to comment.