-
Notifications
You must be signed in to change notification settings - Fork 130
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
[commerce-sdk-react] SCAPI Custom endpoint support #1793
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9943fee
custom endpoint query
kevinxh 2cc05db
add query key
kevinxh 31a716c
add query test
kevinxh 15e0712
add useCustomMutation
kevinxh 51c4fb7
add useMutation test
kevinxh 6de641a
fix tests
kevinxh 3ee10de
remove console logs
kevinxh e2c4ab6
merge client config
kevinxh 2c4551f
merge headers and client configs
kevinxh 25fe405
add more tests
kevinxh 5c51521
add default strings
kevinxh a3906c0
add changelog
kevinxh b32ec50
bump size by 1kb
kevinxh 57d39cc
rename types
kevinxh b988313
re-order options to make it overriable
kevinxh 717109b
Update packages/commerce-sdk-react/src/hooks/useQuery.ts
kevinxh 1cfc058
order alphabetically
kevinxh 4c3ec0f
Merge branch 'custom-endpoint-support' of https://github.com/Salesfor…
kevinxh dfdd49c
guard missing customApiPathParameters
kevinxh 08ad401
guard missing params
kevinxh 9ba471a
export useCustomMutation
kevinxh b2b3a69
fix test
kevinxh 0185552
merge changelog
kevinxh 08b8bd9
reuse types
kevinxh 84b4b4a
resolve merge conflict
kevinxh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2024, 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 nock from 'nock' | ||
import {act} from '@testing-library/react' | ||
import { | ||
mockMutationEndpoints, | ||
renderHookWithProviders, | ||
waitAndExpectSuccess, | ||
DEFAULT_TEST_CONFIG | ||
} from '../test-utils' | ||
import {useCustomMutation} from './useMutation' | ||
|
||
jest.mock('../auth/index.ts', () => { | ||
const {default: mockAuth} = jest.requireActual('../auth/index.ts') | ||
mockAuth.prototype.ready = jest.fn().mockResolvedValue({access_token: 'access_token'}) | ||
return mockAuth | ||
}) | ||
|
||
describe('useCustomMutation', () => { | ||
beforeEach(() => nock.cleanAll()) | ||
test('useCustomMutation returns data on success', async () => { | ||
const mockRes = {data: '123'} | ||
const apiName = 'hello-world' | ||
mockMutationEndpoints(apiName, mockRes) | ||
const {result} = renderHookWithProviders(() => { | ||
const clientConfig = { | ||
parameters: { | ||
clientId: 'CLIENT_ID', | ||
siteId: 'SITE_ID', | ||
organizationId: 'ORG_ID', | ||
shortCode: 'SHORT_CODE' | ||
}, | ||
proxy: 'http://localhost:8888/mobify/proxy/api' | ||
} | ||
return useCustomMutation({ | ||
options: { | ||
method: 'POST', | ||
customApiPathParameters: { | ||
endpointPath: 'test-hello-world', | ||
apiName | ||
}, | ||
body: {test: '123'} | ||
}, | ||
clientConfig, | ||
rawResponse: false | ||
}) | ||
}) | ||
expect(result.current.data).toBeUndefined() | ||
act(() => result.current.mutate()) | ||
await waitAndExpectSuccess(() => result.current) | ||
expect(result.current.data).toEqual(mockRes) | ||
}) | ||
test('clientConfig is optional, default to CommerceApiProvider configs', async () => { | ||
const mockRes = {data: '123'} | ||
const apiName = 'hello-world' | ||
const endpointPath = 'test-hello-world' | ||
mockMutationEndpoints( | ||
`${apiName}/v1/organizations/${DEFAULT_TEST_CONFIG.organizationId}/${endpointPath}`, | ||
mockRes | ||
) | ||
const {result} = renderHookWithProviders(() => { | ||
return useCustomMutation({ | ||
options: { | ||
method: 'POST', | ||
customApiPathParameters: { | ||
endpointPath: 'test-hello-world', | ||
apiName | ||
}, | ||
body: {test: '123'} | ||
}, | ||
rawResponse: false | ||
}) | ||
}) | ||
expect(result.current.data).toBeUndefined() | ||
act(() => result.current.mutate()) | ||
await waitAndExpectSuccess(() => result.current) | ||
expect(result.current.data).toEqual(mockRes) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2024, 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 nock from 'nock' | ||
import { | ||
mockQueryEndpoint, | ||
renderHookWithProviders, | ||
waitAndExpectSuccess, | ||
DEFAULT_TEST_CONFIG | ||
} from '../test-utils' | ||
import {useCustomQuery} from './useQuery' | ||
|
||
jest.mock('../auth/index.ts', () => { | ||
const {default: mockAuth} = jest.requireActual('../auth/index.ts') | ||
mockAuth.prototype.ready = jest.fn().mockResolvedValue({access_token: 'access_token'}) | ||
return mockAuth | ||
}) | ||
|
||
describe('useCustomQuery', () => { | ||
beforeEach(() => nock.cleanAll()) | ||
afterEach(() => { | ||
expect(nock.pendingMocks()).toHaveLength(0) | ||
}) | ||
test('useCustomQuery returns data on success', async () => { | ||
const mockRes = {data: '123'} | ||
const apiName = 'hello-world' | ||
mockQueryEndpoint(apiName, mockRes) | ||
const {result} = renderHookWithProviders(() => { | ||
const clientConfig = { | ||
parameters: { | ||
clientId: 'CLIENT_ID', | ||
siteId: 'SITE_ID', | ||
organizationId: 'ORG_ID', | ||
shortCode: 'SHORT_CODE' | ||
}, | ||
proxy: 'http://localhost:8888/mobify/proxy/api' | ||
} | ||
return useCustomQuery({ | ||
options: { | ||
method: 'GET', | ||
customApiPathParameters: { | ||
apiVersion: 'v1', | ||
endpointPath: 'test-hello-world', | ||
apiName | ||
} | ||
}, | ||
clientConfig, | ||
rawResponse: false | ||
}) | ||
}) | ||
await waitAndExpectSuccess(() => result.current) | ||
expect(result.current.data).toEqual(mockRes) | ||
}) | ||
test('clientConfig is optional, default to CommerceApiProvider configs', async () => { | ||
const mockRes = {data: '123'} | ||
const apiName = 'hello-world' | ||
const endpointPath = 'test-hello-world' | ||
mockQueryEndpoint( | ||
`${apiName}/v1/organizations/${DEFAULT_TEST_CONFIG.organizationId}/${endpointPath}`, | ||
mockRes | ||
) | ||
const {result} = renderHookWithProviders(() => { | ||
return useCustomQuery({ | ||
options: { | ||
method: 'GET', | ||
customApiPathParameters: { | ||
apiVersion: 'v1', | ||
endpointPath, | ||
apiName | ||
} | ||
}, | ||
rawResponse: false | ||
}) | ||
}) | ||
await waitAndExpectSuccess(() => result.current) | ||
expect(result.current.data).toEqual(mockRes) | ||
}) | ||
test('query defaults to GET request', async () => { | ||
const mockRes = {data: '123'} | ||
const apiName = 'hello-world' | ||
const endpointPath = 'test-hello-world' | ||
mockQueryEndpoint( | ||
`${apiName}/v1/organizations/${DEFAULT_TEST_CONFIG.organizationId}/${endpointPath}`, | ||
mockRes | ||
) | ||
const {result} = renderHookWithProviders(() => { | ||
return useCustomQuery({ | ||
options: { | ||
customApiPathParameters: { | ||
apiVersion: 'v1', | ||
endpointPath, | ||
apiName | ||
} | ||
}, | ||
rawResponse: false | ||
}) | ||
}) | ||
await waitAndExpectSuccess(() => result.current) | ||
expect(result.current.data).toEqual(mockRes) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This answers my previous question. 👍