diff --git a/src/components/ui/Select/tests/Select.lazyMount.test.tsx b/src/components/ui/Select/tests/Select.lazyMount.test.tsx new file mode 100644 index 000000000..b7bbd70b6 --- /dev/null +++ b/src/components/ui/Select/tests/Select.lazyMount.test.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import Select from '../Select'; +import Theme from '~/components/ui/Theme/Theme'; + +const mockMatchMedia = () => { + if ('matchMedia' in window && typeof window.matchMedia === 'function') return; + Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation(() => ({ + matches: false, + addEventListener: jest.fn(), + removeEventListener: jest.fn() + })) + }); +}; + +describe('Select lazy mount', () => { + beforeEach(() => mockMatchMedia()); + + test('does not mount listbox content until opened', () => { + render( + + + + + + Apple + + + + + ); + + expect(screen.queryByTestId('select-content')).not.toBeInTheDocument(); + }); + + test('mounts content after trigger opens the listbox', async() => { + const user = userEvent.setup(); + + render( + + + + + + Apple + + + + + ); + + await user.click(screen.getByRole('combobox')); + expect(screen.getByText('Apple')).toBeInTheDocument(); + }); +});