|
| 1 | +import { fireEvent, render } from "@testing-library/react"; |
| 2 | +import React from "react"; |
| 3 | + |
| 4 | +import SidebarCollection from "../features/Sidebar/Collection/SidebarCollection"; |
| 5 | + |
| 6 | +const mockData = [ |
| 7 | + { |
| 8 | + url: "http://netflix.com", |
| 9 | + method: "GET", |
| 10 | + headers: { |
| 11 | + "Cache-Control": "no-cache", |
| 12 | + Accept: "*/*", |
| 13 | + "Accept-Encoding": "gzip,deflate", |
| 14 | + Connection: "keep-alive", |
| 15 | + }, |
| 16 | + requestedTime: 1658086010977, |
| 17 | + favoritedTime: 1658086023824, |
| 18 | + isUserFavorite: false, |
| 19 | + id: "76deed12-b222-45a5-a3ac-18a785fcef7c", |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +describe("SidebarCollection component test", () => { |
| 24 | + it("should display correct sidebar empty message when props are passed", () => { |
| 25 | + const { getByText } = render(<SidebarCollection sidebarOption="History" />); |
| 26 | + |
| 27 | + expect( |
| 28 | + getByText(/Your history collection seems to be empty./i), |
| 29 | + ).toBeInTheDocument(); |
| 30 | + expect( |
| 31 | + getByText(/Start making requests to view your history collection./i), |
| 32 | + ).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should display correct sidebar empty message when props are passed again", () => { |
| 36 | + const { getByText } = render( |
| 37 | + <SidebarCollection sidebarOption="Favorites" />, |
| 38 | + ); |
| 39 | + |
| 40 | + expect( |
| 41 | + getByText(/Your favorites collection seems to be empty./i), |
| 42 | + ).toBeInTheDocument(); |
| 43 | + expect( |
| 44 | + getByText( |
| 45 | + /Press the heart icon from your history collection to add it to your favorites collection./i, |
| 46 | + ), |
| 47 | + ).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should display input placeholder text correctly when there is data to be displayed", () => { |
| 51 | + const { getByPlaceholderText } = render( |
| 52 | + <SidebarCollection userCollection={mockData} />, |
| 53 | + ); |
| 54 | + |
| 55 | + expect(getByPlaceholderText(/Search collection.../i)).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should change value when user inputs value", () => { |
| 59 | + const testInputValue = "facebook?!"; |
| 60 | + const { getByPlaceholderText } = render( |
| 61 | + <SidebarCollection userCollection={mockData} />, |
| 62 | + ); |
| 63 | + |
| 64 | + fireEvent.change(getByPlaceholderText(/Search collection.../i), { |
| 65 | + target: { value: testInputValue }, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(getByPlaceholderText(/Search collection.../i).value).toBe( |
| 69 | + testInputValue, |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should display correct url when passed userCollection as props", () => { |
| 74 | + const { getByText } = render( |
| 75 | + <SidebarCollection userCollection={mockData} />, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(getByText("http://netflix.com")).toBeInTheDocument(); |
| 79 | + expect(getByText(/GET/i)).toBeInTheDocument(); |
| 80 | + expect(getByText(/Added 19 hours ago/i)).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should delete collection when delete button is clicked", async () => { |
| 84 | + const mockHandleDeleteButton = () => { |
| 85 | + mockData.splice(0, mockData.length); |
| 86 | + }; |
| 87 | + |
| 88 | + const { findByRole } = render( |
| 89 | + <SidebarCollection |
| 90 | + userCollection={mockData} |
| 91 | + handleDeleteButton={mockHandleDeleteButton} |
| 92 | + />, |
| 93 | + ); |
| 94 | + |
| 95 | + const icon = await findByRole("iconWrapper"); |
| 96 | + |
| 97 | + fireEvent.click(icon.firstChild); |
| 98 | + |
| 99 | + expect(mockData.length).toBe(0); |
| 100 | + }); |
| 101 | +}); |
0 commit comments