Skip to content

Commit

Permalink
fix(builder): multiple nested filters were not working (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoPedroAS51 authored Nov 28, 2020
1 parent 80a7636 commit 72de598
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class Builder {
if (Array.isArray(key)) {
const [_key, _value] = this._nestedFilter(key, value)

this.filters[_key] = _value
this.filters[_key] = { ...this.filters[_key], ..._value }
} else {
this.filters[key] = value
}
Expand All @@ -125,7 +125,7 @@ export default class Builder {
if (Array.isArray(key)) {
const [_key, _value] = this._nestedFilter(key, array.join(','))

this.filters[_key] = _value
this.filters[_key] = { ...this.filters[_key], ..._value }
} else {
this.filters[key] = array.join(',')
}
Expand Down
15 changes: 15 additions & 0 deletions tests/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ describe('Query builder', () => {

expect(post._builder.filters).toEqual({ user: { status: 'active' } })
expect(post._builder.query()).toEqual('?filter[user][status]=active')

post = Post
.where(['schedule', 'start'], '2020-11-27')
.where(['schedule', 'end'], '2020-11-28')

expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27', end: '2020-11-28' } })
expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27&filter[schedule][end]=2020-11-28')
})

test('where() throws a exception when doest not have params or only first param', () => {
Expand Down Expand Up @@ -152,7 +159,15 @@ describe('Query builder', () => {

post = Post.whereIn(['user', 'status'], ['active', 'inactive'])

expect(post._builder.filters).toEqual({ user: { status: 'active,inactive' } })
expect(post._builder.query()).toEqual('?filter[user][status]=active,inactive')

post = Post
.whereIn(['schedule', 'start'], ['2020-11-27', '2020-11-28'])
.whereIn(['schedule', 'end'], ['2020-11-28', '2020-11-29'])

expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27,2020-11-28', end: '2020-11-28,2020-11-29' } })
expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27,2020-11-28&filter[schedule][end]=2020-11-28,2020-11-29')
})

test('whereIn() throws a exception when second parameter is not a array', () => {
Expand Down

0 comments on commit 72de598

Please sign in to comment.