Skip to content

Commit 72de598

Browse files
authoredNov 28, 2020
fix(builder): multiple nested filters were not working (#154)
1 parent 80a7636 commit 72de598

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
 

Diff for: ‎src/Builder.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export default class Builder {
109109
if (Array.isArray(key)) {
110110
const [_key, _value] = this._nestedFilter(key, value)
111111

112-
this.filters[_key] = _value
112+
this.filters[_key] = { ...this.filters[_key], ..._value }
113113
} else {
114114
this.filters[key] = value
115115
}
@@ -125,7 +125,7 @@ export default class Builder {
125125
if (Array.isArray(key)) {
126126
const [_key, _value] = this._nestedFilter(key, array.join(','))
127127

128-
this.filters[_key] = _value
128+
this.filters[_key] = { ...this.filters[_key], ..._value }
129129
} else {
130130
this.filters[key] = array.join(',')
131131
}

Diff for: ‎tests/builder.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ describe('Query builder', () => {
121121

122122
expect(post._builder.filters).toEqual({ user: { status: 'active' } })
123123
expect(post._builder.query()).toEqual('?filter[user][status]=active')
124+
125+
post = Post
126+
.where(['schedule', 'start'], '2020-11-27')
127+
.where(['schedule', 'end'], '2020-11-28')
128+
129+
expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27', end: '2020-11-28' } })
130+
expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27&filter[schedule][end]=2020-11-28')
124131
})
125132

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

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

162+
expect(post._builder.filters).toEqual({ user: { status: 'active,inactive' } })
155163
expect(post._builder.query()).toEqual('?filter[user][status]=active,inactive')
164+
165+
post = Post
166+
.whereIn(['schedule', 'start'], ['2020-11-27', '2020-11-28'])
167+
.whereIn(['schedule', 'end'], ['2020-11-28', '2020-11-29'])
168+
169+
expect(post._builder.filters).toEqual({ schedule: { start: '2020-11-27,2020-11-28', end: '2020-11-28,2020-11-29' } })
170+
expect(post._builder.query()).toEqual('?filter[schedule][start]=2020-11-27,2020-11-28&filter[schedule][end]=2020-11-28,2020-11-29')
156171
})
157172

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

0 commit comments

Comments
 (0)
Please sign in to comment.