diff --git a/src/App.js b/src/App.js index bf75542..21d27a4 100644 --- a/src/App.js +++ b/src/App.js @@ -3,7 +3,7 @@ import FindAvailableProductForm from './components/FindAvailableProductForm'; import ProductForm from './components/ProductForm'; import { ToastContainer, toast } from 'react-toastify'; import { useEffect } from 'react'; -import GetDispatchedProductByDateForm from './components/GetDispatchedProductByDateForm'; +import FindOffersForDate from './components/FindOffersForDate'; function App() { @@ -27,7 +27,7 @@ function App() {

- +
diff --git a/src/__test__/App.test.js b/src/__test__/App.test.js index f1b7034..091bbf7 100644 --- a/src/__test__/App.test.js +++ b/src/__test__/App.test.js @@ -6,7 +6,7 @@ import { ApolloProvider } from "@apollo/client"; import client from "../apolloClient"; import FindAvailableProductForm from "../components/FindAvailableProductForm"; import { startGraphQlStub, stopGraphQlStub } from "specmatic"; -import GetDispatchedProductByDateForm from "../components/GetDispatchedProductByDateForm"; +import FindOffersForDate from "../components/FindOffersForDate"; global.setImmediate = global.setImmediate || ((fn, ...args) => global.setTimeout(fn, 0, ...args)); let stub; @@ -74,20 +74,23 @@ describe("App component tests", () => { }); }); - test("should fetch dispatched product by date", async () => { + test("should fetch offers valid until date", async () => { render( - + ); - fireEvent.change(screen.getByTestId("date"), { target: { value: "2020-12-12" } }); + fireEvent.change(screen.getByTestId("date"), { target: { value: "2024-12-31" } }); fireEvent.click(screen.getByTestId("submit")); await waitFor(() => { - expect(screen.getByText("12/12/2020")).toBeInTheDocument(); // Adjust the date format if needed + expect(screen.getByText("WKND30")).toBeInTheDocument(); + expect(screen.getByText("12/12/2024")).toBeInTheDocument(); + expect(screen.getByText("SUNDAY20")).toBeInTheDocument(); + expect(screen.getByText("12/25/2024")).toBeInTheDocument(); }); -}); + }); }); afterAll(async () => { diff --git a/src/components/GetDispatchedProductByDateForm.js b/src/components/FindOffersForDate.js similarity index 57% rename from src/components/GetDispatchedProductByDateForm.js rename to src/components/FindOffersForDate.js index 6d41511..344711d 100644 --- a/src/components/GetDispatchedProductByDateForm.js +++ b/src/components/FindOffersForDate.js @@ -1,33 +1,33 @@ -import React, { useEffect, useState } from 'react'; +import React, { useState } from 'react'; import { useLazyQuery, gql } from '@apollo/client'; import { toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; -const GetDispatchedProductByDateForm = () => { +const FindOffersForDate = () => { const [date, setDate] = useState(''); - const [dispatchedProduct, setDispatchedProduct] = useState(null); + const [offers, setOffers] = useState(null); const formatDateString = (dateString) => { - return dateString.replace(/-/g, '/'); // Replace hyphens with slashes + return dateString.replace(/-/g, '/'); }; - const GET_DISPATCHED_PRODUCT_BY_DATE = gql` + const FIND_OFFERS_FOR_DATE = gql` query { - getDispatchedProductByDate(date: "${formatDateString(date)}") { - id - dispatchDate + findOffersForDate(date: "${formatDateString(date)}") { + offerCode + validUntil } } `; - const [getDispatchedProductByDate, { loading }] = useLazyQuery(GET_DISPATCHED_PRODUCT_BY_DATE, { + const [findOffersForDate, { loading }] = useLazyQuery(FIND_OFFERS_FOR_DATE, { fetchPolicy: 'network-only', onCompleted: (data) => { - setDispatchedProduct(data.getDispatchedProductByDate); + setOffers(data.findOffersForDate); }, onError: () => { - setDispatchedProduct(null); - toast.error('Error fetching dispatched product'); + setOffers(null); + toast.error('Error fetching offers'); }, }); @@ -40,12 +40,12 @@ const GetDispatchedProductByDateForm = () => { return; } - getDispatchedProductByDate({ variables: { date } }); + findOffersForDate({ variables: { date } }); }; return (
-

Get Dispatched Product by Date

+

Find offers valid until a certain date

- {dispatchedProduct && ( -
-

Dispatched Product

-
-

ID: {dispatchedProduct.id}

-

Dispatch Date: {new Date(dispatchedProduct.dispatchDate).toLocaleDateString()}

+ {offers && offers.map(offer => { + return (<> +
+
+

Offer code: {offer.offerCode}

+

Valid Until: {new Date(offer.validUntil).toLocaleDateString()}

+
-
- )} + + ) + }) + }
); }; -export default GetDispatchedProductByDateForm; +export default FindOffersForDate;