Skip to content

Commit

Permalink
CCS-4128: product listing unit tests (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
grantsydney authored Feb 17, 2021
1 parent 0280b3d commit 471c99c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
46 changes: 15 additions & 31 deletions pantheon-bundle/frontend/src/app/productListing.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react"
import "@app/fetchMock"
import { mount, shallow } from "enzyme"
import { render, fireEvent } from '@testing-library/react'
import { DataList, DataListItem, DataListItemCells, DataListItemRow, FormGroup, TextInput } from "@patternfly/react-core"
import ProductListing from "./productListing"
import { ProductContext, IProduct } from "./contexts/ProductContext"
Expand Down Expand Up @@ -79,36 +80,19 @@ describe("ProductListing tests", () => {


it("should update input and list of products on text change", () => {
const container = mount(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
const searchInput = container.find('#search.pf-c-form-control')
searchInput.simulate('change', {
target: {
value: 'product',
},
});
setTimeout(() => {
expect(container.prop('input')).toEqual(
'product',
);
expect(container.state('input')).toEqual(
'product',
);
expect(container.state('filteredProducts')).toHaveLength(1)
expect(container.state('filteredProducts')).toMatchObject(allProducts[0])
}, 2000)
searchInput.simulate('change', {
target: {
value: '',
},
});
setTimeout(() => {
expect(container.prop('input')).toEqual(
'',
);
expect(container.state('input')).toEqual(
'',
);
expect(container.state('filteredProducts')).toHaveLength(2)
}, 2000)
const { getByPlaceholderText } = render(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
let productInput = getByPlaceholderText('Type product name to search') as HTMLInputElement
expect(productInput.value).toBe('')
fireEvent.change(productInput, { target: { value: 'test_name' } })
expect(productInput.value).toBe('test_name')
})

it("should test toggling caret to display product details dropdown option", () => {
const { getByTestId, getByText } = render(<ProductContext.Provider value={allProducts}><ProductListing /></ProductContext.Provider>)
const caret = getByTestId('product-5df8d913-79b9-42fd-a17b-ffc917add446-button')
fireEvent.click(caret)
expect(getByText('Product Details')).toBeTruthy()
const productDetailButton = getByText('Product Details')
fireEvent.click(productDetailButton)
})
})
2 changes: 1 addition & 1 deletion pantheon-bundle/frontend/src/app/productListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default function ProductListing(props: any) {
menuItems={[
<OptionsMenuItem onSelect={() => onSelect(product["jcr:uuid"])} key="dropdown">Product Details</OptionsMenuItem>]}
isOpen={product.isOpen}
toggle={<OptionsMenuToggle onToggle={() => onToggle(product["jcr:uuid"])} toggleTemplate={<CaretDownIcon aria-hidden="true" />} aria-label="Sort by" hideCaret={true} id={`product-${product["jcr:uuid"]}-button`} />} />
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`} />} />
</DataListAction>
</DataListCell>
]}
Expand Down

0 comments on commit 471c99c

Please sign in to comment.