|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent, screen } from '@testing-library/react'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import { createStore } from 'redux'; |
| 5 | +import WeeklySummaryOptions from '../WeeklySummaryOptions'; |
| 6 | + |
| 7 | +// Mock Redux store for darkMode |
| 8 | +const mockStore = (initialState) => { |
| 9 | + return createStore((state = initialState, action) => state); |
| 10 | +}; |
| 11 | + |
| 12 | +describe('WeeklySummaryOptions Component', () => { |
| 13 | + // Test to render the summary options |
| 14 | + test('renders summary options', () => { |
| 15 | + const store = mockStore({ |
| 16 | + theme: { darkMode: false }, |
| 17 | + }); |
| 18 | + |
| 19 | + render( |
| 20 | + <Provider store={store}> |
| 21 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 22 | + </Provider> |
| 23 | + ); |
| 24 | + |
| 25 | + // Check if the select dropdown is rendered |
| 26 | + expect(screen.getByRole('combobox')).toBeInTheDocument(); |
| 27 | + |
| 28 | + // Ensure all options are present in the dropdown |
| 29 | + const options = screen.getAllByRole('option'); |
| 30 | + expect(options).toHaveLength(9); |
| 31 | + }); |
| 32 | + |
| 33 | + // Test if handleUserProfile is called on change |
| 34 | + test('handleUserProfile called on change', () => { |
| 35 | + const store = mockStore({ |
| 36 | + theme: { darkMode: false }, |
| 37 | + }); |
| 38 | + |
| 39 | + const handleUserProfile = jest.fn(); |
| 40 | + |
| 41 | + render( |
| 42 | + <Provider store={store}> |
| 43 | + <WeeklySummaryOptions handleUserProfile={handleUserProfile} /> |
| 44 | + </Provider> |
| 45 | + ); |
| 46 | + |
| 47 | + // Simulate a change in the select dropdown |
| 48 | + fireEvent.change(screen.getByRole('combobox'), { target: { value: 'Team Marigold' } }); |
| 49 | + |
| 50 | + // Ensure the handleUserProfile function is called |
| 51 | + expect(handleUserProfile).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + // Test if the default value is set to "Required" |
| 55 | + test('default value set to "Required"', () => { |
| 56 | + const store = mockStore({ |
| 57 | + theme: { darkMode: false }, |
| 58 | + }); |
| 59 | + |
| 60 | + render( |
| 61 | + <Provider store={store}> |
| 62 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 63 | + </Provider> |
| 64 | + ); |
| 65 | + |
| 66 | + // Check if the default value is "Required" |
| 67 | + expect(screen.getByRole('combobox').value).toBe('Required'); |
| 68 | + }); |
| 69 | + |
| 70 | + // Test if the correct value attributes are set |
| 71 | + test('correct value attributes', () => { |
| 72 | + const store = mockStore({ |
| 73 | + theme: { darkMode: false }, |
| 74 | + }); |
| 75 | + |
| 76 | + render( |
| 77 | + <Provider store={store}> |
| 78 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 79 | + </Provider> |
| 80 | + ); |
| 81 | + |
| 82 | + // Test if the options have the correct value attributes |
| 83 | + const options = screen.getAllByRole('option'); |
| 84 | + expect(options[0].value).toBe('Required'); |
| 85 | + expect(options[1].value).toBe('Not Required'); |
| 86 | + expect(options[2].value).toBe('Team Fabulous'); |
| 87 | + // Add further checks for all options... |
| 88 | + }); |
| 89 | + |
| 90 | + // Test if the value changes when a different option is selected |
| 91 | + test('value changes when a different option is selected', () => { |
| 92 | + const store = mockStore({ |
| 93 | + theme: { darkMode: false }, |
| 94 | + }); |
| 95 | + |
| 96 | + const handleUserProfile = jest.fn(); |
| 97 | + |
| 98 | + render( |
| 99 | + <Provider store={store}> |
| 100 | + <WeeklySummaryOptions handleUserProfile={handleUserProfile} /> |
| 101 | + </Provider> |
| 102 | + ); |
| 103 | + |
| 104 | + // Select a different option |
| 105 | + fireEvent.change(screen.getByRole('combobox'), { target: { value: 'Team Marigold' } }); |
| 106 | + |
| 107 | + // Assert the updated value of the select input |
| 108 | + expect(screen.getByRole('combobox').value).toBe('Team Marigold'); |
| 109 | + }); |
| 110 | + |
| 111 | + // Test if the component renders without default value |
| 112 | + test('render without default value', () => { |
| 113 | + const store = mockStore({ |
| 114 | + theme: { darkMode: false }, |
| 115 | + }); |
| 116 | + |
| 117 | + render( |
| 118 | + <Provider store={store}> |
| 119 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 120 | + </Provider> |
| 121 | + ); |
| 122 | + |
| 123 | + // Test if the select element has a value attribute set to "Required" (default) or not |
| 124 | + expect(screen.getByRole('combobox').value).toBe('Required'); |
| 125 | + }); |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + // Test if dark mode styling is applied |
| 131 | + test('applies dark mode styling', () => { |
| 132 | + const store = mockStore({ |
| 133 | + theme: { darkMode: true }, |
| 134 | + }); |
| 135 | + |
| 136 | + render( |
| 137 | + <Provider store={store}> |
| 138 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 139 | + </Provider> |
| 140 | + ); |
| 141 | + |
| 142 | + const selectElement = screen.getByRole('combobox'); |
| 143 | + |
| 144 | + // Test if dark mode classes are applied |
| 145 | + expect(selectElement).toHaveClass('bg-darkmode-liblack'); |
| 146 | + expect(selectElement).toHaveClass('text-light'); |
| 147 | + expect(selectElement).toHaveClass('border-0'); |
| 148 | + }); |
| 149 | + |
| 150 | + // Test if dark mode styling is not applied when darkMode is false |
| 151 | + test('does not apply dark mode styling when darkMode is false', () => { |
| 152 | + const store = mockStore({ |
| 153 | + theme: { darkMode: false }, |
| 154 | + }); |
| 155 | + |
| 156 | + render( |
| 157 | + <Provider store={store}> |
| 158 | + <WeeklySummaryOptions handleUserProfile={() => {}} /> |
| 159 | + </Provider> |
| 160 | + ); |
| 161 | + |
| 162 | + const selectElement = screen.getByRole('combobox'); |
| 163 | + |
| 164 | + // Test if no dark mode classes are applied |
| 165 | + expect(selectElement).not.toHaveClass('bg-darkmode-liblack'); |
| 166 | + expect(selectElement).not.toHaveClass('text-light'); |
| 167 | + expect(selectElement).not.toHaveClass('border-0'); |
| 168 | + }); |
| 169 | +}); |
0 commit comments