|
2 | 2 | /* eslint-disable import/no-named-as-default-member */ |
3 | 3 | import React from 'react'; |
4 | 4 | import { render, fireEvent, screen } from '@testing-library/react'; |
5 | | -import Header from '../../app/components/Header'; |
| 5 | +import Header from '../../app/components/Header/Header'; |
| 6 | +import ServiceDropdown from '../../app/components/Header/ServiceDropdown'; |
6 | 7 | import { DashboardContext } from '../../app/context/DashboardContext'; |
7 | 8 | import { ApplicationContext } from '../../app/context/ApplicationContext'; |
8 | 9 | import { HashRouter as Router } from 'react-router-dom'; |
@@ -55,13 +56,59 @@ describe('Speed Chart', () => { |
55 | 56 | }); |
56 | 57 |
|
57 | 58 | // trying to test the functionality of component not passed as props |
58 | | - it('Should check/uncheck the checkbox when clicking services', () => { |
59 | | - // const checkBox = screen.getByRole('checkbox'); |
60 | | - // fireEvent.click(checkBox); |
61 | | - // expect(checkBox.parentElement).toHaveClass('selected'); |
62 | | - // fireEvent.click(checkBox); |
63 | | - // expect(checkBox.parentElement).not.toHaveClass('selected'); |
64 | | - }); |
| 59 | + // it('Should check/uncheck the checkbox when clicking services', () => { |
| 60 | + // const checkBox = screen.getByTestId('checkbox'); |
| 61 | + // fireEvent.click(checkBox); |
| 62 | + // expect(checkBox.parentElement).toHaveClass('selected'); |
| 63 | + // fireEvent.click(checkBox); |
| 64 | + // expect(checkBox.parentElement).not.toHaveClass('selected'); |
| 65 | + // }); |
65 | 66 |
|
66 | 67 | it('Should also change selectModal to true or false', () => {}); |
67 | 68 | }); |
| 69 | + |
| 70 | +describe('ServiceDropdown test', () => { |
| 71 | + it('opens and closes ServiceDropdown component on click', () => { |
| 72 | + const servicesData = [ |
| 73 | + { microservice: 'inventory' }, |
| 74 | + { microservice: 'orders' }, |
| 75 | + { microservice: 'auth' } |
| 76 | + ]; |
| 77 | + |
| 78 | + // Define initial selected services state |
| 79 | + const selectedServices = []; |
| 80 | + |
| 81 | + // Define a mock toggleDropdown function |
| 82 | + const toggleDropdown = jest.fn(); |
| 83 | + |
| 84 | + // Render the ServiceDropdown component |
| 85 | + render( |
| 86 | + <ServiceDropdown |
| 87 | + servicesData={servicesData} |
| 88 | + selectedServices={selectedServices} |
| 89 | + setSelectedServices={jest.fn()} // Mock setSelectedServices function |
| 90 | + isOpen={false} // Assuming dropdown is closed initially |
| 91 | + toggleDropdown={toggleDropdown} |
| 92 | + /> |
| 93 | + ); |
| 94 | + |
| 95 | + // Assert that dropdown is initially closed |
| 96 | + expect(screen.queryByText('inventory')).not.toBeInTheDocument(); |
| 97 | + |
| 98 | + // simulate click event on button within ServiceDropdown component |
| 99 | + const selectButton = screen.getByTestId('ssButton'); |
| 100 | + fireEvent.click(selectButton); |
| 101 | + |
| 102 | + // expect the toggleDropdown function to have been called |
| 103 | + expect(toggleDropdown).toHaveBeenCalled(); |
| 104 | + |
| 105 | + // Assert that dropdown is now open by checking that service is rendered |
| 106 | + // expect(screen.getByText('inventory')).toBeInTheDocument(); |
| 107 | + |
| 108 | + // // Simulate click event to close the dropdown |
| 109 | + // fireEvent.click(selectButton); |
| 110 | + |
| 111 | + // // Assert that dropdown is now closed by checking that service is no longer rendered |
| 112 | + // expect(screen.getByText('inventory')).not.toBeInTheDocument(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments