Skip to content

Commit

Permalink
refactored(#1): test now uses ModelPaginator impl
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgobich committed Jul 25, 2024
1 parent a20f86a commit b0bd754
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ const paginationUrls = paginatorDto.meta.pagesInRange
*/
```

If, for example, you're using something like Inertia, you can then type your props accordingly

```ts
import { SimplePaginatorDtoContract } from '@adocasts.com/dto/types'
import DifficultyDto from '#dtos/difficulty'

const props = defineProps<{
paginated: SimplePaginatorDtoContract<DifficultyDto>
}>()

const rows = props.paginated.data
const info = props.paginated.meta
const urls = props.paginated.meta.pagesInRange
```

## Things To Note

- At present we assume the Model's name from the file name of the model.
Expand Down
14 changes: 14 additions & 0 deletions test-utils/model_paginator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SimplePaginator } from '@adonisjs/lucid/database'
import { ModelPaginatorContract, CherryPick } from '@ioc:Adonis/Lucid/Orm'

export class ModelPaginator extends SimplePaginator implements ModelPaginatorContract<any> {
/**
* Serialize models
*/
serialize(cherryPick?: CherryPick) {
return {
meta: this.getMeta(),
data: this.all().map((row) => row.serialize(cherryPick)),
}
}
}
58 changes: 29 additions & 29 deletions tests/dtos/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from '@japa/runner'
import { BaseDto, BaseModelDto } from '../../src/base/main.js'
import { BaseModel, column } from '@adonisjs/lucid/orm'
// import { SimplePaginator } from '@adonisjs/lucid/database'
// import SimplePaginatorDto from '../../src/paginator/simple_paginator_dto.js'
import SimplePaginatorDto from '../../src/paginator/simple_paginator_dto.js'
import { ModelPaginator } from '../../test-utils/model_paginator.js'

test.group('Internal DTOs', (group) => {
group.each.teardown(async ({ context }) => {
Expand Down Expand Up @@ -58,31 +58,31 @@ test.group('Internal DTOs', (group) => {
dtoArray.map((dto) => assert.instanceOf(dto, TestDto))
})

// test('should allow conversion to SimplePaginatorDto for Lucid Models', async ({ assert }) => {
// class Test extends BaseModel {
// @column()
// declare id: number
// }

// class TestDto extends BaseModelDto {
// declare id: number
// constructor(instance: Test) {
// super()
// this.id = instance.id
// }
// }

// const test1 = new Test().merge({ id: 1 })
// const test2 = new Test().merge({ id: 2 })
// const test3 = new Test().merge({ id: 3 })

// const paginator = new SimplePaginator(3, 2, 1, test1, test2, test3)
// const paginatorDto = TestDto.fromPaginator(paginator, { start: 1, end: 2 })

// assert.instanceOf(paginatorDto, SimplePaginatorDto)
// assert.isArray(paginatorDto.data)
// assert.lengthOf(paginatorDto.meta.pagesInRange!, 2)

// paginatorDto.data.map((dto) => assert.instanceOf(dto, TestDto))
// })
test('should allow conversion to SimplePaginatorDto for Lucid Models', async ({ assert }) => {
class Test extends BaseModel {
@column()
declare id: number
}

class TestDto extends BaseModelDto {
declare id: number
constructor(instance: Test) {
super()
this.id = instance.id
}
}

const test1 = new Test().merge({ id: 1 })
const test2 = new Test().merge({ id: 2 })
const test3 = new Test().merge({ id: 3 })

const paginator = new ModelPaginator(3, 2, 1, test1, test2, test3)
const paginatorDto = TestDto.fromPaginator(paginator, { start: 1, end: 2 })

assert.instanceOf(paginatorDto, SimplePaginatorDto)
assert.isArray(paginatorDto.data)
assert.lengthOf(paginatorDto.meta.pagesInRange!, 2)

paginatorDto.data.map((dto) => assert.instanceOf(dto, TestDto))
})
})

0 comments on commit b0bd754

Please sign in to comment.