Skip to content

Commit

Permalink
test case for filtering null and not null on relation
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-maegerli authored and Helveg committed Sep 29, 2024
1 parent 12a7eb8 commit be99407
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/__tests__/cat-home.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class CatHomeEntity {
@Column()
name: string

@Column({ nullable: true })
street: string | null

@OneToOne(() => CatEntity, (cat) => cat.home)
cat: CatEntity

Expand Down
38 changes: 32 additions & 6 deletions src/paginate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ describe('paginate', () => {
})

catHomes = await catHomeRepo.save([
catHomeRepo.create({ name: 'Box', cat: cats[0] }),
catHomeRepo.create({ name: 'House', cat: cats[1] }),
catHomeRepo.create({ name: 'Box', cat: cats[0], street: null }),
catHomeRepo.create({ name: 'House', cat: cats[1], street: 'Mainstreet' }),
])
catHomePillows = await catHomePillowRepo.save([
catHomePillowRepo.create({ color: 'red', home: catHomes[0] }),
Expand Down Expand Up @@ -2022,26 +2022,52 @@ describe('paginate', () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
'home.name': [FilterSuffix.NOT, FilterOperator.NULL],
'home.street': [FilterSuffix.NOT, FilterOperator.NULL],
},
relations: ['home'],
}
const query: PaginateQuery = {
path: '',
filter: {
'home.name': '$not:$null',
'home.street': '$not:$null',
},
}

const result = await paginate<CatEntity>(query, catRepo, config)
const expectedResult = [0, 1].map((i) => {
const expectedResult = [1].map((i) => {
const ret = Object.assign(clone(cats[i]), { home: Object.assign(clone(catHomes[i])) })
delete ret.home.cat
return ret
})

expect(result.data).toStrictEqual(expectedResult)
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.home.name=$not:$null')
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.home.street=$not:$null')
})

it('should return result based on null query on relation', async () => {
const config: PaginateConfig<CatEntity> = {
sortableColumns: ['id'],
filterableColumns: {
'home.street': [FilterOperator.NULL],
},
relations: ['home'],
}
const query: PaginateQuery = {
path: '',
filter: {
'home.street': '$null',
},
}

const result = await paginate<CatEntity>(query, catRepo, config)
const expectedResult = [0].map((i) => {
const ret = Object.assign(clone(cats[i]), { home: Object.assign(clone(catHomes[i])) })
delete ret.home.cat
return ret
})

expect(result.data).toStrictEqual(expectedResult)
expect(result.links.current).toBe('?page=1&limit=20&sortBy=id:ASC&filter.home.name=$null')
})

it('should ignore filterable column which is not configured', async () => {
Expand Down

0 comments on commit be99407

Please sign in to comment.