Skip to content

Commit af8e677

Browse files
committed
Pr changes and refactor
1 parent 0d57657 commit af8e677

File tree

1 file changed

+144
-47
lines changed

1 file changed

+144
-47
lines changed

packages/quantic/force-app/main/default/lwc/quanticDocumentSuggestion/__tests__/quanticDocumentSuggestion.test.js

Lines changed: 144 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,27 @@ const documentSuggestionListMock = {
2929
}),
3030
};
3131

32-
// @ts-ignore
33-
global.CoveoHeadlessCaseAssist = {
32+
const functionsMocks = {
33+
fetchDocumentSuggestions: jest.fn(),
34+
logDocumentSuggestionClick: jest.fn(),
35+
logDocumentSuggestionRating: jest.fn(),
36+
};
37+
38+
const headlessCaseAssistMock = {
3439
buildDocumentSuggestionList: jest
3540
.fn()
3641
.mockReturnValue(documentSuggestionListMock),
3742
loadCaseAssistAnalyticsActions: jest.fn().mockReturnValue({}),
3843
loadDocumentSuggestionActions: jest.fn().mockReturnValue({
39-
fetchDocumentSuggestions: jest
40-
.fn()
41-
.mockReturnValue('mockedFetchDocumentSuggestions'),
42-
logDocumentSuggestionClick: jest.fn((id) => [
43-
'mockedLogDocumentSuggestionClick',
44-
id,
45-
]),
46-
logDocumentSuggestionRating: jest.fn((id, score) => [
47-
'mockedLogDocumentSuggestionRating',
48-
id,
49-
score,
50-
]),
44+
fetchDocumentSuggestions: functionsMocks.fetchDocumentSuggestions,
45+
logDocumentSuggestionClick: functionsMocks.logDocumentSuggestionClick,
46+
logDocumentSuggestionRating: functionsMocks.logDocumentSuggestionRating,
5147
}),
5248
};
5349

50+
// @ts-ignore
51+
global.CoveoHeadlessCaseAssist = headlessCaseAssistMock;
52+
5453
const createTestComponent = buildCreateTestComponent(
5554
QuanticDocumentSuggestion,
5655
'c-quantic-document-suggestion',
@@ -69,6 +68,16 @@ const selectors = {
6968
componentError: 'c-quantic-component-error',
7069
};
7170

71+
function mockErroneousHeadlessInitialization() {
72+
headlessLoaderMock.initializeWithHeadless.mockImplementation(
73+
async (element) => {
74+
if (element instanceof QuanticDocumentSuggestion) {
75+
element.setInitializationError();
76+
}
77+
}
78+
);
79+
}
80+
7281
function checkElement(element, selector, shouldExist) {
7382
if (shouldExist) {
7483
expect(element.shadowRoot.querySelector(selector)).toBeTruthy();
@@ -89,6 +98,30 @@ describe('c-quantic-document-suggestion', () => {
8998
cleanup();
9099
});
91100

101+
describe('on initialization', () => {
102+
it('should initialize the component and subscribe to the controller', async () => {
103+
createTestComponent();
104+
105+
expect(headlessLoaderMock.registerComponentForInit).toHaveBeenCalled();
106+
expect(headlessLoaderMock.initializeWithHeadless).toHaveBeenCalled();
107+
expect(
108+
headlessCaseAssistMock.buildDocumentSuggestionList
109+
).toHaveBeenCalledTimes(1);
110+
expect(
111+
headlessCaseAssistMock.buildDocumentSuggestionList
112+
).toHaveBeenCalledWith(engineMock);
113+
expect(
114+
headlessCaseAssistMock.loadCaseAssistAnalyticsActions
115+
).toHaveBeenCalledTimes(1);
116+
expect(
117+
headlessCaseAssistMock.loadDocumentSuggestionActions
118+
).toHaveBeenCalledTimes(1);
119+
expect(engineMock.dispatch).not.toHaveBeenCalled();
120+
expect(documentSuggestionListMock.subscribe).toHaveBeenCalledTimes(1);
121+
expect(updateSuggestions).toBeDefined();
122+
});
123+
});
124+
92125
describe('when loading suggestions', () => {
93126
it('should render the loading holder only', async () => {
94127
const element = createTestComponent();
@@ -107,8 +140,6 @@ describe('c-quantic-document-suggestion', () => {
107140
const element = createTestComponent();
108141
await updateSuggestions();
109142

110-
expect(engineMock.dispatch).not.toHaveBeenCalled();
111-
expect(headlessLoaderMock.registerComponentForInit).toHaveBeenCalled();
112143
checkElement(element, selectors.accordion, false);
113144
checkElement(element, selectors.noSuggestions, true);
114145
});
@@ -121,9 +152,7 @@ describe('c-quantic-document-suggestion', () => {
121152
});
122153
await flushPromises();
123154

124-
expect(engineMock.dispatch).toHaveBeenCalledWith(
125-
'mockedFetchDocumentSuggestions'
126-
);
155+
expect(functionsMocks.fetchDocumentSuggestions).toHaveBeenCalledTimes(1);
127156
});
128157
});
129158

@@ -148,82 +177,97 @@ describe('c-quantic-document-suggestion', () => {
148177
it('should render the component and all parts with default options', async () => {
149178
const element = createTestComponent();
150179
await flushPromises();
180+
expect(functionsMocks.fetchDocumentSuggestions).not.toHaveBeenCalled();
181+
expect(headlessLoaderMock.registerComponentForInit).toHaveBeenCalled();
151182

152183
checkElement(element, selectors.accordion, false);
153184
checkElement(element, selectors.noSuggestions, true);
154185
checkElement(element, selectors.componentError, false);
155186

156187
await updateSuggestions();
157188

158-
expect(engineMock.dispatch).not.toHaveBeenCalled();
159-
expect(headlessLoaderMock.registerComponentForInit).toHaveBeenCalled();
160-
161189
checkElement(element, selectors.noSuggestions, false);
162190
checkElement(element, selectors.container, true);
163191
checkElement(element, selectors.accordion, true);
164192
const sections = element.shadowRoot.querySelectorAll(
165193
selectors.accordionSection
166194
);
167195
expect(sections.length).toBe(2);
168-
expect(sections[0].getAttribute('data-id')).toBe('ego');
169-
checkElement(element, selectors.quickview, true);
196+
sections.forEach((section, index) => {
197+
expect(section.label).toBe(
198+
documentSuggestionListMock.state.documents[index].title
199+
);
200+
const uniqueId =
201+
documentSuggestionListMock.state.documents[index].uniqueId;
202+
expect(section.getAttribute('data-id')).toBe(uniqueId);
203+
expect(section.querySelector(selectors.quickview)).toBeTruthy();
204+
expect(
205+
section
206+
.querySelector('slot[name="actions"]')
207+
.getAttribute('data-doc-id')
208+
).toBe(uniqueId);
209+
expect(
210+
section
211+
.querySelector('slot[name="rating"]')
212+
.getAttribute('data-doc-id')
213+
).toBe(uniqueId);
214+
});
170215
});
171216

172217
it('should not render quick view button when withoutQuickview is set to true', async () => {
173218
const element = createTestComponent({
174219
withoutQuickview: true,
175-
fetchOnInit: true,
176220
});
177-
await flushPromises();
221+
await updateSuggestions();
178222

179223
expect(element.shadowRoot.querySelector(selectors.quickview)).toBeFalsy();
180224
});
181225

182226
it('should log a suggestion click if not opened', async () => {
183-
const element = createTestComponent({
184-
fetchOnInit: true,
185-
});
227+
const element = createTestComponent();
186228
await updateSuggestions();
187229

188-
const accordion = element.shadowRoot.querySelector(selectors.accordion);
189230
const sections = element.shadowRoot.querySelectorAll(
190231
selectors.accordionSection
191232
);
233+
const accordion = element.shadowRoot.querySelector(selectors.accordion);
192234

193-
// Should not log click when opening first
235+
// First document is already opened and will be collapsed, no click event logged.
194236
sections[0].click();
195-
expect(engineMock.dispatch).not.toHaveBeenCalledWith(
196-
'mockedLogDocumentSuggestionClick'
237+
expect(functionsMocks.logDocumentSuggestionClick).toHaveBeenCalledTimes(
238+
0
197239
);
198-
199240
accordion.activeSectionName = sections[1].name;
241+
200242
sections[1].click();
201-
expect(engineMock.dispatch).toHaveBeenCalledWith([
202-
'mockedLogDocumentSuggestionClick',
203-
sections[1].name,
204-
]);
243+
await flushPromises();
244+
expect(functionsMocks.logDocumentSuggestionClick).toHaveBeenCalledTimes(
245+
1
246+
);
247+
expect(functionsMocks.logDocumentSuggestionClick).toHaveBeenCalledWith(
248+
documentSuggestionListMock.state.documents[1].uniqueId
249+
);
205250
});
206251

207252
it('should handle document rating event', async () => {
208-
const element = createTestComponent({
209-
fetchOnInit: true,
210-
});
253+
const element = createTestComponent();
211254
await updateSuggestions();
212255

213256
const customEvent = new CustomEvent('quantic__rating', {
214257
detail: {id: 'mastercraft', score: 2},
215258
});
216259
element.shadowRoot.dispatchEvent(customEvent);
217-
expect(engineMock.dispatch).toHaveBeenCalledWith([
218-
'mockedLogDocumentSuggestionRating',
260+
expect(functionsMocks.logDocumentSuggestionRating).toHaveBeenCalledTimes(
261+
1
262+
);
263+
expect(functionsMocks.logDocumentSuggestionRating).toHaveBeenCalledWith(
219264
'mastercraft',
220-
2,
221-
]);
265+
2
266+
);
222267
});
223268

224-
it('should truncate documents to maxDocuments', async () => {
269+
it('should truncate documents to maxDocuments if the state contains more', async () => {
225270
const element = createTestComponent({
226-
fetchOnInit: true,
227271
maxDocuments: 1,
228272
});
229273
await updateSuggestions();
@@ -253,6 +297,46 @@ describe('c-quantic-document-suggestion', () => {
253297
});
254298
});
255299

300+
describe('when two document suggestions have a similar title', () => {
301+
it('should open the right document suggestion when clicked', async () => {
302+
documentSuggestionListMock.state.documents = [
303+
{
304+
title: 'dewalt',
305+
uniqueId: 'dw745',
306+
fields: {
307+
uri: 'table saw',
308+
},
309+
},
310+
{
311+
title: 'dewalt',
312+
uniqueId: 'dw560',
313+
fields: {
314+
uri: 'router',
315+
},
316+
},
317+
];
318+
319+
const element = createTestComponent();
320+
await updateSuggestions();
321+
322+
const sectionToClick = element.shadowRoot.querySelectorAll(
323+
selectors.accordionSection
324+
)[1];
325+
const accordion = element.shadowRoot.querySelector(selectors.accordion);
326+
accordion.activeSectionName = sectionToClick.name;
327+
sectionToClick.click();
328+
expect(sectionToClick.name).toBe(
329+
documentSuggestionListMock.state.documents[1].uniqueId
330+
);
331+
expect(functionsMocks.logDocumentSuggestionClick).toHaveBeenCalledTimes(
332+
1
333+
);
334+
expect(functionsMocks.logDocumentSuggestionClick).toHaveBeenCalledWith(
335+
sectionToClick.name
336+
);
337+
});
338+
});
339+
256340
describe('with invalid options', () => {
257341
it('should set an error message if maxDocuments is invalid', async () => {
258342
const element = createTestComponent({
@@ -271,4 +355,17 @@ describe('c-quantic-document-suggestion', () => {
271355
checkElement(element, selectors.componentError, true);
272356
});
273357
});
358+
359+
describe('when an initialization error occurs', () => {
360+
beforeEach(() => {
361+
mockErroneousHeadlessInitialization();
362+
});
363+
364+
it('should display the initialization error component', async () => {
365+
const element = createTestComponent();
366+
await flushPromises();
367+
368+
checkElement(element, selectors.componentError, true);
369+
});
370+
});
274371
});

0 commit comments

Comments
 (0)