Skip to content

Commit 471c99c

Browse files
authored
CCS-4128: product listing unit tests (#514)
1 parent 0280b3d commit 471c99c

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

pantheon-bundle/frontend/src/app/productListing.test.tsx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react"
22
import "@app/fetchMock"
33
import { mount, shallow } from "enzyme"
4+
import { render, fireEvent } from '@testing-library/react'
45
import { DataList, DataListItem, DataListItemCells, DataListItemRow, FormGroup, TextInput } from "@patternfly/react-core"
56
import ProductListing from "./productListing"
67
import { ProductContext, IProduct } from "./contexts/ProductContext"
@@ -79,36 +80,19 @@ describe("ProductListing tests", () => {
7980

8081

8182
it("should update input and list of products on text change", () => {
82-
const container = mount(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
83-
const searchInput = container.find('#search.pf-c-form-control')
84-
searchInput.simulate('change', {
85-
target: {
86-
value: 'product',
87-
},
88-
});
89-
setTimeout(() => {
90-
expect(container.prop('input')).toEqual(
91-
'product',
92-
);
93-
expect(container.state('input')).toEqual(
94-
'product',
95-
);
96-
expect(container.state('filteredProducts')).toHaveLength(1)
97-
expect(container.state('filteredProducts')).toMatchObject(allProducts[0])
98-
}, 2000)
99-
searchInput.simulate('change', {
100-
target: {
101-
value: '',
102-
},
103-
});
104-
setTimeout(() => {
105-
expect(container.prop('input')).toEqual(
106-
'',
107-
);
108-
expect(container.state('input')).toEqual(
109-
'',
110-
);
111-
expect(container.state('filteredProducts')).toHaveLength(2)
112-
}, 2000)
83+
const { getByPlaceholderText } = render(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
84+
let productInput = getByPlaceholderText('Type product name to search') as HTMLInputElement
85+
expect(productInput.value).toBe('')
86+
fireEvent.change(productInput, { target: { value: 'test_name' } })
87+
expect(productInput.value).toBe('test_name')
88+
})
89+
90+
it("should test toggling caret to display product details dropdown option", () => {
91+
const { getByTestId, getByText } = render(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
92+
const caret = getByTestId('product-5df8d913-79b9-42fd-a17b-ffc917add446-button')
93+
fireEvent.click(caret)
94+
expect(getByText('Product Details')).toBeTruthy()
95+
const productDetailButton = getByText('Product Details')
96+
fireEvent.click(productDetailButton)
11397
})
11498
})

pantheon-bundle/frontend/src/app/productListing.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default function ProductListing(props: any) {
111111
menuItems={[
112112
<OptionsMenuItem onSelect={() => onSelect(product["jcr:uuid"])} key="dropdown">Product Details</OptionsMenuItem>]}
113113
isOpen={product.isOpen}
114-
toggle={<OptionsMenuToggle onToggle={() => onToggle(product["jcr:uuid"])} toggleTemplate={<CaretDownIcon aria-hidden="true" />} aria-label="Sort by" hideCaret={true} id={`product-${product["jcr:uuid"]}-button`} />} />
114+
toggle={<OptionsMenuToggle onToggle={() => onToggle(product["jcr:uuid"])} toggleTemplate={<CaretDownIcon aria-hidden="true" />} aria-label="Sort by" hideCaret={true} id={`product-${product["jcr:uuid"]}-button`} data-testid={`product-${product["jcr:uuid"]}-button`} />} />
115115
</DataListAction>
116116
</DataListCell>
117117
]}

0 commit comments

Comments
 (0)