Skip to content

Commit

Permalink
feat: add totalItems to userMediaSource
Browse files Browse the repository at this point in the history
  • Loading branch information
glassbead0 authored and vnugent committed Feb 13, 2025
1 parent b1f9acc commit cd61958
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/db/MediaObjectTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface UserMedia {
edges: MediaEdge[]
pageInfo: {
hasNextPage: boolean
totalItems: number
endCursor: string | null
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/schema/Media.gql
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type MediaEdge {
type PageInfo {
"True if there are more data after the last cursor."
hasNextPage: Boolean!
"Total number of items."
totalItems: Int!
"Not yet supported."
endCursor: String
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/MediaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export default class MediaDataSource extends MongoDataSource<MediaObject> {
}
])

const itemCount = await this.mediaObjectModel.countDocuments({ userUuid })

let hasNextPage = false
if (rs.length > first) {
// ok there's a next page. remove the extra item.
Expand All @@ -185,6 +187,7 @@ export default class MediaDataSource extends MongoDataSource<MediaObject> {
)),
pageInfo: {
hasNextPage,
totalItems: itemCount,
endCursor: null
}

Expand Down
13 changes: 8 additions & 5 deletions src/model/__tests__/MediaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ describe('MediaDataSource', () => {
* With 3 items per page we should expect 3 pages.
*/
const newMediaListInput: MediaObjectGQLInput[] = []
for (let i = 0; i < 7; i = i + 1) {
const mediaCount = 7
for (let i = 0; i < mediaCount; i = i + 1) {
newMediaListInput.push({ ...MEDIA_TEMPLATE, mediaUrl: `/photo${i}.jpg` })
}

Expand All @@ -241,7 +242,7 @@ describe('MediaDataSource', () => {

const page1 = await media.getOneUserMediaPagination(input)

verifyPageData(page1, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(0, 3), ITEMS_PER_PAGE, true)
verifyPageData(page1, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(0, 3), mediaCount, ITEMS_PER_PAGE, true)

const page1Edges = page1.mediaConnection.edges
const input2: UserMediaQueryInput = {
Expand All @@ -251,7 +252,7 @@ describe('MediaDataSource', () => {
}
const page2 = await media.getOneUserMediaPagination(input2)

verifyPageData(page2, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(3, 6), ITEMS_PER_PAGE, true)
verifyPageData(page2, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(3, 6), mediaCount, ITEMS_PER_PAGE, true)

const page2Edges = page2.mediaConnection.edges
const input3: UserMediaQueryInput = {
Expand All @@ -261,7 +262,7 @@ describe('MediaDataSource', () => {
}
const page3 = await media.getOneUserMediaPagination(input3)

verifyPageData(page3, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(6, 7), 1, false)
verifyPageData(page3, MEDIA_TEMPLATE.userUuid, expectedMedia.slice(6, 7), mediaCount, 1, false)
})
})

Expand All @@ -270,18 +271,20 @@ describe('MediaDataSource', () => {
* @param actualPage
* @param expectedUserUuid
* @param expectedMedia
* @param totalItems
* @param itemsPerPage
* @param hasNextPage
*/
const verifyPageData = (
actualPage: UserMedia,
expectedUserUuid: string,
expectedMedia: MediaObject[],
totalItems: number,
itemsPerPage: number,
hasNextPage: boolean): void => {
expect(actualPage.userUuid).toEqual(expectedUserUuid)
expect(actualPage.mediaConnection.pageInfo.hasNextPage).toStrictEqual(hasNextPage)

expect(actualPage.mediaConnection.pageInfo.totalItems).toStrictEqual(totalItems)
const pageEdges = actualPage.mediaConnection.edges
expect(pageEdges).toHaveLength(itemsPerPage)

Expand Down

0 comments on commit cd61958

Please sign in to comment.