Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify hooks logic #959

Merged
merged 132 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
465bf53
Merge hooks changes from wjh/simplify-hooks
wjhsf Feb 3, 2023
6a03683
lint fixes
wjhsf Feb 3, 2023
e9a3983
Use correct type.
wjhsf Feb 3, 2023
2602899
Remove unnecessary file.
wjhsf Feb 3, 2023
2b7d27f
Add support for updater in cache updates.
wjhsf Feb 6, 2023
90dfab1
Sort imports.
wjhsf Feb 7, 2023
69f2c11
Change `updater` generic from being the same for all to being per fun…
wjhsf Feb 8, 2023
e7bd76d
Clean up cache update matrix.
wjhsf Feb 8, 2023
b0a8715
Rekerjigger cache logic to see what we can do with it.
wjhsf Feb 8, 2023
f12fda1
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 8, 2023
229f650
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 9, 2023
107cd5b
Create options merge util.
wjhsf Feb 9, 2023
ab586ec
Use merged options in query hooks and simplify query keys.
wjhsf Feb 9, 2023
c28d249
Simplify endMatches logic.
wjhsf Feb 9, 2023
623e1de
Add predicate helper to match API config values.
wjhsf Feb 9, 2023
9f550ae
Fix temp test file.
wjhsf Feb 9, 2023
80c0fe8
Use consistent order of generic parameters.
wjhsf Feb 10, 2023
2bd6fc7
Remove pointless line.
wjhsf Feb 10, 2023
6f26e05
Use consistent generic parameter names.
wjhsf Feb 10, 2023
7632df1
Add comments.
wjhsf Feb 10, 2023
8ad426c
More comments.
wjhsf Feb 10, 2023
9e402c2
Remove test directory.
wjhsf Feb 10, 2023
5c5fff0
Generate hooks for all APIs
wjhsf Feb 10, 2023
8b9e7f2
Update Shopper Baskets config from Test directory.
wjhsf Feb 10, 2023
6b30735
Update Shopper Experience query hooks.
wjhsf Feb 10, 2023
b3b36b2
Fix missed merged options type.
wjhsf Feb 13, 2023
71e8831
Add Shopper Login hooks.
wjhsf Feb 13, 2023
0a9ef34
Convert getGiftCertificate from mutation hook to query hook.
wjhsf Feb 13, 2023
d4cdf44
Update Shopper Login hooks.
wjhsf Feb 13, 2023
79930b9
First pass at converting to new cache update matrix.
wjhsf Feb 13, 2023
6a35ad1
Remove canceled Shopper Discovery Search API.
wjhsf Feb 13, 2023
d27786d
Update login helpers.
wjhsf Feb 14, 2023
601b9f3
Fix auth types.
wjhsf Feb 14, 2023
d0e5855
Hopefully avoid caching getGiftCertificate.
wjhsf Feb 14, 2023
7dc671b
Rename shopper login helper to auth helper.
wjhsf Feb 15, 2023
b0f5735
Move auth helper to hooks root.
wjhsf Feb 15, 2023
e6c2ba7
Mark useAuthorizationHeader as internal.
wjhsf Feb 15, 2023
89bd6aa
Rename useAuth to useAuthContext for clarity w/ useAuthHelper.
wjhsf Feb 15, 2023
4cdf3c8
Convert useAuthHelper to default export for consistency.
wjhsf Feb 15, 2023
da93e18
Remove siteId from AuthData, as it is passed via config.
wjhsf Feb 15, 2023
4e727a5
Update comment with generated code.
wjhsf Feb 15, 2023
7e3f613
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 15, 2023
ed97973
Move headers to last parameter because we rarely use it.
wjhsf Feb 15, 2023
dd6adce
Update cache update matrices to new interface.
wjhsf Feb 15, 2023
87b43a9
Refactore test project after api changes.
bendvc Feb 16, 2023
a5f56f7
Clean up cache update logic.
wjhsf Feb 16, 2023
e2377ac
Update names for clarity.
wjhsf Feb 16, 2023
080faf1
Revert test file to version from develop.
wjhsf Feb 16, 2023
973bb6a
Merge remote-tracking branch 'origin/origin/bendvc/hooks-on-develop' …
wjhsf Feb 16, 2023
641138d
Add reminder to revert.
wjhsf Feb 16, 2023
9b6b290
Remove unnecessary default export.
wjhsf Feb 16, 2023
8daf092
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 16, 2023
86fc823
Cache update TODOs should return functions, not throw.
wjhsf Feb 16, 2023
28441ad
Allow user to override authorization header.
wjhsf Feb 16, 2023
f517523
Remove old TODO.
wjhsf Feb 16, 2023
d403b4d
Remove ability to set custom query key.
wjhsf Feb 17, 2023
7a0deab
Replace getQueryKey callback with just declaring query key.
wjhsf Feb 17, 2023
03f455b
Clean up types.
wjhsf Feb 17, 2023
28dcb7b
Rename config.ts to cache.ts
wjhsf Feb 17, 2023
d40554f
Restore type checking test files.
wjhsf Feb 20, 2023
730de3a
Suppress react query error logs during tests.
wjhsf Feb 20, 2023
4a2c86d
Strip unused parameters before generating query key.
wjhsf Feb 20, 2023
3bf4f05
Fix tpe errors.
wjhsf Feb 20, 2023
00ac081
Partially fix type errors.
wjhsf Feb 20, 2023
11fd9eb
Update Shopper Baskets cache logic to use narrower parameters.
wjhsf Feb 20, 2023
382fcb7
Test that all endpoints have hooks.
wjhsf Feb 20, 2023
6bf8272
Create new mutation tests for refactored code.
wjhsf Feb 20, 2023
2dd71ec
Add test to validate all mutations have cache update logic.
wjhsf Feb 20, 2023
17621e4
Alphabetize cache updates.
wjhsf Feb 20, 2023
d49f16a
Simplify mutation tests a bit.
wjhsf Feb 20, 2023
241cdfe
Fix nock adding duplicate request mocks.
wjhsf Feb 21, 2023
859032c
Make failing tests have more helpful failures.
wjhsf Feb 21, 2023
c5ad9c2
Fix tests failing due to missing parameters.
wjhsf Feb 21, 2023
7696a88
Replace old mutation test file with new one.
wjhsf Feb 21, 2023
15e8aea
Remove unused helper type.
wjhsf Feb 21, 2023
98e9fff
Create hook success/error helpers.
wjhsf Feb 21, 2023
6764cb8
Improve parameter matching.
wjhsf Feb 21, 2023
465c748
Add `deleteBasket` tests.
wjhsf Feb 21, 2023
603552d
Add request mock to fix test.
wjhsf Feb 21, 2023
cc0e0c2
Add TODO cache update logic for missing mutations.
wjhsf Feb 21, 2023
acbc12d
Change onSuccess from bound to unbound function.
wjhsf Feb 22, 2023
9efac92
Add test for not implemented mutations.
wjhsf Feb 22, 2023
c39f0a8
Pass not implemented tests.
wjhsf Feb 22, 2023
528d96a
Remove unused import.
wjhsf Feb 22, 2023
1648305
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 22, 2023
1f02ab4
Implement `resetPassword` caching as a no-op.
wjhsf Feb 22, 2023
a5a321e
Add Shopper Baskets query tests.
wjhsf Feb 22, 2023
1db46f0
Add "all endpoints have hooks" tests for all APIs.
wjhsf Feb 22, 2023
874b2ff
Fix failing index tests.
wjhsf Feb 22, 2023
da1c71e
Implement tests for all query hooks.
wjhsf Feb 23, 2023
0ae5349
Remove hooks for `authorizeCustomer` and `getTrustedAgentAuthorizatio…
wjhsf Feb 23, 2023
45b63fd
Update "not implemented" tests to check if cache update logic exists.
wjhsf Feb 23, 2023
48d399d
Extract reused type into type def.
wjhsf Feb 23, 2023
3ce0fa6
Update comment to reflect changed tests.
wjhsf Feb 23, 2023
83c18d0
Remove unnecessary `async`.
wjhsf Feb 23, 2023
32b958a
Implement Shopper Contexts mutation tests.
wjhsf Feb 23, 2023
c2286d9
Rename `makeOptions` to `createOptions`.
wjhsf Feb 23, 2023
271d3a2
Implement Shopper Login mutation tests.
wjhsf Feb 23, 2023
a4ba2e5
Add extra ResponseError assertion.
wjhsf Feb 23, 2023
2972eb2
Update test names.
wjhsf Feb 23, 2023
f78a1c1
Implement Shopper Orders mutation tests.
wjhsf Feb 23, 2023
4a6409d
Convert TODO from throwing to just logging.
wjhsf Feb 23, 2023
4d856af
Fix failing "all endpoints have hooks" tests.
wjhsf Feb 23, 2023
2d66118
Remove unused imports.
wjhsf Feb 23, 2023
8246a6b
Introduce query key helpers.
wjhsf Feb 24, 2023
ca4bc5c
Remove no longer needed query test file.
wjhsf Feb 24, 2023
681085b
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 24, 2023
ea2b699
Update hook usage.
wjhsf Feb 24, 2023
e7073cd
Implement basic Shopper Customers mutation tests.
wjhsf Feb 24, 2023
33d04f0
Export query key types.
wjhsf Feb 24, 2023
448a826
Remove old file.
wjhsf Feb 24, 2023
87933ff
Update cache logic to use query keys instead of predicates.
wjhsf Feb 25, 2023
09d7899
Update parser for proper TypeScript support.
wjhsf Feb 25, 2023
b162e03
Remove unused babel-eslint.
wjhsf Feb 25, 2023
05a0975
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 25, 2023
1ec20ee
Temporarily(?) use internal-lib-build so TypeScript files are properl…
wjhsf Feb 26, 2023
8732c1f
Fix eslint and tsc errors.
wjhsf Feb 26, 2023
f0a119f
Temporarily suppress linter errors.
wjhsf Feb 27, 2023
96347e3
Implement tests for shopper customers mutations that modify a customer.
wjhsf Feb 27, 2023
504773b
Implement full Shopper Customers mutation tests.
wjhsf Feb 28, 2023
cacd83a
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Feb 28, 2023
63f35c8
Rename `StringIndexNever` to `StringIndexToNever`
wjhsf Feb 28, 2023
254b43e
Remove unnecessary invalidation.
wjhsf Feb 28, 2023
081ea67
Update generated comments for hooks.
wjhsf Feb 28, 2023
7d52245
Remove unused variable.
wjhsf Feb 28, 2023
8918178
Remove SLAS `authenticateCustomer` and `getPasswordResetToken` hooks.
wjhsf Mar 1, 2023
bff5174
Remove deprecated Shopper Customers mutations.
wjhsf Mar 1, 2023
a5cc03c
Comment out Shopper Customers endpoints in closed beta.
wjhsf Mar 1, 2023
8a9fdcc
Fix Shopper Customers tests.
wjhsf Mar 1, 2023
4b645c7
Fix Shopper Login tests.
wjhsf Mar 1, 2023
a5b0a40
Update "hook exists" tests to distinguish between "mutation in enum" …
wjhsf Mar 1, 2023
61d8fcb
Merge branch 'develop' into wjh/hooks-on-develop
wjhsf Mar 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {ShopperBasketsTypes} from 'commerce-sdk-isomorphic'
import {ApiClients, CacheUpdateMatrix} from '../types'
import {CacheUpdate} from '../types'

const updateBasketQuery = (basketId?: string): CacheUpdate => {
// TODO: we're missing headers, rawResponse -> not only {basketId}
const arg = {basketId}
return basketId
? {
update: [['/baskets', basketId, arg]]
wjhsf marked this conversation as resolved.
Show resolved Hide resolved
}
: {}
}

const removeBasketQuery = (basketId?: string): CacheUpdate => {
const arg = {basketId}
return basketId
? {
remove: [['/baskets', basketId, arg]]
}
: {}
}

const invalidateCustomerBasketsQuery = (customerId: string | null): CacheUpdate => {
// TODO: should we use arg here or not? The invalidate method does not need exact query key.
const arg = {customerId}
return customerId
? {
invalidate: [['/customers', customerId, '/baskets', arg]]
}
: {}
}

const updateBasketFromRequest = (
customerId: string | null,
params: {
parameters: ShopperBasketsTypes.Basket
}
): CacheUpdate => ({
...updateBasketQuery(params.parameters.basketId),
...invalidateCustomerBasketsQuery(customerId)
})

const updateBasketFromResponse = (
customerId: string | null,
params: unknown, // not used,
response: ShopperBasketsTypes.Basket
): CacheUpdate => ({
...updateBasketQuery(response.basketId),
...invalidateCustomerBasketsQuery(customerId)
})

export const cacheUpdateMatrix: CacheUpdateMatrix<ApiClients['shopperBaskets']> = {
addCouponToBasket: updateBasketFromRequest,
addItemToBasket: updateBasketFromRequest,
removeItemFromBasket: updateBasketFromRequest,
addPaymentInstrumentToBasket: updateBasketFromRequest,
createBasket: updateBasketFromResponse, // Response!
deleteBasket: (customerId, params): CacheUpdate => ({
...invalidateCustomerBasketsQuery(customerId),
...removeBasketQuery(params.parameters.basketId)
}),
mergeBasket: updateBasketFromResponse, // Response!
removeCouponFromBasket: updateBasketFromRequest,
removePaymentInstrumentFromBasket: updateBasketFromRequest,
updateBasket: updateBasketFromRequest,
updateBillingAddressForBasket: updateBasketFromRequest,
updateCustomerForBasket: updateBasketFromRequest,
updateItemInBasket: updateBasketFromRequest,
updatePaymentInstrumentInBasket: updateBasketFromRequest,
updateShippingAddressForShipment: updateBasketFromRequest,
updateShippingMethodForShipment: updateBasketFromRequest
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Salesforce, Inc.
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Salesforce, Inc.
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
Expand All @@ -13,6 +13,8 @@ import {
assertUpdateQuery,
DEFAULT_TEST_HOST,
mockMutationEndpoints,
NEW_DATA,
OLD_DATA,
renderHookWithProviders
} from '../../test-utils'
import {
Expand All @@ -21,7 +23,7 @@ import {
useShopperBasketsMutation
} from './mutation'
import {useBasket} from './query'
import {useCustomerBaskets} from '../ShopperCustomers'
import {useCustomerBaskets} from '../ShopperCustomers/query'
import {CacheUpdateMatrixElement} from '../utils'

const CUSTOMER_ID = 'CUSTOMER_ID'
Expand All @@ -42,10 +44,7 @@ jest.mock('../useCustomerId.ts', () => {
return jest.fn().mockReturnValue(CUSTOMER_ID)
})

type MutationPayloads = {
[key in ShopperBasketsMutationType]?: {body: any; parameters: any}
}
const mutationPayloads: MutationPayloads = {
const mutationPayloads = {
updateBasket: {
parameters: {basketId: BASKET_ID},
body: {}
Expand Down Expand Up @@ -110,27 +109,9 @@ const mutationPayloads: MutationPayloads = {
parameters: {basketId: BASKET_ID, shipmentId: SHIPMENT_ID},
body: {id: '001'}
}
}
const oldCustomerBaskets = {
total: 1,
baskets: [{basketId: BASKET_ID, hello: 'world'}]
}

const newCustomerBaskets = {
total: 1,
baskets: [{basketId: BASKET_ID, hello: 'world_modified'}]
}
} as const

const oldBasket = {
basketId: BASKET_ID,
hello: 'world'
}

const newBasket = {
basketId: BASKET_ID,
hello: 'world_modified'
}
const tests = (Object.keys(mutationPayloads) as ShopperBasketsMutationType[]).map(
const tests = (Object.keys(mutationPayloads) as Array<keyof typeof mutationPayloads>).map(
(mutationName) => {
const payload = mutationPayloads[mutationName]

Expand All @@ -140,11 +121,7 @@ const tests = (Object.keys(mutationPayloads) as ShopperBasketsMutationType[]).ma
{
name: 'success',
assertions: async () => {
mockMutationEndpoints(
'/checkout/shopper-baskets/',
{errorResponse: 200},
newBasket
)
mockMutationEndpoints('/checkout/shopper-baskets/')
mockRelatedQueries()

const {result, waitForValueToChange} = renderHookWithProviders(() => {
Expand All @@ -171,24 +148,21 @@ const tests = (Object.keys(mutationPayloads) as ShopperBasketsMutationType[]).ma

await waitForValueToChange(() => result.current.mutation.isSuccess)
expect(result.current.mutation.isSuccess).toBe(true)

// On successful mutation, the query cache gets updated too. Let's assert it.
const cacheUpdateMatrix = getCacheUpdateMatrix(CUSTOMER_ID)
// @ts-ignore
const matrixElement = cacheUpdateMatrix[mutationName](payload, {})
const {invalidate, update, remove}: CacheUpdateMatrixElement = matrixElement

const assertionData = {
basket: newBasket,
customerBaskets: newCustomerBaskets
}
update?.forEach(({name}) => {
// @ts-ignore
assertUpdateQuery(result.current.queries[name], assertionData[name])
assertUpdateQuery(result.current.queries[name], NEW_DATA)
})

invalidate?.forEach(({name}) => {
// @ts-ignore
assertInvalidateQuery(result.current.queries[name], oldCustomerBaskets)
assertInvalidateQuery(result.current.queries[name], OLD_DATA)
})

remove?.forEach(({name}) => {
Expand Down Expand Up @@ -244,24 +218,24 @@ const mockRelatedQueries = () => {
.get((uri) => {
return uri.includes(basketEndpoint)
})
.reply(200, oldBasket)
.reply(200, OLD_DATA)
nock(DEFAULT_TEST_HOST)
.persist()
.get((uri) => {
return uri.includes(basketEndpoint)
})
.reply(200, newBasket)
.reply(200, NEW_DATA)

// For get customer basket
nock(DEFAULT_TEST_HOST)
.get((uri) => {
return uri.includes(customerEndpoint)
})
.reply(200, oldCustomerBaskets)
.reply(200, OLD_DATA)
nock(DEFAULT_TEST_HOST)
.persist()
.get((uri) => {
return uri.includes(customerEndpoint)
})
.reply(200, newCustomerBaskets)
.reply(200, NEW_DATA)
}
Loading