diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fbbe432f0..c6db4c16e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ Our versioning strategy is as follows: * `[templates/nextjs-sxa]` Fix feature `show Grid column` in Experience Editor. ([#1704](https://github.com/Sitecore/jss/pull/1704)) * `[sitecore-jss-nextjs] [templates/nextjs-xmcloud]` SDK initialization rejections are now correctly handled. Errors should no longer occur after getSDK() promises resolve when they shouldn't (for example, getting Events SDK in development environment) ([#1712](https://github.com/Sitecore/jss/pull/1712) [#1715](https://github.com/Sitecore/jss/pull/1715) [#1716](https://github.com/Sitecore/jss/pull/1716)) * `[sitecore-jss-nextjs]` Fix redirects middleware for working with absolute url where is using site language context ([#1727](https://github.com/Sitecore/jss/pull/1727)) ([#1737](https://github.com/Sitecore/jss/pull/1737)) -* `[sitecore-jss]` Enable the Layout and dictionary service to use custom `retryStrategy`. ([#1749](https://github.com/Sitecore/jss/pull/1749)) +* `[sitecore-jss]` Enable the Layout and dictionary service to use custom `retryStrategy`. ([#1749](https://github.com/Sitecore/jss/pull/1749)) ([#1751](https://github.com/Sitecore/jss/pull/1751)) * `[templates/nextjs-sxa]` Fix base styles of SXA components. Remove conflicted styles of BasicSite template. ([#1757](https://github.com/Sitecore/jss/pull/1757)) ### 🛠 Breaking Changes diff --git a/packages/sitecore-jss/src/graphql-request-client.test.ts b/packages/sitecore-jss/src/graphql-request-client.test.ts index 9551395371..1e8464c9fc 100644 --- a/packages/sitecore-jss/src/graphql-request-client.test.ts +++ b/packages/sitecore-jss/src/graphql-request-client.test.ts @@ -4,7 +4,11 @@ import { expect, use, spy } from 'chai'; import sinon from 'sinon'; import spies from 'chai-spies'; import nock from 'nock'; -import { GraphQLRequestClient, DefaultRetryStrategy } from './graphql-request-client'; +import { + GraphQLRequestClient, + DefaultRetryStrategy, + GraphQLRequestClientConfig, +} from './graphql-request-client'; import { ClientError } from 'graphql-request'; import debugApi from 'debug'; import debug from './debug'; @@ -169,6 +173,31 @@ describe('GraphQLRequestClient', () => { }); describe('Working with retryer', () => { + it('should use the clientConfig values configured by the client', () => { + const clientConfig: GraphQLRequestClientConfig = { + retries: 4, + retryStrategy: { + getDelay: () => 1000, + shouldRetry: () => true, + }, + }; + + const graphQLClient = new GraphQLRequestClient(endpoint, clientConfig); + + expect(graphQLClient['retries']).to.equal(clientConfig.retries); + expect(graphQLClient['retryStrategy']).to.deep.equal(clientConfig.retryStrategy); + }); + + it('should fallback to use default values when clientConfig is undefined', () => { + const clientConfig = { retries: undefined, retryStrategy: undefined }; + const graphQLClient = new GraphQLRequestClient(endpoint, clientConfig); + + expect(graphQLClient['retries']).to.equal(0); + expect(graphQLClient['retryStrategy']).to.deep.equal( + new DefaultRetryStrategy({ statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524] }) + ); + }); + it('should use retry and throw error when retries specified', async function() { this.timeout(8000); nock('http://jssnextweb') diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts index 7cd8260b4a..ec87a85323 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts @@ -1,4 +1,6 @@ +/* eslint-disable no-unused-expressions */ import { expect } from 'chai'; +import sinon, { SinonSpy } from 'sinon'; import nock from 'nock'; import { SitecoreTemplateId } from '../constants'; import { GraphQLClient, GraphQLRequestClient } from '../graphql-request-client'; @@ -274,4 +276,26 @@ describe('GraphQLDictionaryService', () => { // eslint-disable-next-line no-unused-expressions expect(graphQLRequestClient).to.exist; }); + + it('should call clientFactory with the correct arguments', () => { + const clientFactorySpy: SinonSpy = sinon.spy(); + const mockServiceConfig = { + siteName: 'supersite', + clientFactory: clientFactorySpy, + retries: 3, + retryStrategy: { + getDelay: () => 1000, + shouldRetry: () => true, + }, + }; + + new GraphQLDictionaryService(mockServiceConfig); + + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + expect(calledWithArgs.debugger).to.exist; + expect(calledWithArgs.retries).to.equal(mockServiceConfig.retries); + expect(calledWithArgs.retryStrategy).to.deep.equal(mockServiceConfig.retryStrategy); + }); }); diff --git a/packages/sitecore-jss/src/layout/graphql-layout-service.test.ts b/packages/sitecore-jss/src/layout/graphql-layout-service.test.ts index a85623f0fb..4813fb4e5c 100644 --- a/packages/sitecore-jss/src/layout/graphql-layout-service.test.ts +++ b/packages/sitecore-jss/src/layout/graphql-layout-service.test.ts @@ -1,4 +1,6 @@ +/* eslint-disable no-unused-expressions */ import { expect, use } from 'chai'; +import sinon, { SinonSpy } from 'sinon'; import spies from 'chai-spies'; import nock from 'nock'; import { GraphQLLayoutService } from './graphql-layout-service'; @@ -294,4 +296,26 @@ describe('GraphQLLayoutService', () => { expect(error.response.error).to.equal('whoops'); }); }); + + it('should call clientFactory with the correct arguments', () => { + const clientFactorySpy: SinonSpy = sinon.spy(); + const mockServiceConfig = { + siteName: 'supersite', + clientFactory: clientFactorySpy, + retries: 3, + retryStrategy: { + getDelay: () => 1000, + shouldRetry: () => true, + }, + }; + + new GraphQLLayoutService(mockServiceConfig); + + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + expect(calledWithArgs.debugger).to.exist; + expect(calledWithArgs.retries).to.equal(mockServiceConfig.retries); + expect(calledWithArgs.retryStrategy).to.deep.equal(mockServiceConfig.retryStrategy); + }); }); diff --git a/packages/sitecore-jss/src/site/graphql-error-pages-service.test.ts b/packages/sitecore-jss/src/site/graphql-error-pages-service.test.ts index c8c79c1498..e51cdbdd0a 100644 --- a/packages/sitecore-jss/src/site/graphql-error-pages-service.test.ts +++ b/packages/sitecore-jss/src/site/graphql-error-pages-service.test.ts @@ -1,4 +1,6 @@ +/* eslint-disable no-unused-expressions */ import { expect } from 'chai'; +import sinon, { SinonSpy } from 'sinon'; import nock from 'nock'; import { ErrorPages, GraphQLErrorPagesService } from './graphql-error-pages-service'; import { siteNameError } from '../constants'; @@ -118,4 +120,27 @@ describe('GraphQLErrorPagesService', () => { return expect(nock.isDone()).to.be.true; }); }); + + it('should call clientFactory with the correct arguments', () => { + const clientFactorySpy: SinonSpy = sinon.spy(); + const mockServiceConfig = { + siteName: 'supersite', + language, + clientFactory: clientFactorySpy, + retries: 3, + retryStrategy: { + getDelay: () => 1000, + shouldRetry: () => true, + }, + }; + + new GraphQLErrorPagesService(mockServiceConfig); + + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + expect(calledWithArgs.debugger).to.exist; + expect(calledWithArgs.retries).to.equal(mockServiceConfig.retries); + expect(calledWithArgs.retryStrategy).to.deep.equal(mockServiceConfig.retryStrategy); + }); });