-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NextJS] GraphQL Request Client + GraphQL Sitemap Service (#530)
- Loading branch information
1 parent
95b7e11
commit 7f179c7
Showing
12 changed files
with
821 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
321 changes: 321 additions & 0 deletions
321
packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
import { expect } from 'chai'; | ||
import nock from 'nock'; | ||
import { GraphQLSitemapService } from './graphql-sitemap-service'; | ||
|
||
describe('GraphQLSitemapService', () => { | ||
const ROOT_ITEM = '/sitecore/next/home'; | ||
const graphQLLayoutService = new GraphQLSitemapService({ | ||
endpoint: 'http://jssnextweb/graphql', | ||
}); | ||
|
||
const mockRootItemIdRequest = () => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(200, { | ||
data: { | ||
item: { | ||
id: '4FEFEBB5-49D7-4E8F-89D5-D40BC1F23BAD', | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
const mockPathsRequest = (results: { url: { path: string } }[]) => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(200, { | ||
data: { | ||
search: { | ||
results, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe('SSG mode', () => { | ||
it('should fetch sitemap', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
mockPathsRequest([ | ||
{ | ||
url: { path: '/' }, | ||
}, | ||
{ | ||
url: { path: '/x1' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2/y3/y4' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2' }, | ||
}, | ||
]); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([ | ||
{ | ||
params: { | ||
path: [''], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['x1'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2', 'y3', 'y4'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should fetch sitemap using multiple locales', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
mockPathsRequest([ | ||
{ | ||
url: { path: '/' }, | ||
}, | ||
{ | ||
url: { path: '/x1' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2/y3/y4' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2' }, | ||
}, | ||
]); | ||
|
||
mockPathsRequest([ | ||
{ | ||
url: { path: '/' }, | ||
}, | ||
{ | ||
url: { path: '/x1-da-DK' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2/y3/y4-da-DK' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2-da-DK' }, | ||
}, | ||
]); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua', 'da-DK'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([ | ||
{ | ||
params: { | ||
path: [''], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['x1'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2', 'y3', 'y4'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2'], | ||
}, | ||
locale: 'ua', | ||
}, | ||
{ | ||
params: { | ||
path: [''], | ||
}, | ||
locale: 'da-DK', | ||
}, | ||
{ | ||
params: { | ||
path: ['x1-da-DK'], | ||
}, | ||
locale: 'da-DK', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2', 'y3', 'y4-da-DK'], | ||
}, | ||
locale: 'da-DK', | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2-da-DK'], | ||
}, | ||
locale: 'da-DK', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should fetch sitemap when items is empty', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
mockPathsRequest([]); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should fetch sitemap if locales are not provided', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap([], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should not fetch sitemap if rootItemId is not provided', async () => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(200, { | ||
data: null, | ||
}); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap([], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should handle error when request root item id', async () => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(401, { | ||
error: 'whoops', | ||
}); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should handle error when request paths', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(401, 'whoops'); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
}); | ||
|
||
describe('Export mode', () => { | ||
it('should fetch sitemap', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
mockPathsRequest([ | ||
{ | ||
url: { path: '/' }, | ||
}, | ||
{ | ||
url: { path: '/x1' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2/y3/y4' }, | ||
}, | ||
{ | ||
url: { path: '/y1/y2' }, | ||
}, | ||
]); | ||
|
||
const sitemap = await graphQLLayoutService.fetchExportSitemap('ua', ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([ | ||
{ | ||
params: { | ||
path: [''], | ||
}, | ||
}, | ||
{ | ||
params: { | ||
path: ['x1'], | ||
}, | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2', 'y3', 'y4'], | ||
}, | ||
}, | ||
{ | ||
params: { | ||
path: ['y1', 'y2'], | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('should fetch sitemap when items is empty', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
mockPathsRequest([]); | ||
|
||
const sitemap = await graphQLLayoutService.fetchExportSitemap('ua', ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should not fetch sitemap if rootItemId is not provided', async () => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(200, { | ||
data: null, | ||
}); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap([], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should handle error when request root item id', async () => { | ||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(401, { | ||
error: 'whoops', | ||
}); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
|
||
it('should handle error when request paths', async () => { | ||
mockRootItemIdRequest(); | ||
|
||
nock('http://jssnextweb') | ||
.post('/graphql') | ||
.reply(401, 'whoops'); | ||
|
||
const sitemap = await graphQLLayoutService.fetchSSGSitemap(['ua'], ROOT_ITEM); | ||
|
||
expect(sitemap).to.deep.equal([]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.