-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(quantic): Exposed the options to be be passed in Did You Mean co…
…ntroller in the QuanticDidYouMean component (#4682) [SFINT-5681](https://coveord.atlassian.net/browse/SFINT-5681) ## IN THIS PR: - Exposed `disableQueryAutoCorrection` and `queryCorrectionMode` properties in the `QuanticDidYouMean` component. - Added unit tests - Fixed issue with props in the controller of the did-you-mean feature for insight Notes: - This needs to be merge after this [PR](#4598) - We are setting `queryCorrectionMode` to legacy to avoid breaking changes. This will be changed after the next major release, we have the jira tickets to eventually change that. ## TESTS: <img width="397" alt="image" src="https://github.com/user-attachments/assets/a2ae1ee1-a145-465a-a25b-c7623710f2f0"> [SFINT-5681]: https://coveord.atlassian.net/browse/SFINT-5681?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
1 parent
4700df2
commit 4ccf316
Showing
3 changed files
with
235 additions
and
6 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
199 changes: 199 additions & 0 deletions
199
.../quantic/force-app/main/default/lwc/quanticDidYouMean/__tests__/quanticDidYouMean.test.js
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,199 @@ | ||
/* eslint-disable no-import-assign */ | ||
// @ts-ignore | ||
import {createElement} from 'lwc'; | ||
import QuanticDidYouMean from '../quanticDidYouMean'; | ||
import * as mockHeadlessLoader from 'c/quanticHeadlessLoader'; | ||
|
||
jest.mock('c/quanticHeadlessLoader'); | ||
|
||
let isInitialized = false; | ||
|
||
const exampleEngine = { | ||
id: 'exampleEngineId', | ||
}; | ||
|
||
const defaultQueryCorrectionMode = 'legacy'; | ||
|
||
const defaultOptions = { | ||
engineId: exampleEngine.id, | ||
disableQueryAutoCorrection: false, | ||
queryCorrectionMode: defaultQueryCorrectionMode, | ||
}; | ||
|
||
const mockDidYouMeanState = { | ||
wasCorrectedTo: 'example corrected query', | ||
originalQuery: 'example original query', | ||
wasAutomaticallyCorrected: true, | ||
queryCorrection: { | ||
correctedQuery: 'example corrected query', | ||
wordCorrected: 'example', | ||
}, | ||
hasQueryCorrection: true, | ||
}; | ||
|
||
const functionsMocks = { | ||
buildDidYouMean: jest.fn(() => ({ | ||
state: mockDidYouMeanState, | ||
subscribe: functionsMocks.subscribe, | ||
})), | ||
buildQueryTrigger: jest.fn(() => ({ | ||
state: {}, | ||
subscribe: functionsMocks.subscribe, | ||
})), | ||
applyCorrection: jest.fn(() => {}), | ||
subscribe: jest.fn((cb) => { | ||
cb(); | ||
return functionsMocks.unsubscribe; | ||
}), | ||
unsubscribe: jest.fn(() => {}), | ||
}; | ||
|
||
function prepareHeadlessState() { | ||
// @ts-ignore | ||
mockHeadlessLoader.getHeadlessBundle = () => { | ||
return { | ||
buildDidYouMean: functionsMocks.buildDidYouMean, | ||
buildQueryTrigger: functionsMocks.buildQueryTrigger, | ||
}; | ||
}; | ||
} | ||
|
||
function mockSuccessfulHeadlessInitialization() { | ||
// @ts-ignore | ||
mockHeadlessLoader.initializeWithHeadless = (element, _, initialize) => { | ||
if (element instanceof QuanticDidYouMean && !isInitialized) { | ||
isInitialized = true; | ||
initialize(exampleEngine); | ||
} | ||
}; | ||
} | ||
|
||
function createTestComponent(options = defaultOptions) { | ||
prepareHeadlessState(); | ||
const element = createElement('c-quantic-did-you-mean', { | ||
is: QuanticDidYouMean, | ||
}); | ||
|
||
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. | ||
async function flushPromises() { | ||
return Promise.resolve(); | ||
} | ||
|
||
describe('c-quantic-did-you-mean', () => { | ||
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(); | ||
isInitialized = false; | ||
} | ||
|
||
beforeEach(() => { | ||
mockSuccessfulHeadlessInitialization(); | ||
}); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
describe('controller initialization', () => { | ||
it('should subscribe to the headless state changes', async () => { | ||
createTestComponent(); | ||
await flushPromises(); | ||
|
||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalled(); | ||
expect(functionsMocks.subscribe).toHaveBeenCalled(); | ||
}); | ||
|
||
describe('#disableQueryAutoCorrection property', () => { | ||
describe('when disableQueryAutoCorrection is false (default)', () => { | ||
it('should properly initialize the controller with automatic query correction enabled', async () => { | ||
createTestComponent(); | ||
await flushPromises(); | ||
|
||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledTimes(1); | ||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledWith( | ||
exampleEngine, | ||
expect.objectContaining({ | ||
options: expect.objectContaining({ | ||
automaticallyCorrectQuery: true, | ||
queryCorrectionMode: defaultQueryCorrectionMode, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when disableQueryAutoCorrection is true', () => { | ||
it('should properly initialize the controller with automatic query correction disabled', async () => { | ||
createTestComponent({ | ||
...defaultOptions, | ||
disableQueryAutoCorrection: true, | ||
}); | ||
await flushPromises(); | ||
|
||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledTimes(1); | ||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledWith( | ||
exampleEngine, | ||
expect.objectContaining({ | ||
options: expect.objectContaining({ | ||
automaticallyCorrectQuery: false, | ||
queryCorrectionMode: defaultQueryCorrectionMode, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#queryCorrectionMode property', () => { | ||
describe('when queryCorrectionMode is `legacy` (default)', () => { | ||
it('should properly initialize the controller with the query correction mode set to `legacy`', async () => { | ||
createTestComponent(); | ||
await flushPromises(); | ||
|
||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledTimes(1); | ||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledWith( | ||
exampleEngine, | ||
expect.objectContaining({ | ||
options: expect.objectContaining({ | ||
automaticallyCorrectQuery: true, | ||
queryCorrectionMode: defaultQueryCorrectionMode, | ||
}), | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when queryCorrectionMode is `next`', () => { | ||
it('should properly initialize the controller with the query correction mode set to `next`', async () => { | ||
createTestComponent({ | ||
...defaultOptions, | ||
queryCorrectionMode: 'next', | ||
}); | ||
await flushPromises(); | ||
|
||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledTimes(1); | ||
expect(functionsMocks.buildDidYouMean).toHaveBeenCalledWith( | ||
exampleEngine, | ||
expect.objectContaining({ | ||
options: expect.objectContaining({ | ||
automaticallyCorrectQuery: true, | ||
queryCorrectionMode: 'next', | ||
}), | ||
}) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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