// Component tests for usa-combo-box import './index.ts'; describe('USA Combo Box Component Tests', () => { const stateOptions = [ { value: 'AL', label: 'Alabama' }, { value: 'AK', label: 'Alaska' }, { value: 'AZ', label: 'Arizona' }, { value: 'AR', label: 'Arkansas' }, { value: 'CA', label: 'California' }, { value: 'CO', label: 'Colorado' }, { value: 'CT', label: 'Connecticut' }, { value: 'FL', label: 'Florida' }, { value: 'GA', label: 'Georgia' }, { value: 'NY', label: 'New York' }, { value: 'TX', label: 'Texas' }, ]; it('should render combo box with default properties', () => { cy.mount(``); cy.get('usa-combo-box').should('exist'); cy.get('.usa-combo-box').should('exist'); cy.get('.usa-combo-box__input').should('exist'); cy.get('.usa-combo-box__toggle-list').should('exist'); }); it('should render options when provided', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Click toggle to open dropdown cy.get('.usa-combo-box__toggle-list').click(); cy.get('.usa-combo-box__list').should('be.visible'); cy.get('.usa-combo-box__list-option').should('have.length', stateOptions.length); cy.get('.usa-combo-box__list-option').first().should('contain.text', 'Alabama'); }); it('should handle text input and filtering', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Type to filter options cy.get('.usa-combo-box__input').type('cal'); cy.get('.usa-combo-box__list').should('be.visible'); cy.get('.usa-combo-box__list-option').should('have.length', 1); cy.get('.usa-combo-box__list-option').should('contain.text', 'California'); // Clear and try different filter cy.get('.usa-combo-box__input').clear().type('ar'); cy.get('.usa-combo-box__list-option').should('have.length', 2); // Arkansas, Arizona }); it('should handle option selection via click', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Open dropdown and select option cy.get('.usa-combo-box__toggle-list').click(); cy.get('.usa-combo-box__list-option').contains('Florida').click(); // Should close dropdown and set value cy.get('.usa-combo-box__input').should('have.value', 'Florida'); cy.get('.usa-combo-box__list').should('not.be.visible'); }); it('should handle keyboard navigation', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Focus input and open with arrow down cy.get('.usa-combo-box__input').focus().type('{downarrow}'); cy.get('.usa-combo-box__list').should('be.visible'); // First option should be focused cy.get('.usa-combo-box__list-option--focused').should('contain.text', 'Alabama'); // Arrow down to next option cy.get('.usa-combo-box__input').type('{downarrow}'); cy.get('.usa-combo-box__list-option--focused').should('contain.text', 'Alaska'); // Arrow up to previous option cy.get('.usa-combo-box__input').type('{uparrow}'); cy.get('.usa-combo-box__list-option--focused').should('contain.text', 'Alabama'); // Enter to select cy.get('.usa-combo-box__input').type('{enter}'); cy.get('.usa-combo-box__input').should('have.value', 'Alabama'); cy.get('.usa-combo-box__list').should('not.be.visible'); }); it('should handle escape key to close dropdown', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Open dropdown cy.get('.usa-combo-box__toggle-list').click(); cy.get('.usa-combo-box__list').should('be.visible'); // Escape to close cy.get('.usa-combo-box__input').type('{esc}'); cy.get('.usa-combo-box__list').should('not.be.visible'); }); it('should emit change events', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; const changeSpy = cy.stub(); combo.addEventListener('change', changeSpy); // Select an option cy.get('.usa-combo-box__toggle-list').click(); cy.get('.usa-combo-box__list-option').contains('Texas').click(); cy.then(() => { expect(changeSpy).to.have.been.called; }); }); }); it('should handle disabled state', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); cy.get('.usa-combo-box__input').should('be.disabled'); cy.get('.usa-combo-box__toggle-list').should('be.disabled'); cy.get('.usa-combo-box').should('have.class', 'usa-combo-box--disabled'); }); it('should handle required state', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); cy.get('.usa-combo-box__input').should('have.attr', 'required'); cy.get('.usa-combo-box__input').should('have.attr', 'aria-required', 'true'); }); it('should handle error state', () => { cy.mount(` `); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); cy.get('.usa-combo-box__input').should('have.attr', 'aria-invalid', 'true'); cy.get('.usa-combo-box').should('have.class', 'usa-combo-box--error'); cy.get('.usa-error-message').should('contain.text', 'Please select a valid state'); }); it('should handle custom placeholder', () => { cy.mount(` `); cy.get('.usa-combo-box__input').should( 'have.attr', 'placeholder', 'Select or type a state name' ); }); it('should handle no results state', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Type something that doesn't match any options cy.get('.usa-combo-box__input').type('xyz'); cy.get('.usa-combo-box__list').should('be.visible'); cy.get('.usa-combo-box__status').should('contain.text', 'No results found'); cy.get('.usa-combo-box__list-option').should('have.length', 0); }); it('should handle clear functionality', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; combo.value = 'California'; }); // Should show clear button when there's a value cy.get('.usa-combo-box__clear-input').should('be.visible'); // Click clear button cy.get('.usa-combo-box__clear-input').click(); cy.get('.usa-combo-box__input').should('have.value', ''); cy.get('.usa-combo-box__clear-input').should('not.be.visible'); }); it('should handle form integration', () => { cy.mount(`
`); cy.window().then((win) => { const combo = win.document.getElementById('state-combo') as any; combo.options = stateOptions; const form = win.document.getElementById('test-form') as HTMLFormElement; const submitSpy = cy.stub(); form.addEventListener('submit', (e) => { e.preventDefault(); const formData = new FormData(form); submitSpy(formData.get('user-state')); }); // Select an option and submit cy.get('.usa-combo-box__toggle-list').click(); cy.get('.usa-combo-box__list-option').contains('New York').click(); cy.get('button[type="submit"]').click(); cy.then(() => { expect(submitSpy).to.have.been.calledWith('NY'); }); }); }); it('should handle custom filtering', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.options = stateOptions; }); // Type 'a' - should only show options starting with 'a' cy.get('.usa-combo-box__input').type('a'); cy.get('.usa-combo-box__list-option').should('have.length', 3); // Alabama, Alaska, Arizona cy.get('.usa-combo-box__list-option').should('not.contain.text', 'California'); // Contains 'a' but doesn't start with it }); it('should handle option groups', () => {}); // Open dropdown cy.get('.usa-combo-box__toggle-list').click(); // Should show group headers cy.get('.usa-combo-box__list-option-group').should('have.length', 2); cy.get('.usa-combo-box__list-option-group').first().should('contain.text', 'West Coast'); cy.get('.usa-combo-box__list-option-group').last().should('contain.text', 'East Coast'); }); it('should handle async option loading', () => { cy.mount(``); cy.window().then((win) => { const combo = win.document.getElementById('test-combo') as any; combo.addEventListener('usa-combo-box:search', (e: CustomEvent) => { // Simulate async loading setTimeout(() => { const query = e.detail.query.toLowerCase(); const filtered = stateOptions.filter((option) => option.label.toLowerCase().includes(query) ); combo.options = filtered; }, 100); }); }); // Type minimum characters to trigger search cy.get('.usa-combo-box__input').type('ca'); cy.get('.usa-combo-box__status').should('contain.text', 'Loading...'); // Wait for async results cy.wait(200); cy.get('.usa-combo-box__list-option').should('have.length', 1); cy.get('.usa-combo-box__list-option').should('contain.text', 'California'); }); // NOTE: Custom option rendering is not supported // USWDS combo box manages all option rendering via standard