-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/2130/setContentTypeConfig-for-page-b…
…uilder
- Loading branch information
Showing
26 changed files
with
993 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/peregrine/lib/talons/CheckoutPage/ItemsReview/__fixtures__/cartItems.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
export default { | ||
cart: { | ||
id: 'GXtkt675mPd3gYuvhWLd5iw5ekVoDj1b', | ||
total_quantity: 7, | ||
items: [ | ||
{ | ||
id: '29568', | ||
product: { | ||
id: 1093, | ||
name: 'Jillian Top', | ||
thumbnail: { | ||
url: | ||
'https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/d3ba9f7bcd3b0724e976dc5144b29c7d/v/t/vt12-kh_main_2.jpg', | ||
__typename: 'ProductImage' | ||
}, | ||
__typename: 'ConfigurableProduct' | ||
}, | ||
quantity: 3, | ||
configurable_options: [ | ||
{ | ||
id: 179, | ||
option_label: 'Fashion Color', | ||
value_id: 18, | ||
value_label: 'Peach', | ||
__typename: 'SelectedConfigurableOption' | ||
}, | ||
{ | ||
id: 182, | ||
option_label: 'Fashion Size', | ||
value_id: 27, | ||
value_label: 'M', | ||
__typename: 'SelectedConfigurableOption' | ||
} | ||
], | ||
__typename: 'ConfigurableCartItem' | ||
}, | ||
{ | ||
id: '29570', | ||
product: { | ||
id: 1115, | ||
name: 'Juno Sweater', | ||
thumbnail: { | ||
url: | ||
'https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/d3ba9f7bcd3b0724e976dc5144b29c7d/v/s/vsw02-pe_main_2.jpg', | ||
__typename: 'ProductImage' | ||
}, | ||
__typename: 'ConfigurableProduct' | ||
}, | ||
quantity: 1, | ||
configurable_options: [ | ||
{ | ||
id: 179, | ||
option_label: 'Fashion Color', | ||
value_id: 21, | ||
value_label: 'Rain', | ||
__typename: 'SelectedConfigurableOption' | ||
}, | ||
{ | ||
id: 182, | ||
option_label: 'Fashion Size', | ||
value_id: 29, | ||
value_label: 'XS', | ||
__typename: 'SelectedConfigurableOption' | ||
} | ||
], | ||
__typename: 'ConfigurableCartItem' | ||
}, | ||
{ | ||
id: '29572', | ||
product: { | ||
id: 1152, | ||
name: 'Angelina Tank Dress', | ||
thumbnail: { | ||
url: | ||
'https://master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud/media/catalog/product/cache/d3ba9f7bcd3b0724e976dc5144b29c7d/v/d/vd01-ll_main_2.jpg', | ||
__typename: 'ProductImage' | ||
}, | ||
__typename: 'ConfigurableProduct' | ||
}, | ||
quantity: 3, | ||
configurable_options: [ | ||
{ | ||
id: 179, | ||
option_label: 'Fashion Color', | ||
value_id: 20, | ||
value_label: 'Lilac', | ||
__typename: 'SelectedConfigurableOption' | ||
}, | ||
{ | ||
id: 182, | ||
option_label: 'Fashion Size', | ||
value_id: 26, | ||
value_label: 'L', | ||
__typename: 'SelectedConfigurableOption' | ||
} | ||
], | ||
__typename: 'ConfigurableCartItem' | ||
} | ||
], | ||
__typename: 'Cart' | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
...e/lib/talons/CheckoutPage/ItemsReview/__tests__/__snapshots__/useItemsReview.spec.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`returns correct shape 1`] = ` | ||
Object { | ||
"hasErrors": false, | ||
"isLoading": true, | ||
"items": Array [], | ||
"setShowAllItems": [Function], | ||
"showAllItems": false, | ||
"totalQuantity": 0, | ||
} | ||
`; |
98 changes: 98 additions & 0 deletions
98
packages/peregrine/lib/talons/CheckoutPage/ItemsReview/__tests__/useItemsReview.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from 'react'; | ||
import { useLazyQuery } from '@apollo/react-hooks'; | ||
|
||
import createTestInstance from '../../../../../lib/util/createTestInstance'; | ||
import { useItemsReview } from '../useItemsReview'; | ||
|
||
import cartItems from '../__fixtures__/cartItems'; | ||
|
||
jest.mock('@apollo/react-hooks', () => ({ | ||
useLazyQuery: jest.fn().mockReturnValue([ | ||
() => {}, | ||
{ | ||
data: null, | ||
error: null, | ||
loading: true | ||
} | ||
]) | ||
})); | ||
|
||
jest.mock('../../../../context/cart', () => { | ||
const state = { | ||
cartId: 'cart123' | ||
}; | ||
const api = {}; | ||
const useCartContext = jest.fn(() => [state, api]); | ||
|
||
return { useCartContext }; | ||
}); | ||
|
||
const Component = props => { | ||
const talonProps = useItemsReview(props); | ||
|
||
return <i talonProps={talonProps} />; | ||
}; | ||
|
||
test('returns correct shape', () => { | ||
const tree = createTestInstance( | ||
<Component queries={{ getItemsInCart: jest.fn() }} /> | ||
); | ||
const { root } = tree; | ||
const { talonProps } = root.findByType('i').props; | ||
|
||
expect(talonProps).toMatchSnapshot(); | ||
}); | ||
|
||
test('Should return total quantity from gql query', () => { | ||
useLazyQuery.mockReturnValueOnce([ | ||
() => {}, | ||
{ | ||
data: cartItems, | ||
error: null, | ||
loading: false | ||
} | ||
]); | ||
const tree = createTestInstance( | ||
<Component queries={{ getItemsInCart: jest.fn() }} /> | ||
); | ||
const { root } = tree; | ||
const { talonProps } = root.findByType('i').props; | ||
|
||
expect(talonProps.totalQuantity).toBe(cartItems.cart.total_quantity); | ||
}); | ||
|
||
test('Should return 0 for total quantity if gql does not return total_quality', () => { | ||
const newCartItems = { | ||
...cartItems | ||
}; | ||
newCartItems.cart.total_quantity = null; | ||
useLazyQuery.mockReturnValueOnce([ | ||
() => {}, | ||
{ | ||
data: newCartItems, | ||
error: null, | ||
loading: false | ||
} | ||
]); | ||
const tree = createTestInstance( | ||
<Component queries={{ getItemsInCart: jest.fn() }} /> | ||
); | ||
const { root } = tree; | ||
const { talonProps } = root.findByType('i').props; | ||
|
||
expect(talonProps.totalQuantity).toBe(0); | ||
}); | ||
|
||
test('hasErrors in return props should be set to true if gql throws any errors', () => { | ||
useLazyQuery.mockReturnValueOnce([ | ||
() => {}, | ||
{ data: null, loading: false, error: 'some error' } | ||
]); | ||
const tree = createTestInstance( | ||
<Component queries={{ getItemsInCart: jest.fn() }} /> | ||
); | ||
const { root } = tree; | ||
const { talonProps } = root.findByType('i').props; | ||
|
||
expect(talonProps.hasErrors).toBeTruthy(); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/peregrine/lib/talons/CheckoutPage/ItemsReview/useItemsReview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useEffect, useState, useCallback } from 'react'; | ||
import { useLazyQuery } from '@apollo/react-hooks'; | ||
|
||
import { useCartContext } from '../../../context/cart'; | ||
|
||
export const useItemsReview = props => { | ||
const [showAllItems, setShowAllItems] = useState(false); | ||
|
||
const { | ||
queries: { getItemsInCart } | ||
} = props; | ||
|
||
const [{ cartId }] = useCartContext(); | ||
|
||
const [fetchItemsInCart, { data, error, loading }] = useLazyQuery( | ||
getItemsInCart, | ||
{ | ||
fetchPolicy: 'cache-and-network' | ||
} | ||
); | ||
|
||
const setShowAllItemsFlag = useCallback(() => setShowAllItems(true), [ | ||
setShowAllItems | ||
]); | ||
|
||
useEffect(() => { | ||
if (cartId) { | ||
fetchItemsInCart({ | ||
variables: { | ||
cartId | ||
} | ||
}); | ||
} | ||
}, [cartId, fetchItemsInCart]); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
console.error(error); | ||
} | ||
}, [error]); | ||
|
||
useEffect(() => { | ||
/** | ||
* If there are 2 or less than 2 items in cart | ||
* set show all items to `true`. | ||
*/ | ||
if (data && data.cart && data.cart.items.length <= 2) { | ||
setShowAllItems(true); | ||
} | ||
}, [data]); | ||
|
||
const items = data ? data.cart.items : []; | ||
|
||
const totalQuantity = data ? +data.cart.total_quantity : 0; | ||
|
||
return { | ||
isLoading: !!loading, | ||
items, | ||
hasErrors: !!error, | ||
totalQuantity, | ||
showAllItems, | ||
setShowAllItems: setShowAllItemsFlag | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.