Repository navigation
Content Types: Page through all allowed content types rather than stopping at the first 100 (closes #23512) #23522
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
Changes from 1 commit
4cc8d53
a3f388a
a1990d4
4706190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| const { http, HttpResponse } = window.MockServiceWorker; | ||
| import { umbMemberTypeMockDb } from '../../db/member-type.db.js'; | ||
| import { UMB_SLUG } from './slug.js'; | ||
| import { pageResponse } from '../../utils.js'; | ||
| import { umbracoPath } from '@umbraco-cms/backoffice/utils'; | ||
|
|
||
| export const structureHandlers = [ | ||
| http.get(umbracoPath(`${UMB_SLUG}/allowed-at-root`), () => { | ||
| http.get(umbracoPath(`${UMB_SLUG}/allowed-at-root`), ({ request }) => { | ||
| const response = umbMemberTypeMockDb.getAllowedAtRoot(); | ||
| return HttpResponse.json(response); | ||
| return HttpResponse.json(pageResponse(response, request)); | ||
| }), | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,25 @@ | ||
| import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; | ||
| import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity'; | ||
| import type { UmbDataSourceResponse, UmbPagedModel } from '@umbraco-cms/backoffice/repository'; | ||
| import type { UmbOffsetPaginationRequestModel } from '@umbraco-cms/backoffice/utils'; | ||
|
|
||
| export interface UmbContentTypeStructureDataSourceConstructor<ItemType> { | ||
| new (host: UmbControllerHost): UmbContentTypeStructureDataSource<ItemType>; | ||
| } | ||
|
|
||
| export interface UmbContentTypeStructureDataSource<ItemType> { | ||
| /** | ||
| * Returns a single page of the content types allowed as children. | ||
| * @param {string | null} unique - The content type to get the allowed children of, or `null` for the root. | ||
| * @param {string | null} parentContentUnique - The content item the children will be created under, if any. | ||
| * @param {UmbOffsetPaginationRequestModel} paging - The page to return. Implementations must honour this, as a | ||
| * source that ignores it returns the same page repeatedly when a caller pages through the full set. | ||
| * @returns {Promise} A promise resolving to `{ data: { items, total } }`, or `{ error }`. | ||
| */ | ||
| getAllowedChildrenOf( | ||
| unique: string | null, | ||
| parentContentUnique: string | null, | ||
| paging?: UmbOffsetPaginationRequestModel, | ||
| ): Promise<UmbDataSourceResponse<UmbPagedModel<ItemType>>>; | ||
| getAllowedParentsOf?(unique: string): Promise<UmbDataSourceResponse<Array<UmbEntityModel>>>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { useMockHandlers, resetMockHandlers } from '../../../../../../mocks/index.js'; | ||
| import { UmbDocumentTypeStructureRepository } from './document-type-structure.repository.js'; | ||
| import { expect } from '@open-wc/testing'; | ||
| import { customElement } from '@umbraco-cms/backoffice/external/lit'; | ||
| import { UmbControllerHostElementMixin } from '@umbraco-cms/backoffice/controller-api'; | ||
| import { umbracoPath } from '@umbraco-cms/backoffice/utils'; | ||
|
|
||
| const { http, HttpResponse } = window.MockServiceWorker; | ||
|
|
||
| const UMB_SLUG = '/document-type'; | ||
|
|
||
| const TOTAL_ITEMS = 150; | ||
|
|
||
| const allItems = Array.from({ length: TOTAL_ITEMS }, (_, index) => ({ | ||
| id: `document-type-${index}`, | ||
| name: `Document Type ${index}`, | ||
| description: null, | ||
| icon: 'icon-document', | ||
| })); | ||
|
|
||
| type PageRequest = { skip: string | null; take: string | null }; | ||
|
|
||
| @customElement('umb-test-document-type-structure-repository-host') | ||
| class UmbTestDocumentTypeStructureRepositoryHostElement extends UmbControllerHostElementMixin(HTMLElement) {} | ||
|
|
||
| describe('UmbDocumentTypeStructureRepository', () => { | ||
| let host: UmbTestDocumentTypeStructureRepositoryHostElement; | ||
| let repository: UmbDocumentTypeStructureRepository; | ||
| let requests: Array<PageRequest>; | ||
|
|
||
| // The global mock handlers ignore skip/take and always return everything, which would let an unpaged | ||
| // repository pass. These handlers page for real, so the assertions below only hold if the repository pages. | ||
| const pagingHandler = (path: string) => | ||
| http.get(umbracoPath(path), ({ request }) => { | ||
| const url = new URL(request.url); | ||
| const skip = url.searchParams.get('skip'); | ||
| const take = url.searchParams.get('take'); | ||
| requests.push({ skip, take }); | ||
|
|
||
| const from = Number(skip) || 0; | ||
| const size = take === null ? 100 : Number(take); | ||
|
|
||
| return HttpResponse.json({ items: allItems.slice(from, from + size), total: allItems.length }); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| requests = []; | ||
| host = new UmbTestDocumentTypeStructureRepositoryHostElement(); | ||
| document.body.appendChild(host); | ||
| repository = new UmbDocumentTypeStructureRepository(host); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| repository.destroy(); | ||
| document.body.innerHTML = ''; | ||
| resetMockHandlers(); | ||
| }); | ||
|
|
||
| describe('requestAllAllowedChildrenOf', () => { | ||
| it('pages through every allowed child of a document type', async () => { | ||
| useMockHandlers(pagingHandler(`${UMB_SLUG}/:id/allowed-children`)); | ||
|
|
||
| const { data } = await repository.requestAllAllowedChildrenOf('parent-document-type', null); | ||
|
|
||
| expect(data?.items).to.have.lengthOf(TOTAL_ITEMS); | ||
| expect(data?.total).to.equal(TOTAL_ITEMS); | ||
| expect(requests).to.eql([ | ||
| { skip: '0', take: '100' }, | ||
| { skip: '100', take: '100' }, | ||
| ]); | ||
| }); | ||
|
Check warning on line 71 in src/Umbraco.Web.UI.Client/src/packages/documents/document-types/repository/structure/document-type-structure.repository.test.ts
|
||
|
|
||
| it('pages through every document type allowed at root', async () => { | ||
| useMockHandlers(pagingHandler(`${UMB_SLUG}/allowed-at-root`)); | ||
|
|
||
| const { data } = await repository.requestAllAllowedChildrenOf(null, null); | ||
|
|
||
| expect(data?.items).to.have.lengthOf(TOTAL_ITEMS); | ||
| expect(requests).to.eql([ | ||
| { skip: '0', take: '100' }, | ||
| { skip: '100', take: '100' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns an error when a page fails', async () => { | ||
|
Check failure on line 85 in src/Umbraco.Web.UI.Client/src/packages/documents/document-types/repository/structure/document-type-structure.repository.test.ts
|
||
| useMockHandlers( | ||
| http.get(umbracoPath(`${UMB_SLUG}/:id/allowed-children`), () => new HttpResponse(null, { status: 500 })), | ||
| ); | ||
|
|
||
| const { data, error } = await repository.requestAllAllowedChildrenOf('parent-document-type', null); | ||
|
|
||
| expect(error).to.exist; | ||
| expect(data).to.be.undefined; | ||
| }); | ||
| }); | ||
|
|
||
| describe('requestAllowedChildrenOf', () => { | ||
| it('makes a single unpaged request when no paging is given', async () => { | ||
| useMockHandlers(pagingHandler(`${UMB_SLUG}/:id/allowed-children`)); | ||
|
|
||
| const { data } = await repository.requestAllowedChildrenOf('parent-document-type', null); | ||
|
|
||
| expect(requests).to.eql([{ skip: null, take: null }]); | ||
| expect(data?.total).to.equal(TOTAL_ITEMS); | ||
| }); | ||
|
|
||
| it('requests only the given page', async () => { | ||
| useMockHandlers(pagingHandler(`${UMB_SLUG}/:id/allowed-children`)); | ||
|
|
||
| const { data } = await repository.requestAllowedChildrenOf('parent-document-type', null, { | ||
| skip: 100, | ||
| take: 100, | ||
| }); | ||
|
|
||
| expect(requests).to.eql([{ skip: '100', take: '100' }]); | ||
| expect(data?.items).to.have.lengthOf(TOTAL_ITEMS - 100); | ||
| expect(data?.total).to.equal(TOTAL_ITEMS); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.