nestjs-paginate/src/paginate.spec.ts

163 lines
5.0 KiB
TypeScript
Raw Normal View History

2020-06-28 17:34:00 +00:00
import { createConnection, Repository, Column } from 'typeorm'
2020-06-26 21:25:03 +00:00
import { Paginated, paginate, PaginateConfig } from './paginate'
import { PaginateQuery } from './decorator'
import { Entity, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm'
import { HttpException } from '@nestjs/common'
@Entity()
export class CatEntity {
@PrimaryGeneratedColumn()
id: number
2020-06-28 17:34:00 +00:00
@Column()
name: string
@Column()
color: string
2020-06-26 21:25:03 +00:00
@CreateDateColumn()
createdAt: string
}
describe('paginate', () => {
let repo: Repository<CatEntity>
2020-06-28 17:34:00 +00:00
let cats: CatEntity[]
2020-06-26 21:25:03 +00:00
beforeAll(async () => {
const connection = await createConnection({
type: 'sqlite',
database: ':memory:',
synchronize: true,
logging: false,
entities: [CatEntity],
})
repo = connection.getRepository(CatEntity)
2020-06-28 17:34:00 +00:00
cats = await repo.save([
repo.create({ name: 'Milo', color: 'brown' }),
repo.create({ name: 'Garfield', color: 'ginger' }),
repo.create({ name: 'Shadow', color: 'black' }),
repo.create({ name: 'George', color: 'white' }),
repo.create({ name: 'Leche', color: 'white' }),
])
2020-06-26 21:25:03 +00:00
})
it('should return an instance of Paginated', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
2020-06-28 17:34:00 +00:00
defaultSortBy: [['createdAt', 'DESC']],
2020-06-26 21:25:03 +00:00
defaultLimit: 1,
}
const query: PaginateQuery = {
path: '',
}
2020-06-28 17:34:00 +00:00
const result = await paginate<CatEntity>(query, repo, config)
2020-06-26 21:25:03 +00:00
2020-06-28 17:34:00 +00:00
expect(result).toBeInstanceOf(Paginated)
expect(result.data).toStrictEqual(cats.slice(0, 1))
2020-06-26 21:25:03 +00:00
})
2020-06-28 17:34:00 +00:00
it('should default to page 1, if negative page is given', async () => {
2020-06-26 21:25:03 +00:00
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
2020-06-28 17:34:00 +00:00
defaultLimit: 1,
2020-06-26 21:25:03 +00:00
}
const query: PaginateQuery = {
path: '',
2020-06-28 17:34:00 +00:00
page: -1,
2020-06-26 21:25:03 +00:00
}
2020-06-28 17:34:00 +00:00
const result = await paginate<CatEntity>(query, repo, config)
2020-06-26 21:25:03 +00:00
2020-06-28 17:34:00 +00:00
expect(result.meta.currentPage).toBe(1)
expect(result.data).toStrictEqual(cats.slice(0, 1))
2020-06-26 21:25:03 +00:00
})
it('should return correct links', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
}
const query: PaginateQuery = {
path: '',
page: 2,
limit: 2,
}
const { links } = await paginate<CatEntity>(query, repo, config)
expect(links.first).toBe('?page=1&limit=2&sortBy=id:ASC')
expect(links.previous).toBe('?page=1&limit=2&sortBy=id:ASC')
expect(links.current).toBe('?page=2&limit=2&sortBy=id:ASC')
expect(links.next).toBe('?page=3&limit=2&sortBy=id:ASC')
expect(links.last).toBe('?page=3&limit=2&sortBy=id:ASC')
})
2020-06-27 18:54:14 +00:00
it('should default to defaultSortBy if query sortBy does not exist', async () => {
2020-06-26 21:25:03 +00:00
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'createdAt'],
2020-06-28 17:34:00 +00:00
defaultSortBy: [['id', 'DESC']],
2020-06-26 21:25:03 +00:00
}
const query: PaginateQuery = {
path: '',
}
2020-06-28 17:34:00 +00:00
const result = await paginate<CatEntity>(query, repo, config)
2020-06-26 21:25:03 +00:00
2020-06-28 17:34:00 +00:00
expect(result.meta.sortBy).toStrictEqual([['id', 'DESC']])
expect(result.data).toStrictEqual(cats.slice(0).reverse())
2020-06-26 21:25:03 +00:00
})
2020-06-28 17:34:00 +00:00
it('should sort result by multiple columns', async () => {
2020-06-27 18:54:14 +00:00
const config: PaginateConfig<CatEntity> = {
2020-06-28 17:34:00 +00:00
sortableColumns: ['name', 'color'],
2020-06-27 18:54:14 +00:00
}
const query: PaginateQuery = {
path: '',
sortBy: [
2020-06-28 17:34:00 +00:00
['color', 'DESC'],
['name', 'ASC'],
2020-06-27 18:54:14 +00:00
],
}
2020-06-28 17:34:00 +00:00
const result = await paginate<CatEntity>(query, repo, config)
2020-06-27 18:54:14 +00:00
2020-06-28 17:34:00 +00:00
expect(result.meta.sortBy).toStrictEqual([
['color', 'DESC'],
['name', 'ASC'],
2020-06-27 18:54:14 +00:00
])
2020-06-28 17:34:00 +00:00
expect(result.data).toStrictEqual([cats[3], cats[4], cats[1], cats[0], cats[2]])
})
it('should return result based on search term', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id', 'name', 'color'],
searchableColumns: ['name', 'color'],
}
const query: PaginateQuery = {
path: '',
search: 'i',
}
const result = await paginate<CatEntity>(query, repo, config)
expect(result.meta.search).toStrictEqual('i')
expect(result.data).toStrictEqual([cats[0], cats[1], cats[3], cats[4]])
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&search=i')
2020-06-27 18:54:14 +00:00
})
2020-06-26 21:25:03 +00:00
it('should throw an error when no sortableColumns', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: [],
}
const query: PaginateQuery = {
path: '',
}
try {
await paginate<CatEntity>(query, repo, config)
} catch (err) {
expect(err).toBeInstanceOf(HttpException)
}
})
})