From 3d670c708373a51f587e354b54bfcd9a9720be52 Mon Sep 17 00:00:00 2001 From: teodoraj Date: Wed, 9 Apr 2025 21:34:15 +0100 Subject: [PATCH] task1: replace hard coded data and fix the test --- .../PropertyListing/PropertyListing.jsx | 30 +++++-------------- .../tests/PropertyListing.spec.jsx | 26 ++++++++++++++-- src/hooks/useGetProperties.js | 18 +++++++++++ 3 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 src/hooks/useGetProperties.js diff --git a/src/components/PropertyListing/PropertyListing.jsx b/src/components/PropertyListing/PropertyListing.jsx index ed43fb5..983179d 100644 --- a/src/components/PropertyListing/PropertyListing.jsx +++ b/src/components/PropertyListing/PropertyListing.jsx @@ -1,32 +1,18 @@ import React from 'react'; import PropertyCard from '../PropertyCard'; import './PropertyListing.scss'; - -const DUMMY_PROPERTY = { - id: 73864112, - bedrooms: 3, - summary: 'Property 1 Situated moments from the River Thames in Old Chelsea...', - displayAddress: '1 CHEYNE WALK, CHELSEA, SW3', - propertyType: 'Flat', - price: 1950000, - branchName: 'M2 Property, London', - propertyUrl: '/property-for-sale/property-73864112.html', - contactUrl: '/property-for-sale/contactBranch.html?propertyId=73864112', - propertyTitle: '3 bedroom flat for sale', - mainImage: - 'https://media.rightmove.co.uk/dir/crop/10:9-16:9/38k/37655/53588679/37655_CAM170036_IMG_01_0000_max_476x317.jpg', -}; +import { useGetProperties } from '../../hooks/useGetProperties'; const PropertyListing = () => { + const properties = useGetProperties(); + return ( ); }; diff --git a/src/components/PropertyListing/tests/PropertyListing.spec.jsx b/src/components/PropertyListing/tests/PropertyListing.spec.jsx index 807d63b..f7d0185 100644 --- a/src/components/PropertyListing/tests/PropertyListing.spec.jsx +++ b/src/components/PropertyListing/tests/PropertyListing.spec.jsx @@ -3,11 +3,33 @@ import { render, screen } from '@testing-library/react'; import { within } from '@testing-library/dom'; import PropertyListing from '../PropertyListing'; +const mockUseGetProperties= jest.fn(); +jest.mock('../../../hooks/useGetProperties', () => ({ + useGetProperties: () => mockUseGetProperties(), + })) + describe('PropertyListing', () => { - it('should render five property cards', async () => { + it('should render list items when data is present', async () => { + const mockData = [ + { + id: 73864112, + bedrooms: 3, + summary: 'Property 1 Situated moments from the River Thames in Old Chelsea...', + displayAddress: '1 CHEYNE WALK, CHELSEA, SW3', + propertyType: 'Flat', + price: 1950000, + branchName: 'M2 Property, London', + propertyUrl: '/property-for-sale/property-73864112.html', + contactUrl: '/property-for-sale/contactBranch.html?propertyId=73864112', + propertyTitle: '3 bedroom flat for sale', + mainImage: + 'https://media.rightmove.co.uk/dir/crop/10:9-16:9/38k/37655/53588679/37655_CAM170036_IMG_01_0000_max_476x317.jpg', + } + ] + mockUseGetProperties.mockReturnValue(mockData) render(); const propertiesList = screen.getByRole('list'); const propertyCards = await within(propertiesList).findAllByRole('listitem'); - expect(propertyCards).toHaveLength(5); + expect(propertyCards).toHaveLength(mockData.length); }); }); diff --git a/src/hooks/useGetProperties.js b/src/hooks/useGetProperties.js new file mode 100644 index 0000000..6e3b41b --- /dev/null +++ b/src/hooks/useGetProperties.js @@ -0,0 +1,18 @@ +import { useEffect, useState } from 'react'; + +export const useGetProperties = () => { + const [properties, setProperties] = useState(null); + useEffect(() => { + const fetchData = async () => { + try { + const response = await fetch('http://localhost:3000/api/properties'); + const data = await response.json(); + setProperties(data); + } catch (error) { + console.log(error.message); + } + }; + fetchData(); + }, []); + return properties; +};