Skip to content

Commit 5813a2d

Browse files
[sitecore-jss] [Dictionary] Return empty dictionary when siteInfo/dictionary is empty
1 parent 88aa6dd commit 5813a2d

4 files changed

+94
-9
lines changed

packages/sitecore-jss/src/editing/graphql-editing-service.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,55 @@ describe('GraphQLEditingService', () => {
233233
spy.restore(clientFactorySpy);
234234
});
235235

236+
it('should return empty dictionary when dictionary is not provided', async () => {
237+
const editingData = mockEditingServiceResponse();
238+
239+
(editingData.data.site.siteInfo as any) = null;
240+
241+
nock(hostname, { reqheaders: { sc_editMode: 'true' } })
242+
.post(endpointPath, /EditingQuery/gi)
243+
.reply(200, editingData);
244+
245+
const clientFactorySpy = sinon.spy(clientFactory);
246+
247+
const service = new GraphQLEditingService({
248+
clientFactory: clientFactorySpy,
249+
});
250+
251+
spy.on(clientFactorySpy.returnValues[0], 'request');
252+
253+
const result = await service.fetchEditingData({
254+
language,
255+
version,
256+
itemId,
257+
siteName,
258+
});
259+
260+
expect(clientFactorySpy.calledOnce).to.be.true;
261+
expect(
262+
clientFactorySpy.calledWith({
263+
debugger: debug.editing,
264+
headers: {
265+
sc_editMode: 'true',
266+
},
267+
})
268+
).to.be.true;
269+
expect(clientFactorySpy.returnValues[0].request).to.be.called.exactly(1);
270+
expect(clientFactorySpy.returnValues[0].request).to.be.called.with(query, {
271+
language,
272+
version,
273+
itemId,
274+
siteName,
275+
});
276+
277+
expect(result).to.deep.equal({
278+
layoutData: layoutDataResponse,
279+
dictionary: {},
280+
});
281+
282+
spy.restore(clientFactorySpy);
283+
});
284+
236285
it('should throw an error when client factory is not provided', async () => {
237286
try {
238287
const service = new GraphQLEditingService({} as GraphQLEditingServiceConfig);

packages/sitecore-jss/src/editing/graphql-editing-service.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,13 @@ export class GraphQLEditingService {
134134
language,
135135
});
136136

137-
dictionaryResults = editingData.site.siteInfo.dictionary.results;
138-
hasNext = editingData.site.siteInfo.dictionary.pageInfo.hasNext;
139-
after = editingData.site.siteInfo.dictionary.pageInfo.endCursor;
137+
if (editingData?.site?.siteInfo?.dictionary) {
138+
dictionaryResults = editingData.site.siteInfo.dictionary.results;
139+
hasNext = editingData.site.siteInfo.dictionary.pageInfo.hasNext;
140+
after = editingData.site.siteInfo.dictionary.pageInfo.endCursor;
141+
} else {
142+
hasNext = false;
143+
}
140144

141145
while (hasNext) {
142146
const data = await this.graphQLClient.request<GraphQLDictionaryQueryResponse>(
@@ -147,9 +151,14 @@ export class GraphQLEditingService {
147151
after,
148152
}
149153
);
150-
dictionaryResults = dictionaryResults.concat(data.site.siteInfo.dictionary.results);
151-
hasNext = data.site.siteInfo.dictionary.pageInfo.hasNext;
152-
after = data.site.siteInfo.dictionary.pageInfo.endCursor;
154+
155+
if (data?.site?.siteInfo?.dictionary) {
156+
dictionaryResults = dictionaryResults.concat(data.site.siteInfo.dictionary.results);
157+
hasNext = data.site.siteInfo.dictionary.pageInfo.hasNext;
158+
after = data.site.siteInfo.dictionary.pageInfo.endCursor;
159+
} else {
160+
hasNext = false;
161+
}
153162
}
154163

155164
dictionaryResults.forEach((item) => (dictionary[item.key] = item.value));

packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,27 @@ describe('GraphQLDictionaryService', () => {
353353
expect(error.response.error).to.equal('whoops');
354354
});
355355
});
356+
357+
it('should return empty result when no dictionary entries found', async () => {
358+
nock(endpoint)
359+
.post('/')
360+
.reply(200, {
361+
data: {
362+
site: {
363+
siteInfo: null,
364+
},
365+
},
366+
});
367+
368+
const service = new GraphQLDictionaryService({
369+
clientFactory,
370+
siteName,
371+
cacheEnabled: false,
372+
useSiteQuery: true,
373+
});
374+
375+
const result = await service.fetchDictionaryData('en');
376+
expect(result).to.deep.equal({});
377+
});
356378
});
357379
});

packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,15 @@ export class GraphQLDictionaryService extends DictionaryServiceBase {
236236
}
237237
);
238238

239-
results = results.concat(fetchResponse?.site.siteInfo.dictionary.results);
240-
hasNext = fetchResponse.site.siteInfo.dictionary.pageInfo.hasNext;
241-
after = fetchResponse.site.siteInfo.dictionary.pageInfo.endCursor;
239+
if (fetchResponse?.site?.siteInfo?.dictionary) {
240+
results = results.concat(fetchResponse.site.siteInfo.dictionary.results);
241+
after = fetchResponse.site.siteInfo.dictionary.pageInfo.endCursor;
242+
hasNext = fetchResponse.site.siteInfo.dictionary.pageInfo.hasNext;
243+
} else {
244+
hasNext = false;
245+
}
242246
}
247+
243248
results.forEach((item) => (phrases[item.key] = item.value));
244249

245250
return phrases;

0 commit comments

Comments
 (0)