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

feat(headless commerce): retrieve clientId from Relay instead of coveoua when building commerce API requests #4111

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
109 changes: 71 additions & 38 deletions packages/headless/src/features/commerce/common/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Relay, createRelay} from '@coveo/relay';
import {CurrencyCodeISO4217} from '@coveo/relay-event-types';
import {
BaseCommerceAPIRequest,
Expand All @@ -18,6 +19,14 @@ import {getCommerceSortInitialState} from '../sort/sort-state';
import * as Actions from './actions';

describe('commerce common actions', () => {
let relay: Relay;
beforeEach(() => {
relay = createRelay({
token: 'token',
trackingId: 'trackingId',
url: 'url',
});
});
describe('#buildBaseCommerceAPIRequest', () => {
let expected: BaseCommerceAPIRequest;
let state: Actions.StateNeededByQueryCommerceAPI;
Expand Down Expand Up @@ -80,15 +89,15 @@ describe('commerce common actions', () => {
};
});

it('given a state with no commercePagination section, returns the expected base request', async () => {
it('given a state with no commercePagination section, returns the expected base request', () => {
delete state.commercePagination;

const request = await Actions.buildBaseCommerceAPIRequest(state);
const request = Actions.buildBaseCommerceAPIRequest(state, relay);

expect(request).toEqual({...expected});
});

it('given a state that has the commercePagination section, returns expected base request with expected #page and #perPage', async () => {
it('given a state that has the commercePagination section, returns expected base request with expected #page and #perPage', () => {
state.commercePagination = {
...getCommercePaginationInitialState(),
principal: {
Expand All @@ -104,12 +113,12 @@ describe('commerce common actions', () => {
perPage: state.commercePagination.principal.perPage,
};

const request = await Actions.buildBaseCommerceAPIRequest(state);
const request = Actions.buildBaseCommerceAPIRequest(state, relay);

expect(request).toEqual(expectedWithPagination);
});

it('given a slotId, returns expected base request with the effective pagination for that slot', async () => {
it('given a slotId, returns expected base request with the effective pagination for that slot', () => {
const slotId = 'slot_id';
state.commercePagination = {
...getCommercePaginationInitialState(),
Expand All @@ -128,7 +137,7 @@ describe('commerce common actions', () => {
perPage: state.commercePagination.recommendations[slotId]!.perPage,
};

const request = await Actions.buildBaseCommerceAPIRequest(state, slotId);
const request = Actions.buildBaseCommerceAPIRequest(state, relay, slotId);

expect(request).toEqual(expectedWithPagination);
});
Expand All @@ -146,63 +155,75 @@ describe('commerce common actions', () => {
);
});

it('given a state with none of the optional sections, returns the expected base request along an empty #facets array', async () => {
it('given a state with none of the optional sections, returns the expected base request along an empty #facets array', () => {
delete state.commerceSort;
delete state.facetOrder;
delete state.commerceFacetSet;

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request).toEqual({
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [],
});
});

it('given a state that has the facetOrder section but not the commerceFacetSet section, returns the expected base request with an empty #facets array', async () => {
it('given a state that has the facetOrder section but not the commerceFacetSet section, returns the expected base request with an empty #facets array', () => {
delete state.commerceSort;
delete state.commerceFacetSet;

state.facetOrder = ['facet_id'];

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request).toEqual({
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [],
});
});

it('given a state that has the commerceFacetSet section but not the facetOrder section, returns the expected base request with an empty #facets array', async () => {
it('given a state that has the commerceFacetSet section but not the facetOrder section, returns the expected base request with an empty #facets array', () => {
delete state.commerceSort;
delete state.commerceFacetSet;

state.commerceFacetSet = {
facet_id: buildMockCommerceFacetSlice(),
};

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request).toEqual({
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [],
});
});

it.each([true, false])(
'sets the capture property from the analytics configuration',
async (analyticsEnabled) => {
(analyticsEnabled) => {
state.configuration.analytics.enabled = analyticsEnabled;

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request.context.capture).toEqual(analyticsEnabled);
}
Expand Down Expand Up @@ -240,18 +261,21 @@ describe('commerce common actions', () => {
[facet2.request.facetId]: facet2,
};
});
it('includes all non-empty facets in the #facets array of the returned request', async () => {
const request = await Actions.buildCommerceAPIRequest(state);
it('includes all non-empty facets in the #facets array of the returned request', () => {
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request).toEqual({
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [facet1.request, facet2.request],
});
});

it('does not include empty facets in the #facets array of the returned request', async () => {
it('does not include empty facets in the #facets array of the returned request', () => {
const facet3 = buildMockCommerceFacetSlice({
request: {
...buildMockCommerceFacetRequest({
Expand All @@ -265,37 +289,43 @@ describe('commerce common actions', () => {
state.commerceFacetSet![facet3.request.facetId] = facet3;
state.facetOrder.push(facet3.request.facetId);

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

expect(request).toEqual({
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [facet1.request, facet2.request],
});
});
});

describe('give a state that has the commerceSort section', () => {
describe('given a state that has the commerceSort section', () => {
beforeEach(() => {
delete state.facetOrder;
delete state.commerceFacetSet;
});

it('when applied sort is "relevance", returns expected base request with expected #sort.sortCriteria', async () => {
it('when applied sort is "relevance", returns expected base request with expected #sort.sortCriteria', () => {
state.commerceSort = {
...getCommerceSortInitialState(),
appliedSort: {
by: SortBy.Relevance,
},
};

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

const expectedWithSort: CommerceAPIRequest = {
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [],
sort: {
sortCriteria: SortBy.Relevance,
Expand All @@ -305,7 +335,7 @@ describe('commerce common actions', () => {
expect(request).toEqual(expectedWithSort);
});

it('when applied sort is "fields", returns expected base request with expected #sort.sortCriteria and #sort.fields', async () => {
it('when applied sort is "fields", returns expected base request with expected #sort.sortCriteria and #sort.fields', () => {
const sortCriterion: SortCriterion = {
by: SortBy.Fields,
fields: [
Expand All @@ -321,12 +351,15 @@ describe('commerce common actions', () => {
appliedSort: sortCriterion,
};

const request = await Actions.buildCommerceAPIRequest(state);
const request = Actions.buildCommerceAPIRequest(state, relay);

expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(state);
expect(mockedBuildBaseCommerceAPIRequest).toHaveBeenCalledWith(
state,
relay
);

const expectedWithSort: CommerceAPIRequest = {
...(await mockedBuildBaseCommerceAPIRequest.mock.results[0].value),
...mockedBuildBaseCommerceAPIRequest.mock.results[0].value,
facets: [],
sort: {
sortCriteria: sortCriterion.by,
Expand Down
18 changes: 10 additions & 8 deletions packages/headless/src/features/commerce/common/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Relay} from '@coveo/relay';
import {getAnalyticsSource} from '../../../api/analytics/analytics-selectors';
import {getVisitorID} from '../../../api/analytics/coveo-analytics-utils';
import {SortParam} from '../../../api/commerce/commerce-api-params';
import {
BaseCommerceAPIRequest,
Expand Down Expand Up @@ -33,30 +33,32 @@ export interface QueryCommerceAPIThunkReturn {
response: CommerceSuccessResponse;
}

export const buildCommerceAPIRequest = async (
state: ListingAndSearchStateNeededByQueryCommerceAPI
): Promise<CommerceAPIRequest> => {
export const buildCommerceAPIRequest = (
state: ListingAndSearchStateNeededByQueryCommerceAPI,
relay: Relay
fbeaudoincoveo marked this conversation as resolved.
Show resolved Hide resolved
): CommerceAPIRequest => {
return {
...(await buildBaseCommerceAPIRequest(state)),
...buildBaseCommerceAPIRequest(state, relay),
facets: getFacets(state),
...(state.commerceSort && {
sort: getSort(state.commerceSort.appliedSort),
}),
};
};

export const buildBaseCommerceAPIRequest = async (
export const buildBaseCommerceAPIRequest = (
state: StateNeededByQueryCommerceAPI,
relay: Relay,
slotId?: string
): Promise<BaseCommerceAPIRequest> => {
): BaseCommerceAPIRequest => {
const {view, user, ...restOfContext} = state.commerceContext;
return {
accessToken: state.configuration.accessToken,
url: state.configuration.platformUrl,
organizationId: state.configuration.organizationId,
trackingId: state.configuration.analytics.trackingId,
...restOfContext,
clientId: await getVisitorID(state.configuration.analytics),
clientId: relay.getMeta('').clientId,
context: {
user,
view,
Expand Down
Loading
Loading