@graphcommerce/magento-wishlist 11.0.0-canary.50 → 11.0.0-canary.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @graphcommerce/magento-wishlist
2
2
 
3
+ ## 11.0.0-canary.54
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2711](https://github.com/graphcommerce-org/graphcommerce/pull/2711) [`9148f28`](https://github.com/graphcommerce-org/graphcommerce/commit/9148f28b95917a99a05d663bef8658456a04c14d) - `useWishlistItems` takes `currentPage` and `pageSize` and returns `count`, the number of items in the wishlist. The wishlist button and the product page load up to 999 items. Magento returns 20 items by default, so with more than 20 items the button did not find the other products. ([@bramvanderholst](https://github.com/bramvanderholst))
8
+
9
+ ## 11.0.0-canary.53
10
+
11
+ ## 11.0.0-canary.52
12
+
13
+ ## 11.0.0-canary.51
14
+
3
15
  ## 11.0.0-canary.50
4
16
 
5
17
  ### Patch Changes
@@ -33,7 +33,7 @@ export function useAddProductToWishlistAction(
33
33
 
34
34
  const add = useAddProductsToWishlist()
35
35
  const remove = useRemoveProductsFromWishlist()
36
- const wishlist = useWishlistItems()
36
+ const wishlist = useWishlistItems(1, 999)
37
37
  const { loggedIn } = useCustomerSession()
38
38
  const [showSuccess, setShowSuccess] = useState<boolean>(false)
39
39
 
@@ -0,0 +1,29 @@
1
+ // @vitest-environment happy-dom
2
+ import { act, renderHook } from '@testing-library/react'
3
+ import { describe, expect, it, vi } from 'vitest'
4
+ import { useAddProductsToWishlist } from './useAddProductsToWishlist'
5
+
6
+ const mutate = vi.hoisted(() => vi.fn(async (_options: unknown) => ({})))
7
+
8
+ vi.mock('@graphcommerce/graphql', () => ({ useApolloClient: () => ({ mutate }) }))
9
+ vi.mock('@graphcommerce/magento-customer', () => ({
10
+ useCustomerSession: () => ({ loggedIn: true }),
11
+ }))
12
+ vi.mock('@mui/material', () => ({ useEventCallback: (callback: unknown) => callback }))
13
+ vi.mock('../useWishlistitems', () => ({
14
+ UseWishlistCustomerDocument: 'UseWishlistCustomerDocument',
15
+ UseWishlistGuestDocument: 'UseWishlistGuestDocument',
16
+ }))
17
+
18
+ describe('useAddProductsToWishlist', () => {
19
+ // Each page of the wishlist has its own cache entry, which the mutation result does not update.
20
+ it('refetches the pages of the wishlist of a customer', async () => {
21
+ const { result } = renderHook(() => useAddProductsToWishlist())
22
+
23
+ await act(() => result.current([{ sku: 'sku', quantity: 1 }]))
24
+
25
+ expect(mutate).toHaveBeenCalledWith(
26
+ expect.objectContaining({ refetchQueries: ['UseWishlistCustomerDocument'] }),
27
+ )
28
+ })
29
+ })
@@ -2,7 +2,7 @@ import { useApolloClient } from '@graphcommerce/graphql'
2
2
  import { useCustomerSession } from '@graphcommerce/magento-customer'
3
3
  import { useEventCallback } from '@mui/material'
4
4
  import type { WishlistItemFragment } from '../../queries/WishlistItem.gql'
5
- import { UseWishlistGuestDocument } from '../useWishlistitems'
5
+ import { UseWishlistCustomerDocument, UseWishlistGuestDocument } from '../useWishlistitems'
6
6
  import type { AddProductToWishlistMutationVariables } from './AddProductToWishlist.gql'
7
7
  import { AddProductToWishlistDocument } from './AddProductToWishlist.gql'
8
8
 
@@ -25,6 +25,8 @@ export function useAddProductsToWishlist() {
25
25
  await client.mutate({
26
26
  mutation: AddProductToWishlistDocument,
27
27
  variables: { input },
28
+ refetchQueries: [UseWishlistCustomerDocument],
29
+ awaitRefetchQueries: true,
28
30
  })
29
31
  return
30
32
  }
@@ -0,0 +1,30 @@
1
+ // @vitest-environment happy-dom
2
+ import { act, renderHook } from '@testing-library/react'
3
+ import { describe, expect, it, vi } from 'vitest'
4
+ import { useRemoveProductsFromWishlist } from './useRemoveProductsFromWishlist'
5
+
6
+ const mutate = vi.hoisted(() => vi.fn(async (_options: unknown) => ({})))
7
+
8
+ vi.mock('@graphcommerce/graphql', () => ({ useApolloClient: () => ({ mutate }) }))
9
+ vi.mock('@graphcommerce/magento-customer', () => ({
10
+ useCustomerSession: () => ({ loggedIn: true }),
11
+ }))
12
+ vi.mock('@graphcommerce/next-ui', () => ({ filterNonNullableKeys: vi.fn(), nonNullable: vi.fn() }))
13
+ vi.mock('@mui/material', () => ({ useEventCallback: (callback: unknown) => callback }))
14
+ vi.mock('../useWishlistitems', () => ({
15
+ UseWishlistCustomerDocument: 'UseWishlistCustomerDocument',
16
+ UseWishlistGuestDocument: 'UseWishlistGuestDocument',
17
+ }))
18
+
19
+ describe('useRemoveProductsFromWishlist', () => {
20
+ // Each page of the wishlist has its own cache entry, which the mutation result does not update.
21
+ it('refetches the pages of the wishlist of a customer', async () => {
22
+ const { result } = renderHook(() => useRemoveProductsFromWishlist())
23
+
24
+ await act(() => result.current(['1']))
25
+
26
+ expect(mutate).toHaveBeenCalledWith(
27
+ expect.objectContaining({ refetchQueries: ['UseWishlistCustomerDocument'] }),
28
+ )
29
+ })
30
+ })
@@ -2,7 +2,7 @@ import { useApolloClient } from '@graphcommerce/graphql'
2
2
  import { useCustomerSession } from '@graphcommerce/magento-customer'
3
3
  import { filterNonNullableKeys, nonNullable } from '@graphcommerce/next-ui'
4
4
  import { useEventCallback } from '@mui/material'
5
- import { UseWishlistGuestDocument } from '../useWishlistitems'
5
+ import { UseWishlistCustomerDocument, UseWishlistGuestDocument } from '../useWishlistitems'
6
6
  import type { UseRemoveProductsFromWishlistMutationVariables } from './UseRemoveProductsFromWishlist.gql'
7
7
  import { UseRemoveProductsFromWishlistDocument } from './UseRemoveProductsFromWishlist.gql'
8
8
 
@@ -17,6 +17,8 @@ export function useRemoveProductsFromWishlist() {
17
17
  await client.mutate({
18
18
  mutation: UseRemoveProductsFromWishlistDocument,
19
19
  variables: { wishlistItemIds },
20
+ refetchQueries: [UseWishlistCustomerDocument],
21
+ awaitRefetchQueries: true,
20
22
  })
21
23
  return
22
24
  }
@@ -1,7 +1,7 @@
1
- query UseWishlistCustomer {
1
+ query UseWishlistCustomer($currentPage: Int = 1, $pageSize: Int = 20) {
2
2
  customer {
3
3
  wishlists {
4
- ...Wishlist
4
+ ...WishlistPagination
5
5
  }
6
6
  }
7
7
  }
@@ -0,0 +1,42 @@
1
+ // @vitest-environment happy-dom
2
+ import { renderHook } from '@testing-library/react'
3
+ import { describe, expect, it, vi } from 'vitest'
4
+ import { UseWishlistCustomerDocument } from './UseWishlistCustomer.gql'
5
+ import { useWishlistItems } from './useWishlistItems'
6
+
7
+ const useCustomerQuery = vi.hoisted(() =>
8
+ vi.fn((_document: unknown, _options: unknown) => ({
9
+ data: {
10
+ customer: { wishlists: [{ items_count: 25, items_v2: { items: [{ id: '1' }] } }] },
11
+ },
12
+ })),
13
+ )
14
+
15
+ vi.mock('@graphcommerce/magento-customer', () => ({
16
+ useCustomerQuery,
17
+ useCustomerSession: () => ({ loggedIn: true }),
18
+ useGuestQuery: () => ({ data: undefined }),
19
+ }))
20
+ vi.mock('@graphcommerce/next-config/config', () => ({ wishlistHideForGuests: false }))
21
+ vi.mock('@graphcommerce/next-ui', () => ({
22
+ nonNullable: (value: unknown) => value !== null && value !== undefined,
23
+ }))
24
+ vi.mock('../useWishlistEnabled/useWishlistEnabled', () => ({ useWishlistEnabled: () => true }))
25
+
26
+ describe('useWishlistItems', () => {
27
+ it('loads the requested page of the wishlist', () => {
28
+ renderHook(() => useWishlistItems(2, 5))
29
+
30
+ expect(useCustomerQuery).toHaveBeenCalledWith(
31
+ UseWishlistCustomerDocument,
32
+ expect.objectContaining({ variables: { currentPage: 2, pageSize: 5 } }),
33
+ )
34
+ })
35
+
36
+ it('returns the number of items in the wishlist, also when a page holds fewer items', () => {
37
+ const { result } = renderHook(() => useWishlistItems(1, 1))
38
+
39
+ expect(result.current.items).toHaveLength(1)
40
+ expect(result.current.count).toBe(25)
41
+ })
42
+ })
@@ -4,6 +4,7 @@ import {
4
4
  useCustomerSession,
5
5
  useGuestQuery,
6
6
  } from '@graphcommerce/magento-customer'
7
+ import { wishlistHideForGuests } from '@graphcommerce/next-config/config'
7
8
  import { nonNullable } from '@graphcommerce/next-ui'
8
9
  import type { WishlistItemFragment } from '../../queries/WishlistItem.gql'
9
10
  import { useWishlistEnabled } from '../useWishlistEnabled/useWishlistEnabled'
@@ -19,24 +20,31 @@ export type UseWishlistItemsGuestReturn = {
19
20
  enabled: boolean
20
21
  loggedIn: false
21
22
  items: WishlistItemFragment[]
23
+ count?: number | null
22
24
  } & useQuery.Result<UseWishlistGuestQuery, UseWishlistGuestQueryVariables>
23
25
 
24
26
  export type UseWishlistItemsCustomerReturn = {
25
27
  enabled: boolean
26
28
  loggedIn: true
27
29
  items: WishlistItemFragment[]
30
+ count?: number | null
28
31
  } & useQuery.Result<UseWishlistCustomerQuery, UseWishlistCustomerQueryVariables>
29
32
 
30
33
  export type UseWishlistItemsReturn = UseWishlistItemsGuestReturn | UseWishlistItemsCustomerReturn
31
34
 
32
- export function useWishlistItems(): UseWishlistItemsReturn {
35
+ export function useWishlistItems(currentPage = 1, pageSize = 20): UseWishlistItemsReturn {
33
36
  const { loggedIn } = useCustomerSession()
34
37
  const enabled = useWishlistEnabled()
35
38
 
36
39
  /** Get customer wishlist from session */
37
- const wishlistCustomer = useCustomerQuery(UseWishlistCustomerDocument, { skip: !enabled })
40
+ const wishlistCustomer = useCustomerQuery(UseWishlistCustomerDocument, {
41
+ variables: { currentPage, pageSize },
42
+ skip: !enabled,
43
+ })
38
44
  /** Get guest wishlist items from cache */
39
- const wishlistGuest = useGuestQuery(UseWishlistGuestDocument, { skip: !enabled })
45
+ const wishlistGuest = useGuestQuery(UseWishlistGuestDocument, {
46
+ skip: !enabled || !!wishlistHideForGuests,
47
+ })
40
48
 
41
49
  if (loggedIn) {
42
50
  const items = (
@@ -44,7 +52,10 @@ export function useWishlistItems(): UseWishlistItemsReturn {
44
52
  ?.items ?? []
45
53
  ).filter(nonNullable)
46
54
 
47
- return { ...wishlistCustomer, enabled, loggedIn: true, items }
55
+ const count = (wishlistCustomer.data ?? wishlistCustomer.previousData)?.customer?.wishlists?.[0]
56
+ ?.items_count
57
+
58
+ return { ...wishlistCustomer, enabled, loggedIn: true, items, count }
48
59
  }
49
60
 
50
61
  const items = (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphcommerce/magento-wishlist",
3
- "version": "11.0.0-canary.50",
3
+ "version": "11.0.0-canary.54",
4
4
  "sideEffects": false,
5
5
  "prettier": "@graphcommerce/prettier-config-pwa",
6
6
  "browserslist": [
@@ -13,20 +13,20 @@
13
13
  }
14
14
  },
15
15
  "peerDependencies": {
16
- "@graphcommerce/ecommerce-ui": "^11.0.0-canary.50",
17
- "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.50",
18
- "@graphcommerce/graphql": "^11.0.0-canary.50",
19
- "@graphcommerce/graphql-mesh": "^11.0.0-canary.50",
20
- "@graphcommerce/image": "^11.0.0-canary.50",
21
- "@graphcommerce/magento-cart": "^11.0.0-canary.50",
22
- "@graphcommerce/magento-customer": "^11.0.0-canary.50",
23
- "@graphcommerce/magento-product": "^11.0.0-canary.50",
24
- "@graphcommerce/magento-product-configurable": "^11.0.0-canary.50",
25
- "@graphcommerce/magento-store": "^11.0.0-canary.50",
26
- "@graphcommerce/next-config": "^11.0.0-canary.50",
27
- "@graphcommerce/next-ui": "^11.0.0-canary.50",
28
- "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.50",
29
- "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.50",
16
+ "@graphcommerce/ecommerce-ui": "^11.0.0-canary.54",
17
+ "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.54",
18
+ "@graphcommerce/graphql": "^11.0.0-canary.54",
19
+ "@graphcommerce/graphql-mesh": "^11.0.0-canary.54",
20
+ "@graphcommerce/image": "^11.0.0-canary.54",
21
+ "@graphcommerce/magento-cart": "^11.0.0-canary.54",
22
+ "@graphcommerce/magento-customer": "^11.0.0-canary.54",
23
+ "@graphcommerce/magento-product": "^11.0.0-canary.54",
24
+ "@graphcommerce/magento-product-configurable": "^11.0.0-canary.54",
25
+ "@graphcommerce/magento-store": "^11.0.0-canary.54",
26
+ "@graphcommerce/next-config": "^11.0.0-canary.54",
27
+ "@graphcommerce/next-ui": "^11.0.0-canary.54",
28
+ "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.54",
29
+ "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.54",
30
30
  "@lingui/core": "^5",
31
31
  "@lingui/macro": "^5",
32
32
  "@lingui/react": "^5",
@@ -16,7 +16,7 @@ function WishlistUrlHandler() {
16
16
  const { setValue } = useFormAddProductsToCart()
17
17
  const router = useRouter()
18
18
  const { loggedIn } = useCustomerSession()
19
- const wishlist = useWishlistItems()
19
+ const wishlist = useWishlistItems(1, 999)
20
20
 
21
21
  const wishlistItems = wishlist.items?.map((item) => item)
22
22
 
@@ -0,0 +1,11 @@
1
+ fragment WishlistPagination on Wishlist {
2
+ __typename
3
+ id
4
+ items_count
5
+ items_v2(currentPage: $currentPage, pageSize: $pageSize) {
6
+ __typename
7
+ items {
8
+ ...WishlistItem
9
+ }
10
+ }
11
+ }