-
Notifications
You must be signed in to change notification settings - Fork 36
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
test(quantic): document suggestion jest tests #5146
base: master
Are you sure you want to change the base?
Conversation
Pull Request ReportPR Title✅ Title follows the conventional commit spec. Live demo linksBundle Size
|
d04c4f9
to
0d57657
Compare
updateSuggestions(); | ||
await flushPromises(); | ||
|
||
expect(engineMock.dispatch).not.toHaveBeenCalled(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which action is the one we are checking that it is not dispatched here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetchSuggestion especially!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in that case wouldn't be better to have expect(fetchSuggestion).not.toHaveBeenCalled();
?
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Show resolved
Hide resolved
...e-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice tests!
looks good to me after addressing the issue of the polluting logs!
headlessLoaderMock.getHeadlessEnginePromise.mockImplementation(() => | ||
Promise.reject({message: 'Skip initialization'}) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is causing the following which is polluting the logs: https://github.com/coveo/ui-kit/actions/runs/14365598472/job/40277925597?pr=5146
We need to address this, a suggestion could be to handle initialization like we do for other components.
loadCaseAssistAnalyticsActions: jest.fn().mockReturnValue({}), | ||
loadDocumentSuggestionActions: jest.fn().mockReturnValue({ | ||
fetchDocumentSuggestions: functionsMocks.fetchDocumentSuggestions, | ||
logDocumentSuggestionClick: functionsMocks.logDocumentSuggestionClick, | ||
logDocumentSuggestionRating: functionsMocks.logDocumentSuggestionRating, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To stay lawful to the Headless implementation, logDocumentSuggestionClick and logDocumentSuggestionRating should come from loadCaseAssistAnalyticsActions
and not loadDocumentSuggestionActions
:
ui-kit/packages/headless/src/features/case-assist/case-assist-analytics-actions-loader.ts
Lines 116 to 130 in 77feade
export function loadCaseAssistAnalyticsActions( | |
engine: CaseAssistEngine | |
): CaseAssistAnalyticsActionCreators { | |
engine.addReducers({}); | |
return { | |
logCaseStart, | |
logCaseNextStage, | |
logCreateCase, | |
logSolveCase, | |
logAbandonCase, | |
logUpdateCaseField, | |
logClassificationClick, | |
logDocumentSuggestionClick, | |
logDocumentSuggestionRating, | |
logQuickviewDocumentSuggestionClick, |
const sections = element.shadowRoot.querySelectorAll( | ||
selectors.accordionSection | ||
); | ||
expect(sections.length).toBe(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an improvements could be to store the suggestions in a variable exampleSuggestions
.
And then here instead of comparing to 2
, we compare to exampleSuggestions.length
This case our test become a bit more scalalble, if in the future we need to add a new test case where we need three document suggestions for example, we would only care about changing exampleSuggestions
, the rest will follow smoothly.
expect(section.label).toBe( | ||
documentSuggestionListMock.state.documents[index].title | ||
); | ||
const uniqueId = | ||
documentSuggestionListMock.state.documents[index].uniqueId; | ||
expect(section.getAttribute('data-id')).toBe(uniqueId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(section.label).toBe( | |
documentSuggestionListMock.state.documents[index].title | |
); | |
const uniqueId = | |
documentSuggestionListMock.state.documents[index].uniqueId; | |
expect(section.getAttribute('data-id')).toBe(uniqueId); | |
sections.forEach((section, index) => { | |
const {title, uniqueId} = documentSuggestionListMock.state.documents[index]; | |
expect(section.label).toBe(title); | |
expect(section.getAttribute('data-id')).toBe(uniqueId); |
expect(element.shadowRoot.querySelector(selectors.quickview)).toBeFalsy(); | ||
}); | ||
|
||
it('should log a suggestion click if not opened', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should log a suggestion click if not opened', async () => { | |
it('should log a suggestion click if the suggestion is not opened', async () => { |
}); | ||
}); | ||
|
||
describe('when two document suggestions have a similar title', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cypress test do this with a component that has numberOfAutoOpenedDocuments
set to 0:
ui-kit/packages/quantic/cypress/e2e/default-2/document-suggestion/document-suggestion.cypress.ts
Lines 210 to 215 in 5946a9e
describe('when two document suggestions have a similar title', () => { | |
it('should open the right document suggestion when clicked', () => { | |
const exampleResponseId = crypto.randomUUID(); | |
visitDocumentSuggestion({ | |
numberOfAutoOpenedDocuments: 0, | |
}); |
I think we should do the same here, as this cypress test was added after a bug that we fixed, so we need to replicate exactly the stup that was causing the issue previously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
); | ||
} | ||
|
||
function checkElement(element, selector, shouldExist) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be in a utils?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally don't prefer the usage of such functions, cause they lead to the absence of assertions in the tests which for more is not very ideal.
I prefer that when I read the test, I instantly understand what's going on and what's being tested without having to check for function definitions that are defined in the same file or even less ideal when defined in separate files.
I know the benefit of such functions is to reduce code duplication and follow the DRY mindset (Don't repeat yourself ).
But for me DRY is less applicable and less beneficial when it comes to test writing, for tests and for me, it's more about clarity and simplicity, even if it includes some code duplication like here a duplication of the expect
assertion that checks that an element is present.
Thoughts? @mikegron @SimonMilord @erocheleau
}); | ||
}); | ||
|
||
it('should not render quick view button when withoutQuickview is set to true', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should not render quick view button when withoutQuickview is set to true', async () => { | |
it('should not render quickview button when withoutQuickview is set to true', async () => { |
); | ||
}); | ||
|
||
it('should handle document rating event', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should handle document rating event', async () => { | |
it('should log analytics when rating a document', async () => { |
SFINT-5793
Created Jest tests for Document suggestion component. Had some troubles testing some things but should be good now lol
Some things maybe different as I was trying to follow jest docs:
prepareHeadlessState
andmockSuccessfulHeadlessInitialization
I didn't really like the formatting but I hope you have suggestions! For instance, the

global.CoveoHeadlessCaseAssist
mock could probably be a utility.