Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss] Add unit tests for retries config #1751

Merged
merged 7 commits into from
Mar 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
31 changes: 30 additions & 1 deletion packages/sitecore-jss/src/graphql-request-client.test.ts
Original file line number Diff line number Diff line change
@@ -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')
24 changes: 24 additions & 0 deletions packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
24 changes: 24 additions & 0 deletions packages/sitecore-jss/src/layout/graphql-layout-service.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
25 changes: 25 additions & 0 deletions packages/sitecore-jss/src/site/graphql-error-pages-service.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});