From 3b65acfda18615064068b910a42bc26a9229bda7 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 29 Feb 2024 14:37:28 -0500 Subject: [PATCH 1/6] add unit tests for config values --- .../src/graphql-request-client.test.ts | 31 ++++++++++++++++++- .../src/layout/graphql-layout-service.ts | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/sitecore-jss/src/graphql-request-client.test.ts b/packages/sitecore-jss/src/graphql-request-client.test.ts index 9551395371..8e6b9cc1c1 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 = {}; + 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/layout/graphql-layout-service.ts b/packages/sitecore-jss/src/layout/graphql-layout-service.ts index 131bb37870..b63e80c7dd 100644 --- a/packages/sitecore-jss/src/layout/graphql-layout-service.ts +++ b/packages/sitecore-jss/src/layout/graphql-layout-service.ts @@ -110,7 +110,7 @@ export class GraphQLLayoutService extends LayoutServiceBase { apiKey: this.serviceConfig.apiKey, debugger: debug.layout, retries: this.serviceConfig.retries, - retryStrategy: this.serviceConfig.retryStrategy, + // retryStrategy: this.serviceConfig.retryStrategy, }); } From 9f985e43c6f290785a8d84d148ee7ab82df65ee1 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 29 Feb 2024 14:46:46 -0500 Subject: [PATCH 2/6] update changelog --- CHANGELOG.md | 2 +- packages/sitecore-jss/src/graphql-request-client.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04eb954ddf..8498483dd0 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)) ### 🛠 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 8e6b9cc1c1..1e8464c9fc 100644 --- a/packages/sitecore-jss/src/graphql-request-client.test.ts +++ b/packages/sitecore-jss/src/graphql-request-client.test.ts @@ -189,7 +189,7 @@ describe('GraphQLRequestClient', () => { }); it('should fallback to use default values when clientConfig is undefined', () => { - const clientConfig = {}; + const clientConfig = { retries: undefined, retryStrategy: undefined }; const graphQLClient = new GraphQLRequestClient(endpoint, clientConfig); expect(graphQLClient['retries']).to.equal(0); From d587012421ad5b724cb0d6efb5d7cd413233039e Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 29 Feb 2024 16:19:25 -0500 Subject: [PATCH 3/6] revert commented code --- packages/sitecore-jss/src/layout/graphql-layout-service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/layout/graphql-layout-service.ts b/packages/sitecore-jss/src/layout/graphql-layout-service.ts index b63e80c7dd..131bb37870 100644 --- a/packages/sitecore-jss/src/layout/graphql-layout-service.ts +++ b/packages/sitecore-jss/src/layout/graphql-layout-service.ts @@ -110,7 +110,7 @@ export class GraphQLLayoutService extends LayoutServiceBase { apiKey: this.serviceConfig.apiKey, debugger: debug.layout, retries: this.serviceConfig.retries, - // retryStrategy: this.serviceConfig.retryStrategy, + retryStrategy: this.serviceConfig.retryStrategy, }); } From 77d35df1d094559d1b4b32d12fc68dfc6935821a Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 7 Mar 2024 12:08:41 -0500 Subject: [PATCH 4/6] add test for each service --- .../i18n/graphql-dictionary-service.test.ts | 23 ++++++++++++++++++ .../src/layout/graphql-layout-service.test.ts | 23 ++++++++++++++++++ .../site/graphql-error-pages-service.test.ts | 24 +++++++++++++++++++ 3 files changed, 70 insertions(+) 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..6b5f630224 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,5 @@ 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 +275,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); + + // eslint-disable-next-line no-unused-expressions + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + 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..b89950de64 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,5 @@ 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 +295,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); + + // eslint-disable-next-line no-unused-expressions + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + 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..a5e3e205c0 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,5 @@ 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 +119,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); + + // eslint-disable-next-line no-unused-expressions + expect(clientFactorySpy.calledOnce).to.be.true; + + const calledWithArgs = clientFactorySpy.firstCall.args[0]; + expect(calledWithArgs.retries).to.equal(mockServiceConfig.retries); + expect(calledWithArgs.retryStrategy).to.deep.equal(mockServiceConfig.retryStrategy); + }); }); From 08f0e310bb33ffcced0e4aa64ba1147aab13f6e1 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Mon, 11 Mar 2024 11:56:40 -0400 Subject: [PATCH 5/6] add test fro debugger --- .../sitecore-jss/src/i18n/graphql-dictionary-service.test.ts | 3 ++- .../sitecore-jss/src/layout/graphql-layout-service.test.ts | 3 ++- .../sitecore-jss/src/site/graphql-error-pages-service.test.ts | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) 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 6b5f630224..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,3 +1,4 @@ +/* eslint-disable no-unused-expressions */ import { expect } from 'chai'; import sinon, { SinonSpy } from 'sinon'; import nock from 'nock'; @@ -290,10 +291,10 @@ describe('GraphQLDictionaryService', () => { new GraphQLDictionaryService(mockServiceConfig); - // eslint-disable-next-line no-unused-expressions 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 b89950de64..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,3 +1,4 @@ +/* eslint-disable no-unused-expressions */ import { expect, use } from 'chai'; import sinon, { SinonSpy } from 'sinon'; import spies from 'chai-spies'; @@ -310,10 +311,10 @@ describe('GraphQLLayoutService', () => { new GraphQLLayoutService(mockServiceConfig); - // eslint-disable-next-line no-unused-expressions 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 a5e3e205c0..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,3 +1,4 @@ +/* eslint-disable no-unused-expressions */ import { expect } from 'chai'; import sinon, { SinonSpy } from 'sinon'; import nock from 'nock'; @@ -135,10 +136,10 @@ describe('GraphQLErrorPagesService', () => { new GraphQLErrorPagesService(mockServiceConfig); - // eslint-disable-next-line no-unused-expressions 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); }); From 69d8406c8a8b31ab1517d6369fd9b75161f945e8 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Mon, 11 Mar 2024 11:58:50 -0400 Subject: [PATCH 6/6] fix changelog --- .prettierignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index e4a1f0af18..ec6d3cdd7f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1 @@ package.json -changelog.md