|
| 1 | +import React from 'react'; |
| 2 | +import { DataViewTextFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTextFilter'; |
| 3 | + |
| 4 | +describe('DataViewTextFilter', () => { |
| 5 | + const onChangeMock = cy.stub(); |
| 6 | + |
| 7 | + const defaultProps = { |
| 8 | + filterId: 'name', |
| 9 | + title: 'Name', |
| 10 | + value: '', |
| 11 | + onChange: onChangeMock, |
| 12 | + ouiaId: 'DataViewTextFilter', |
| 13 | + placeholder: 'Filter by name' |
| 14 | + }; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + cy.mount(<DataViewTextFilter {...defaultProps} />); |
| 18 | + }); |
| 19 | + |
| 20 | + it('renders DataViewTextFilter with correct initial values', () => { |
| 21 | + // Checks that the main components are rendered |
| 22 | + cy.get('[data-ouia-component-id="DataViewTextFilter"]').should('exist'); |
| 23 | + cy.get('[data-ouia-component-id="DataViewTextFilter-input"]').should('have.attr', 'placeholder', 'Filter by name'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('accepts input and calls onChange with trimmed input', () => { |
| 27 | + // Type into the search input field |
| 28 | + cy.get('[data-ouia-component-id="DataViewTextFilter-input"]').type(' Repository One '); |
| 29 | + |
| 30 | + // Verifies that onChange is called with trimmed input value |
| 31 | + cy.wrap(onChangeMock).should('have.been.calledWith', Cypress.sinon.match.any, 'Repository One'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('displays a chip when value is present and clears it on delete', () => { |
| 35 | + // Set initial value to display the chip |
| 36 | + cy.mount(<DataViewTextFilter {...defaultProps} value="Repository One" />); |
| 37 | + |
| 38 | + // Checks that the chip displays with the correct value |
| 39 | + cy.get('[data-ouia-component-id="DataViewTextFilter"]').contains('Repository One').should('exist'); |
| 40 | + |
| 41 | + // Clears the chip and checks if onChange is called with an empty value |
| 42 | + cy.get('[data-ouia-component-id="DataViewTextFilter"] button').click(); |
| 43 | + cy.wrap(onChangeMock).should('have.been.calledWith', Cypress.sinon.match.any, ''); |
| 44 | + }); |
| 45 | + |
| 46 | + it('clears input when the clear button is clicked', () => { |
| 47 | + // Type a value and clear it |
| 48 | + cy.get('[data-ouia-component-id="DataViewTextFilter-input"]').type('Repository Two'); |
| 49 | + cy.get('[data-ouia-component-id="DataViewTextFilter-input"] button.pf-c-button').click(); |
| 50 | + |
| 51 | + // Verifies that the input field is cleared and onChange is called with an empty value |
| 52 | + cy.wrap(onChangeMock).should('have.been.calledWith', Cypress.sinon.match.any, ''); |
| 53 | + cy.get('[data-ouia-component-id="DataViewTextFilter-input"]').should('have.value', ''); |
| 54 | + }); |
| 55 | + |
| 56 | + it('hides or shows the toolbar item based on showToolbarItem prop', () => { |
| 57 | + // Rerender component with showToolbarItem set to false |
| 58 | + cy.mount(<DataViewTextFilter {...defaultProps} showToolbarItem={false} />); |
| 59 | + |
| 60 | + // Verifies that the filter component is not visible in the toolbar |
| 61 | + cy.get('[data-ouia-component-id="DataViewTextFilter"]').should('not.exist'); |
| 62 | + |
| 63 | + // Rerender with showToolbarItem set to true |
| 64 | + cy.mount(<DataViewTextFilter {...defaultProps} showToolbarItem />); |
| 65 | + |
| 66 | + // Verifies that the filter component is now visible |
| 67 | + cy.get('[data-ouia-component-id="DataViewTextFilter"]').should('exist'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments