-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1793 from SalesforceCommerceCloud/custom-endpoint…
…-support [commerce-sdk-react] SCAPI Custom endpoint support
- Loading branch information
Showing
11 changed files
with
383 additions
and
8 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
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) | ||
}) | ||
}) |
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.