Skip to content

Commit

Permalink
fix(quantic): avoid pushing to recent results when use case is not se…
Browse files Browse the repository at this point in the history
…arch (#3731)

### [SFINT-5456](https://coveord.atlassian.net/browse/SFINT-5456)

The recent results feature is not yet supported for the insight use
case, I consider this as a temporary fix.
A real improvement to improve this would be the following:

- Make the necessary updates in the Headless library to support this
feature in the insight use case, we should also make the feature about
the recent queries list available for the insight use case.
- Improve the push recent action in headless v3, for now it takes the
whole result as a parameter, while just sending the unique id should be
enough to find the result in the Headless state.

#### BEFORE


https://github.com/coveo/ui-kit/assets/86681870/1d9dbd5f-ba2e-496f-b1cb-caee4d416708

#### AFTER


https://github.com/coveo/ui-kit/assets/86681870/cdea0d77-5d92-4739-85e1-9ba396b7e0ce



[SFINT-5456]:
https://coveord.atlassian.net/browse/SFINT-5456?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Sylvie Allain <[email protected]>
Co-authored-by: Etienne Rocheleau <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent f8c2a9c commit 5166de0
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import * as mockHeadlessLoader from 'c/quanticHeadlessLoader';
// @ts-ignore
import {createElement} from 'lwc';
import QuanticResultQuickview from '../quanticResultQuickview';

const selectors = {
quickViewButton: '[data-cy="quick-view-button"]',
};

jest.mock('c/quanticHeadlessLoader');
jest.mock(
'@salesforce/label/c.quantic_OpenPreview',
() => ({default: 'Open preview'}),
{
virtual: true,
}
);
jest.mock(
'@salesforce/label/c.quantic_OpenFileForPreview',
() => ({default: 'Open file for preview'}),
{
virtual: true,
}
);
jest.mock(
'@salesforce/label/c.quantic_NoPreviewAvailable',
() => ({default: 'No preview available'}),
{
virtual: true,
}
);
jest.mock('@salesforce/label/c.quantic_Close', () => ({default: 'Close'}), {
virtual: true,
});

const exampleResult = {
raw: {
date: '123',
},
title: 'abc',
uniqueId: '123',
};

function createTestComponent(options = {result: exampleResult}) {
const element = createElement('c-quantic-result-quick-view', {
is: QuanticResultQuickview,
});

if (options) {
for (const [key, value] of Object.entries(options)) {
element[key] = value;
}
}

document.body.appendChild(element);
return element;
}

// Helper function to wait until the microtask queue is empty.
function flushPromises() {
// eslint-disable-next-line @lwc/lwc/no-async-operation
return new Promise((resolve) => setTimeout(resolve, 0));
}

const functionMocks = {
pushRecentResult: jest.fn(),
};

let useCase = 'search';

function mockHeadless() {
jest.spyOn(mockHeadlessLoader, 'getHeadlessBundle').mockReturnValue({
buildQuickview: () => ({
state: {resultHasPreview: true},
fetchResultContent: jest.fn(),
subscribe: jest.fn((callback) => {
callback();
}),
cancelPendingSelect: jest.fn(),
}),
buildInteractiveResult: jest.fn(),
loadRecentResultsActions: () => ({
pushRecentResult: functionMocks.pushRecentResult,
}),
});

jest
.spyOn(mockHeadlessLoader, 'isHeadlessBundle')
.mockReturnValue(useCase === 'search');

jest.spyOn(mockHeadlessLoader, 'getHeadlessEnginePromise').mockReturnValue(
new Promise((resolve) => {
resolve({
dispatch: jest.fn(),
});
})
);
}

function mockBueno() {
jest.spyOn(mockHeadlessLoader, 'getBueno').mockReturnValue(
new Promise(() => {
// @ts-ignore
global.Bueno = {
isString: jest
.fn()
.mockImplementation(
(value) =>
Object.prototype.toString.call(value) === '[object String]'
),
};
})
);
}

function cleanup() {
// The jsdom instance is shared across test cases in a single file so reset the DOM.
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
jest.clearAllMocks();
}

describe('c-quantic-result-quick-view', () => {
beforeEach(() => {
mockHeadless();
mockBueno();
});

afterEach(() => {
cleanup();
});

describe('when the component is used in the search use case', () => {
beforeAll(() => {
useCase = 'search';
});

it('should dispatch the pushRecentResult action', async () => {
const element = createTestComponent();
await flushPromises();

const quickViewButton = element.shadowRoot.querySelector(
selectors.quickViewButton
);
await quickViewButton.click();
await flushPromises();

expect(functionMocks.pushRecentResult).toHaveBeenCalled();
});
});

describe('when the component is not used in the search use case', () => {
beforeAll(() => {
useCase = 'insight';
});

it('should not dispatch the pushRecentResult action', async () => {
const element = createTestComponent();
await flushPromises();

const quickViewButton = element.shadowRoot.querySelector(
selectors.quickViewButton
);
await quickViewButton.click();
await flushPromises();

expect(functionMocks.pushRecentResult).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class QuanticResultQuickview extends LightningElement {
openQuickview() {
this.isQuickviewOpen = true;
this._isLoading = true;
if (!isHeadlessBundle(this.engineId, HeadlessBundleNames.caseAssist)) {
if (isHeadlessBundle(this.engineId, HeadlessBundleNames.search)) {
this.addRecentResult();
}
this.quickview.fetchResultContent();
Expand Down

0 comments on commit 5166de0

Please sign in to comment.