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

fix(commerce): properly memoize suggestions controllers #4184

Merged
merged 9 commits into from
Jul 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export function buildFieldSuggestionsGenerator(

const createFieldSuggestionsControllers = createSelector(
(state: CommerceEngineState) => state.fieldSuggestionsOrder!,
(facetOrder) =>
(state: CommerceEngineState) => state.commerceFacetSet,
(facetOrder, _) =>
facetOrder.map(({type, facetId}) => {
switch (type) {
case 'hierarchical':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const getExecuteFacetSearchThunkPayloadCreator =
navigatorContext
);

const response = await apiClient.facetSearch(await req);
const response = await apiClient.facetSearch(req);
Spuffynism marked this conversation as resolved.
Show resolved Hide resolved

return {facetId, response};
};
Expand Down
43 changes: 35 additions & 8 deletions packages/headless/src/integration-tests/commerce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CommerceEngineConfiguration,
} from '../app/commerce-engine/commerce-engine';
import {getSampleCommerceEngineConfiguration} from '../app/commerce-engine/commerce-engine-configuration';
import {buildFieldSuggestionsGenerator} from '../controllers/commerce/field-suggestions/headless-field-suggestions-generator';
import {ProductListing} from '../controllers/commerce/product-listing/headless-product-listing';
import {buildProductListing} from '../controllers/commerce/product-listing/headless-product-listing';
import {buildRecommendations} from '../controllers/commerce/recommendations/headless-recommendations';
Expand All @@ -16,15 +17,15 @@ describe.skip('commerce', () => {
let engine: CommerceEngine;

beforeAll(async () => {
Object.defineProperty(global, 'document', {
value: {referrer: 'referrer'},
configurable: true,
});

configuration = getSampleCommerceEngineConfiguration();
engine = buildCommerceEngine({
configuration,
loggerOptions: {level: 'silent'},
configuration: {
...configuration,
analytics: {
...configuration.analytics,
enabled: false,
},
},
});
});

Expand Down Expand Up @@ -79,7 +80,7 @@ describe.skip('commerce', () => {
const controllers = facetGenerator.facets;
const facetController = controllers[0];

await waitForNextStateChange(productListing, {
await waitForNextStateChange(facetController, {
action: () => {
switch (facetController.type) {
case 'numericalRange':
Expand Down Expand Up @@ -142,4 +143,30 @@ describe.skip('commerce', () => {

expect(recommendations.state.products).not.toEqual([]);
});

it('provides field suggestions', async () => {
const box = buildSearchBox(engine);

const generator = buildFieldSuggestionsGenerator(engine);

await waitForNextStateChange(box, {
action: () => box.updateText('ca'),
expectedSubscriberCalls: 3,
});

expect(generator.fieldSuggestions).toHaveLength(3);

for (const controller of generator.fieldSuggestions) {
await waitForNextStateChange(engine, {
action: () => controller.updateText('can'),
expectedSubscriberCalls: 3,
});
}

const controller = generator.fieldSuggestions.find(
(controller) => controller.state.facetId === 'ec_category'
)!;

expect(controller.state.values).not.toEqual([]);
});
});