|
1 | 1 | /* eslint-disable no-console */
|
2 | 2 | import '@testing-library/jest-dom';
|
3 | 3 | import React from 'react';
|
4 |
| -import { render } from '@testing-library/react'; |
| 4 | +import { render, screen, fireEvent } from '@testing-library/react'; |
5 | 5 | import { CioAutocomplete } from '../../../src';
|
6 | 6 | import { mockCioClientJS } from '../../test-utils';
|
7 | 7 | import { apiKey as DEMO_API_KEY, onSubmitDefault as onSubmit } from '../../../src/constants';
|
@@ -126,4 +126,103 @@ describe('CioAutocomplete Client-Side Rendering', () => {
|
126 | 126 |
|
127 | 127 | expect(console.error).not.toHaveBeenCalled();
|
128 | 128 | });
|
| 129 | + describe('Custom UI variant', () => { |
| 130 | + afterEach(() => { |
| 131 | + window.sessionStorage.removeItem('_constructorio_custom_autosuggest_ui'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('Renders the default CioAutocomplete when no custom UI variant is set', async () => { |
| 135 | + render(<CioAutocomplete apiKey={DEMO_API_KEY} onSubmit={() => {}} />); |
| 136 | + |
| 137 | + const searchInput = screen.getByRole('combobox'); |
| 138 | + |
| 139 | + fireEvent.change(searchInput, { target: { value: 'pants' } }); |
| 140 | + |
| 141 | + const options = (await screen.findAllByRole('option', undefined, { timeout: 5000 })).filter( |
| 142 | + (elem) => elem.getAttribute('data-cnstrc-item-section') === 'Search Suggestions' |
| 143 | + ); |
| 144 | + |
| 145 | + options.forEach((option) => { |
| 146 | + const suggestionCount = option.querySelector('.cio-suggestion-count'); |
| 147 | + expect(suggestionCount).not.toBeInTheDocument(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('Takes custom UI variant from session storage and applies features based on the variant', async () => { |
| 152 | + window.sessionStorage.setItem( |
| 153 | + '_constructorio_custom_autosuggest_ui', |
| 154 | + 'custom_autosuggest_ui_result_count' |
| 155 | + ); |
| 156 | + |
| 157 | + const props = { |
| 158 | + apiKey: DEMO_API_KEY, |
| 159 | + onSubmit: () => {}, |
| 160 | + sections: [ |
| 161 | + { |
| 162 | + indexSectionName: 'Search Suggestions', |
| 163 | + numResults: 8, |
| 164 | + }, |
| 165 | + { |
| 166 | + indexSectionName: 'Products', |
| 167 | + numResults: 6, |
| 168 | + }, |
| 169 | + ], |
| 170 | + }; |
| 171 | + |
| 172 | + render(<CioAutocomplete {...props} />); |
| 173 | + |
| 174 | + const searchInput = screen.getByRole('combobox'); |
| 175 | + |
| 176 | + fireEvent.change(searchInput, { target: { value: 'pants' } }); |
| 177 | + |
| 178 | + const options = (await screen.findAllByRole('option')).filter( |
| 179 | + (elem) => elem.getAttribute('data-cnstrc-item-section') === 'Search Suggestions' |
| 180 | + ); |
| 181 | + |
| 182 | + options.forEach((option) => { |
| 183 | + const suggestionCount = option.querySelector('.cio-suggestion-count'); |
| 184 | + expect(suggestionCount).toBeInTheDocument(); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + it('Applies features sent in advancedParameters even if a custom UI variant is set that affects that feature', async () => { |
| 189 | + window.sessionStorage.setItem( |
| 190 | + '_constructorio_custom_autosuggest_ui', |
| 191 | + 'custom_autosuggest_ui_result_count' |
| 192 | + ); |
| 193 | + |
| 194 | + const props = { |
| 195 | + apiKey: DEMO_API_KEY, |
| 196 | + onSubmit: () => {}, |
| 197 | + sections: [ |
| 198 | + { |
| 199 | + indexSectionName: 'Search Suggestions', |
| 200 | + numResults: 8, |
| 201 | + }, |
| 202 | + { |
| 203 | + indexSectionName: 'Products', |
| 204 | + numResults: 6, |
| 205 | + }, |
| 206 | + ], |
| 207 | + advancedParameters: { |
| 208 | + displaySearchSuggestionResultCounts: false, |
| 209 | + }, |
| 210 | + }; |
| 211 | + |
| 212 | + render(<CioAutocomplete {...props} />); |
| 213 | + |
| 214 | + const searchInput = screen.getByRole('combobox'); |
| 215 | + |
| 216 | + fireEvent.change(searchInput, { target: { value: 'pants' } }); |
| 217 | + |
| 218 | + const options = (await screen.findAllByRole('option')).filter( |
| 219 | + (elem) => elem.getAttribute('data-cnstrc-item-section') === 'Search Suggestions' |
| 220 | + ); |
| 221 | + |
| 222 | + options.forEach((option) => { |
| 223 | + const suggestionCount = option.querySelector('.cio-suggestion-count'); |
| 224 | + expect(suggestionCount).not.toBeInTheDocument(); |
| 225 | + }); |
| 226 | + }); |
| 227 | + }); |
129 | 228 | });
|
0 commit comments