|
1 | 1 | // Copyright (c) Gridiron Survivor. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import React from 'react'; |
5 | | -import { render, screen } from '@testing-library/react'; |
| 4 | +import { render, screen, within } from '@testing-library/react'; |
6 | 5 | import GiftDetailsView from './GiftDetailsView'; |
7 | 6 | import { describe } from 'node:test'; |
8 | | -import { userEvent } from '@storybook/test'; |
9 | 7 |
|
10 | 8 | const mockGift = { |
11 | 9 | id: '1', |
12 | 10 | title: 'Blanket', |
13 | 11 | price: '$29.99', |
14 | 12 | description: 'fuzzy blanket', |
15 | 13 | matchReasons: ['fits theme', 'highly rated'], |
16 | | - matchScore: 0.98, |
17 | | - imageUrl: |
18 | | - 'https://www.amazon.com/Cozzenity-Checkered-Blanket-Blankets-Lightweight/dp/B0F13991KJ/ref=sr_1_1_sspa?dib=eyJ2IjoiMSJ9.6wzR8Y2VhlZ6NsPFYW2cadhZFdeTIN7H6f_K3SufPMLJMToDPu0O_vCflzFycFVgQu9w8Xa2yC56dPFThjqnH5R3cM6i--ozxTqrjiTRLFj0OR_jh8wRe9Y0992_NPU9HrrjbKFeAWsFHKCcDxcGytcREvh7r2XZf-F-jcpqtBJFNhezM0lAhCO27_HYTgtb21GKie4DnzAWwESS488BC_7hi7FVKkDLJRJSmBIBro0WxlgebmNyTq6QqPcwQpM-o2Xa9hLm7DvYG0QSsRcR9QhK0aiRkQs6wLSW0YJHQBI.GiOlWv46xAPaV98XHUz3LzGTnXgnhW8lH6oq09Rz7Jk&dib_tag=se&hvadid=694526290027&hvdev=c&hvexpln=67&hvlocphy=9031568&hvnetw=g&hvocijid=18272751582845398260--&hvqmt=e&hvrand=18272751582845398260&hvtargid=kwd-460419430462&hydadcr=19233_13355042&keywords=amazon%2Bfuzzy%2Bblanket&mcid=5b4485e6f4883f9f84c099bcd937f120&qid=1747259407&sr=8-1-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY&th=1', |
| 14 | + matchScore: 98, |
| 15 | + imageUrl: 'https://example.com/fuzzy-blanket.jpg', |
19 | 16 | }; |
20 | 17 |
|
21 | | -const mockBadGift = { |
22 | | - id: '1', |
23 | | - title: 'Blanket', |
24 | | - price: '$29.99', |
25 | | - description: 'fuzzy blanket', |
26 | | - matchReasons: ['fits theme', 'highly rated'], |
27 | | - matchScore: 0.98, |
28 | | - imageUrl:'bad_url', |
29 | | -} |
30 | | - |
| 18 | +const mockGiftWithBadUrl = { |
| 19 | + ...mockGift, |
| 20 | + imageUrl: 'bad_url', |
| 21 | +}; |
31 | 22 |
|
32 | 23 | const mockHandleFeedback = jest.fn(); |
33 | 24 |
|
34 | 25 | describe('GiftDetailsView', () => { |
35 | | - //testing the gift and handleFeedback props |
36 | | - it('renders the component with correct props', () => { |
| 26 | + it('renders the component with gift details', () => { |
37 | 27 | render( |
38 | 28 | <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
39 | 29 | ); |
40 | | - }); |
41 | 30 |
|
42 | | - //testing the Amazon URL |
43 | | - it('generates the correct Amazon URL', async () => { |
44 | | - render( |
45 | | - <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
46 | | - ); |
47 | | - |
48 | | - const hrefLink = screen.getByRole('link', { name: /view/i }); |
49 | | - await userEvent.click(hrefLink); |
| 31 | + const giftImage = screen.getByRole('img'); |
| 32 | + expect(giftImage).toHaveAttribute('src', mockGift.imageUrl); |
50 | 33 |
|
51 | | - expect(hrefLink).toHaveAttribute( |
52 | | - 'href', |
53 | | - 'https://www.amazon.com/s?k=Blanket', |
54 | | - ); |
| 34 | + const matchScore = screen.getByRole('score'); |
| 35 | + expect(matchScore).toHaveTextContent(/98% Match/); |
55 | 36 |
|
56 | | - }) |
| 37 | + const price = screen.getByLabelText(/price/i); |
| 38 | + expect(price).toHaveTextContent(mockGift.price); |
57 | 39 |
|
58 | | - // testing conditional rendering for ImageURL |
59 | | - it('renders image if URL is present', () => { |
60 | | - render( |
61 | | - <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
62 | | - ); |
| 40 | + const title = screen.getByRole('heading', { |
| 41 | + level: 3, |
| 42 | + name: mockGift.title, |
| 43 | + }); |
| 44 | + expect(title).toBeInTheDocument(); |
63 | 45 |
|
64 | | - const image = screen.getByTestId('valid-image'); |
65 | | - expect(image).toBeInTheDocument(); |
66 | | - }) |
| 46 | + const description = screen.getByText(mockGift.description); |
| 47 | + expect(description).toHaveTextContent(mockGift.description); |
67 | 48 |
|
68 | | - //testing that Gift Icon appears if not valid image URL |
69 | | - //CAN NOT FIGURE THIS OUT |
70 | | - it('renders Gift Icon if URl is not valid', ()=> { |
71 | | - render( |
72 | | - <GiftDetailsView gift={mockBadGift} handleFeedback={mockHandleFeedback} />, |
73 | | - ); |
| 49 | + const matchReasons = screen.getByRole('list'); |
| 50 | + const matchItems = within(matchReasons) |
| 51 | + .getAllByRole('listitem') |
| 52 | + .map((li) => li.textContent); |
| 53 | + expect(matchItems).toEqual(expect.arrayContaining(mockGift.matchReasons)); |
| 54 | + }); |
74 | 55 |
|
75 | | - const giftIcon = screen.getByTestId('gift-icon'); |
76 | | - expect(giftIcon).toBeInTheDocument(); |
77 | | - }) |
| 56 | + it('generates the correct Amazon URL', async () => { |
| 57 | + // Cloning the env variable |
| 58 | + const original_NEXT_PUBLIC_AMAZON_AFFILIATE_TAG = |
| 59 | + process.env.NEXT_PUBLIC_AMAZON_AFFILIATE_TAG; |
| 60 | + process.env.NEXT_PUBLIC_AMAZON_AFFILIATE_TAG = 'test-tag'; |
78 | 61 |
|
79 | | - it('renders a valid MatchScore and price', () => { |
80 | 62 | render( |
81 | 63 | <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
82 | 64 | ); |
83 | 65 |
|
84 | | - const matchScore = screen.getByTestId('valid-matchScore'); |
85 | | - expect(matchScore).toBeInTheDocument(); |
86 | | - |
87 | | - const price = screen.getByTestId('valid-price'); |
88 | | - expect(price).toBeInTheDocument(); |
89 | | - }) |
90 | | - |
91 | | - it('renders a valid title and description', () => { |
92 | | - render( |
93 | | - <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
| 66 | + const hrefLink = screen.getByRole('link', { name: /view/i }); |
| 67 | + expect(hrefLink).toHaveAttribute( |
| 68 | + 'href', |
| 69 | + `https://www.amazon.com/s?k=${mockGift.title}&tag=${process.env.NEXT_PUBLIC_AMAZON_AFFILIATE_TAG}`, |
94 | 70 | ); |
95 | 71 |
|
96 | | - const title = screen.getByTestId('valid-title'); |
97 | | - expect(title).toBeInTheDocument(); |
98 | | - |
99 | | - const description = screen.getByTestId('valid-description'); |
100 | | - expect(description).toBeInTheDocument(); |
| 72 | + // Restoring the env variable |
| 73 | + process.env.NEXT_PUBLIC_AMAZON_AFFILIATE_TAG = |
| 74 | + original_NEXT_PUBLIC_AMAZON_AFFILIATE_TAG; |
101 | 75 | }); |
102 | 76 |
|
103 | | - it('correctly renders a list for match reasons', () => { |
| 77 | + it('renders default Gift Icon on invalid image url', () => { |
104 | 78 | render( |
105 | | - <GiftDetailsView gift={mockGift} handleFeedback={mockHandleFeedback} />, |
| 79 | + <GiftDetailsView |
| 80 | + gift={mockGiftWithBadUrl} |
| 81 | + handleFeedback={mockHandleFeedback} |
| 82 | + />, |
106 | 83 | ); |
107 | 84 |
|
108 | | - mockGift.matchReasons.forEach((reason) => { |
109 | | - expect(screen.getByText(reason)).toBeInTheDocument(); |
110 | | - }) |
111 | | - }) |
| 85 | + const giftIcon = screen.getByRole('img', { |
| 86 | + name: /gift placeholder image/i, |
| 87 | + }); |
112 | 88 |
|
| 89 | + expect(giftIcon).toBeInTheDocument(); |
| 90 | + }); |
113 | 91 | }); |
0 commit comments