Skip to content

Commit

Permalink
switch to use site query for SXP
Browse files Browse the repository at this point in the history
  • Loading branch information
addy-pathania committed Apr 11, 2024
1 parent f2d95d3 commit 2369ed1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class MultisitePlugin implements ConfigPlugin {
try {
const siteInfoService = new GraphQLSiteInfoService({
clientFactory: createGraphQLClientFactory(config),
<% if (templates.includes("nextjs-xmcloud")) { -%>
// enable site query for the service. Only works on XMCloud currently
useSiteQuery: true,
<% } -%>
});
sites = await siteInfoService.fetchSiteInfo();
} catch (error) {
Expand Down
238 changes: 31 additions & 207 deletions packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
import { expect, spy, use } from 'chai';
import spies from 'chai-spies';
import nock from 'nock';
import {
GraphQLSiteInfoService,
GraphQLSiteInfoResult,
GraphQLXmCloudSiteInfoResult,
} from './graphql-siteinfo-service';
import { GraphQLRequestClient, PageInfo } from '../graphql';
import { GraphQLSiteInfoService, GraphQLSiteInfoResult } from './graphql-siteinfo-service';
import { GraphQLRequestClient } from '../graphql';
import debugApi from 'debug';
import debug from '../debug';

use(spies);

describe('GraphQLSiteInfoService', () => {
describe.only('GraphQLSiteInfoService', () => {
let debugNamespaces: string;
const endpoint = 'http://site';
const apiKey = 'some-api-key';
Expand All @@ -28,29 +24,24 @@ describe('GraphQLSiteInfoService', () => {
hostName: string;
language: string;
}): GraphQLSiteInfoResult => ({
name: { value: name },
hostName: { value: hostName },
language: { value: language },
name: name,
hostName: hostName,
language: language,
});

const nonEmptyResponse = ({
count = 1,
start = 0,
pageInfo = { hasNext: false, endCursor: '' },
sites = [],
}: {
count?: number;
start?: number;
pageInfo?: PageInfo;
sites?: GraphQLSiteInfoResult[];
} = {}) => ({
data: {
search: {
pageInfo,
results: [
site: {
siteInfoCollection: [
...[...Array(count).keys()].map((n) =>
site({
name: `site ${start + n}`,
name: `site ${n}`,
hostName: 'restricted.gov',
language: 'en',
})
Expand All @@ -63,9 +54,8 @@ describe('GraphQLSiteInfoService', () => {

const emptyResponse = {
data: {
search: {
pageInfo: {},
results: [],
site: {
siteInfoCollection: [],
},
},
};
Expand Down Expand Up @@ -154,52 +144,6 @@ describe('GraphQLSiteInfoService', () => {
},
]);
});

it('should return correct result using custom pageSize', async () => {
mockSiteInfoRequest(nonEmptyResponse({ count: 2, pageInfo: { hasNext: true, endCursor: '' } }));
mockSiteInfoRequest(
nonEmptyResponse({ count: 2, start: 2, pageInfo: { hasNext: true, endCursor: '' } })
);
mockSiteInfoRequest(
nonEmptyResponse({ count: 2, start: 4, pageInfo: { hasNext: false, endCursor: '' } })
);

const service = new GraphQLSiteInfoService({ apiKey: apiKey, endpoint: endpoint, pageSize: 2 });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 1',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 2',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 3',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 4',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 5',
hostName: 'restricted.gov',
language: 'en',
},
]);
});

it('should return empty array when empty result received', async () => {
nock(endpoint)
.post('/')
Expand Down Expand Up @@ -271,146 +215,26 @@ describe('GraphQLSiteInfoService', () => {
expect(nock.isDone(), 'skip request').to.be.false;
});

describe('Fetch with site query in XM Cloud', () => {
const site = ({
name,
hostName,
language,
}: {
name: string;
hostName: string;
language: string;
}): GraphQLXmCloudSiteInfoResult => ({
name,
hostName,
language,
});

const nonEmptyResponse = ({
count = 1,
sites = [],
}: {
count?: number;
sites?: GraphQLXmCloudSiteInfoResult[];
} = {}) => ({
data: {
site: {
siteInfoCollection: [
...[...Array(count).keys()].map((n) =>
site({
name: `site ${n}`,
hostName: 'restricted.gov',
language: 'en',
})
),
...sites,
],
},
},
});

const emptyResponse = {
data: {
site: {
siteInfoCollection: [],
},
it('should filter out default website', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'website',
hostName: 'notheadless.org',
language: '',
}),
],
})
);
const service = new GraphQLSiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
};

const getSiteQuerySiteInfoService = (initProps: { [key: string]: unknown }) => {
return new GraphQLSiteInfoService({ useSiteQuery: true, ...initProps });
};

it('should return correct result', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
}),
],
})
);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
},
]);
});

it('should return correct result using clientFactory', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
}),
],
})
);
const clientFactory = GraphQLRequestClient.createClientFactory({
endpoint,
apiKey,
});
const service = getSiteQuerySiteInfoService({ clientFactory });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
},
]);
});

it('should return empty array when empty result received', async () => {
nock(endpoint)
.post('/')
.reply(200, emptyResponse);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.deep.equal([]);
});

it('should filter out default website', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'website',
hostName: 'notheadless.org',
language: '',
}),
],
})
);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
]);
});
]);
});
});
Loading

0 comments on commit 2369ed1

Please sign in to comment.