Skip to content

Commit 373f3ab

Browse files
Add findOffersForDate query
1 parent e8a27e7 commit 373f3ab

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import FindAvailableProductForm from './components/FindAvailableProductForm';
33
import ProductForm from './components/ProductForm';
44
import { ToastContainer, toast } from 'react-toastify';
55
import { useEffect } from 'react';
6-
import GetDispatchedProductByDateForm from './components/GetDispatchedProductByDateForm';
6+
import FindOffersForDate from './components/FindOffersForDate';
77

88
function App() {
99

@@ -27,7 +27,7 @@ function App() {
2727
<br/>
2828
<FindAvailableProductForm/>
2929
<br/>
30-
<GetDispatchedProductByDateForm/>
30+
<FindOffersForDate/>
3131
<br/>
3232
<ToastContainer />
3333
</div>

src/__test__/App.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ApolloProvider } from "@apollo/client";
66
import client from "../apolloClient";
77
import FindAvailableProductForm from "../components/FindAvailableProductForm";
88
import { startGraphQlStub, stopGraphQlStub } from "specmatic";
9-
import GetDispatchedProductByDateForm from "../components/GetDispatchedProductByDateForm";
9+
import FindOffersForDate from "../components/FindOffersForDate";
1010

1111
global.setImmediate = global.setImmediate || ((fn, ...args) => global.setTimeout(fn, 0, ...args));
1212
let stub;
@@ -74,20 +74,23 @@ describe("App component tests", () => {
7474
});
7575
});
7676

77-
test("should fetch dispatched product by date", async () => {
77+
test("should fetch offers valid until date", async () => {
7878
render(
7979
<ApolloProvider client={client}>
80-
<GetDispatchedProductByDateForm />
80+
<FindOffersForDate />
8181
</ApolloProvider>
8282
);
8383

84-
fireEvent.change(screen.getByTestId("date"), { target: { value: "2020-12-12" } });
84+
fireEvent.change(screen.getByTestId("date"), { target: { value: "2024-12-31" } });
8585
fireEvent.click(screen.getByTestId("submit"));
8686

8787
await waitFor(() => {
88-
expect(screen.getByText("12/12/2020")).toBeInTheDocument(); // Adjust the date format if needed
88+
expect(screen.getByText("WKND30")).toBeInTheDocument();
89+
expect(screen.getByText("12/12/2024")).toBeInTheDocument();
90+
expect(screen.getByText("SUNDAY20")).toBeInTheDocument();
91+
expect(screen.getByText("12/25/2024")).toBeInTheDocument();
8992
});
90-
});
93+
});
9194
});
9295

9396
afterAll(async () => {
Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useState } from 'react';
22
import { useLazyQuery, gql } from '@apollo/client';
33
import { toast } from 'react-toastify';
44
import 'react-toastify/dist/ReactToastify.css';
55

6-
const GetDispatchedProductByDateForm = () => {
6+
const FindOffersForDate = () => {
77
const [date, setDate] = useState('');
8-
const [dispatchedProduct, setDispatchedProduct] = useState(null);
8+
const [offers, setOffers] = useState(null);
99

1010
const formatDateString = (dateString) => {
11-
return dateString.replace(/-/g, '/'); // Replace hyphens with slashes
11+
return dateString.replace(/-/g, '/');
1212
};
1313

14-
const GET_DISPATCHED_PRODUCT_BY_DATE = gql`
14+
const FIND_OFFERS_FOR_DATE = gql`
1515
query {
16-
getDispatchedProductByDate(date: "${formatDateString(date)}") {
17-
id
18-
dispatchDate
16+
findOffersForDate(date: "${formatDateString(date)}") {
17+
offerCode
18+
validUntil
1919
}
2020
}
2121
`;
2222

23-
const [getDispatchedProductByDate, { loading }] = useLazyQuery(GET_DISPATCHED_PRODUCT_BY_DATE, {
23+
const [findOffersForDate, { loading }] = useLazyQuery(FIND_OFFERS_FOR_DATE, {
2424
fetchPolicy: 'network-only',
2525
onCompleted: (data) => {
26-
setDispatchedProduct(data.getDispatchedProductByDate);
26+
setOffers(data.findOffersForDate);
2727
},
2828
onError: () => {
29-
setDispatchedProduct(null);
30-
toast.error('Error fetching dispatched product');
29+
setOffers(null);
30+
toast.error('Error fetching offers');
3131
},
3232
});
3333

@@ -40,12 +40,12 @@ const GetDispatchedProductByDateForm = () => {
4040
return;
4141
}
4242

43-
getDispatchedProductByDate({ variables: { date } });
43+
findOffersForDate({ variables: { date } });
4444
};
4545

4646
return (
4747
<div className="max-w-md mx-auto p-8 bg-white shadow-lg rounded-lg">
48-
<h1 className="text-2xl font-bold mb-4">Get Dispatched Product by Date</h1>
48+
<h1 className="text-2xl font-bold mb-4">Find offers valid until a certain date</h1>
4949
<form onSubmit={handleSubmit}>
5050
<div className="mb-4">
5151
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="date">
@@ -71,18 +71,21 @@ const GetDispatchedProductByDateForm = () => {
7171
</button>
7272
</div>
7373
</form>
74-
{dispatchedProduct && (
75-
<div className="mt-8">
76-
<h2 className="text-xl font-bold mb-4">Dispatched Product</h2>
77-
<div className="p-4 border rounded-lg bg-gray-50 shadow-sm">
78-
<p><strong>ID:</strong> {dispatchedProduct.id}</p>
79-
<p><strong>Dispatch Date:</strong> {new Date(dispatchedProduct.dispatchDate).toLocaleDateString()}</p>
74+
{offers && offers.map(offer => {
75+
return (<>
76+
<div className="mt-8">
77+
<div className="p-4 border rounded-lg bg-gray-50 shadow-sm">
78+
<p><strong>Offer code:</strong> {offer.offerCode}</p>
79+
<p><strong>Valid Until:</strong> {new Date(offer.validUntil).toLocaleDateString()}</p>
80+
</div>
8081
</div>
81-
</div>
82-
)}
82+
</>
83+
)
84+
})
85+
}
8386
</div>
8487
);
8588
};
8689

87-
export default GetDispatchedProductByDateForm;
90+
export default FindOffersForDate;
8891

0 commit comments

Comments
 (0)