From 3b65acfda18615064068b910a42bc26a9229bda7 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 29 Feb 2024 14:37:28 -0500 Subject: [PATCH] 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, }); }