-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch multiple CMS template pages instead of just the first (#2314)
## What's the purpose of this pull request? Currently, when trying to load contentTypes from the Headless CMS we just fetch the first page and ignore the hasNextPage response from the cmsClient. ## How it works? This PR changes that to load all publishedContentTypes before returning. This might be troublesome if a given account has thousands of content types of the same type (PLP, for instance). We can either increase the page size to make fewer requests or investigate if we can move the filtering to the CMS Client. ## How to test it? I've added tests, but this can be replicated by copying the `server/cms/index.ts` code to a store's node_modules folder and you should see the effect.
- Loading branch information
Showing
4 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
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
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,95 @@ | ||
import { clientCMS, getCMSPage } from '../../../src/server/cms' | ||
import { jest } from '@jest/globals' | ||
import { ContentData } from '@vtex/client-cms' | ||
|
||
describe('CMS Integration', () => { | ||
const mockData = (count = 1) => { | ||
const data: ContentData[] = [] | ||
for (let i = 0; i < count; i = i + 1) { | ||
data.push({ | ||
id: `data-id-${i}`, | ||
name: `data-name-${i}`, | ||
status: `data-status-${i}`, | ||
type: `data-type-${i}`, | ||
sections: [], | ||
releaseId: `release-${i}`, | ||
}) | ||
} | ||
return data | ||
} | ||
|
||
describe('getCMSPage', () => { | ||
it('returns the first page if there is only one page', async () => { | ||
const mockFunction = jest.fn(() => { | ||
return Promise.resolve({ | ||
data: mockData(3), | ||
hasNextPage: false, | ||
totalItems: 3, | ||
}) | ||
}) | ||
clientCMS.getCMSPagesByContentType = mockFunction | ||
|
||
const result = await getCMSPage( | ||
{ contentType: 'plp' }, | ||
{ cmsClient: clientCMS } | ||
) | ||
|
||
expect(mockFunction.mock.calls.length).toBe(1) | ||
expect(result.data.length).toBe(3) | ||
}) | ||
|
||
it('loads multiple pages', async () => { | ||
const mockFunction: jest.Mock<typeof clientCMS.getCMSPagesByContentType> = | ||
jest.fn() | ||
|
||
mockFunction.mockImplementationOnce(() => { | ||
return Promise.resolve({ | ||
data: mockData(10), | ||
hasNextPage: true, | ||
totalItems: 15, | ||
}) | ||
}) | ||
mockFunction.mockImplementationOnce(() => { | ||
return Promise.resolve({ | ||
data: mockData(5), | ||
hasNextPage: false, | ||
totalItems: 15, | ||
}) | ||
}) | ||
|
||
clientCMS.getCMSPagesByContentType = mockFunction | ||
|
||
const result = await getCMSPage( | ||
{ contentType: 'plp' }, | ||
{ cmsClient: clientCMS, cache: {} } | ||
) | ||
|
||
expect(mockFunction.mock.calls.length).toBe(2) | ||
expect(result.data.length).toBe(15) | ||
}) | ||
|
||
it('it makes no request if the cache is filled', async () => { | ||
const mockFunction: jest.Mock<typeof clientCMS.getCMSPagesByContentType> = | ||
jest.fn() | ||
|
||
mockFunction.mockImplementationOnce(() => { | ||
return Promise.resolve({ | ||
data: mockData(10), | ||
hasNextPage: true, | ||
totalItems: 15, | ||
}) | ||
}) | ||
|
||
clientCMS.getCMSPagesByContentType = mockFunction | ||
|
||
const cache = { plp: { data: [] } } | ||
const result = await getCMSPage( | ||
{ contentType: 'plp' }, | ||
{ cmsClient: clientCMS, cache: cache } | ||
) | ||
|
||
expect(mockFunction.mock.calls.length).toBe(0) | ||
expect(result.data.length).toBe(0) | ||
}) | ||
}) | ||
}) |
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