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(createInsightsMiddleware): always pass Algolia credentials locally #5554

Merged
merged 2 commits into from
Mar 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,13 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);
expect(insightsClient).toHaveBeenCalledTimes(1);
expect(insightsClient).toHaveBeenCalledWith(
'clickedObjectIDsAfterSearch',
{ eventName: 'Add to cart' }
{ eventName: 'Add to cart' },
{
headers: {
'X-Algolia-API-Key': 'apiKey',
'X-Algolia-Application-Id': 'appId',
},
}
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,27 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
});

expect(analytics.viewedObjectIDs).toHaveBeenCalledTimes(1);
expect(analytics.viewedObjectIDs).toHaveBeenCalledWith({
index: 'my-index',
eventName: 'My Hits Viewed',
objectIDs: ['obj1'],
});
expect(analytics.viewedObjectIDs).toHaveBeenCalledWith(
{
index: 'my-index',
eventName: 'My Hits Viewed',
objectIDs: ['obj1'],
},
{
headers: {
'X-Algolia-Application-Id': 'myAppId',
'X-Algolia-API-Key': 'myApiKey',
},
}
);
});

it('calls onEvent when given', () => {
const { insightsClient, instantSearchInstance, analytics } =
createTestEnvironment();

const onEvent = jest.fn();

instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
Expand All @@ -833,7 +842,42 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
hello: 'world',
},
},
insightsClient
expect.any(Function)
);
});

it('sends events using onEvent', () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment();

const onEvent = jest.fn((event, aa) => {
aa(event.insightsMethod, event.payload);
});

instantSearchInstance.use(
createInsightsMiddleware({
insightsClient,
onEvent,
})
);

instantSearchInstance.sendEventToInsights({
insightsMethod: 'viewedObjectIDs',
widgetType: 'ais.customWidget',
eventType: 'click',
payload: {
hello: 'world',
},
});

expect(insightsClient).toHaveBeenLastCalledWith(
'viewedObjectIDs',
{ hello: 'world' },
{
headers: {
'X-Algolia-API-Key': 'myApiKey',
'X-Algolia-Application-Id': 'myAppId',
},
}
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,27 @@ export function createInsightsMiddleware<
immediate: true,
});

// @ts-ignore
const insightsClientWithLocalCredentials = (method, payload) => {
Comment on lines +228 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// @ts-ignore
const insightsClientWithLocalCredentials = (method, payload) => {
const insightsClientWithLocalCredentials: InsightsClient = (method, payload) => {

does that work without angering TS?

Copy link
Member Author

@sarahdayan sarahdayan Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, we need to release the new version of the Insights client so that the types are up to date.

I can do it in a different PR if we don't release it before we're ready to merge this one.

return insightsClient(method, payload, {
headers: {
'X-Algolia-Application-Id': appId,
'X-Algolia-API-Key': apiKey,
},
});
};

instantSearchInstance.sendEventToInsights = (event: InsightsEvent) => {
if (onEvent) {
onEvent(event, _insightsClient as TInsightsClient);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe passing _insightsClient here was a bug, as this wouldn't point to the right function in an auto-pulled scenario.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, with auto-pulling that is wrong, but it used to be the user-provided one on purpose in case someone passes null, the insightsClient would be null and not noop, but that's not very different

Copy link
Member Author

@sarahdayan sarahdayan Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it should stay nullable (e.g., when opting-out of auto-pull)?

onEvent(
event,
insightsClientWithLocalCredentials as TInsightsClient
);
} else if (event.insightsMethod) {
insightsClient(event.insightsMethod, event.payload);
insightsClientWithLocalCredentials(
event.insightsMethod,
event.payload
);

warning(
Boolean((helper.state as PlainSearchParameters).userToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ describe('refinementList', () => {
},
widgetType: 'ais.hierarchicalMenu',
},
null
expect.any(Function)
);

await wait(0);
Expand All @@ -393,7 +393,7 @@ describe('refinementList', () => {
},
widgetType: 'ais.hierarchicalMenu',
},
null
expect.any(Function)
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('hits', () => {
},
widgetType: 'ais.hits',
},
null
expect.any(Function)
);
});

Expand Down Expand Up @@ -157,7 +157,7 @@ describe('hits', () => {
},
widgetType: 'ais.hits',
},
null
expect.any(Function)
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ describe('infiniteHits', () => {
},
widgetType: 'ais.infiniteHits',
},
null
expect.any(Function)
);
});

Expand Down Expand Up @@ -405,7 +405,7 @@ describe('infiniteHits', () => {
},
widgetType: 'ais.infiniteHits',
},
null
expect.any(Function)
);
});

Expand Down
Loading