|
| 1 | +import { addToQuery } from './add-to-query.util.js' |
| 2 | + |
| 3 | +describe('addToQuery', () => { |
| 4 | + it('basic usage', () => { |
| 5 | + const result = addToQuery({}, 'id', 1) |
| 6 | + |
| 7 | + expect(result).toEqual({ id: 1 }) |
| 8 | + }) |
| 9 | + |
| 10 | + it('adds if property not in original query', () => { |
| 11 | + const result = addToQuery({ name: 'John' }, 'id', 1) |
| 12 | + |
| 13 | + expect(result).toEqual({ name: 'John', id: 1 }) |
| 14 | + }) |
| 15 | + |
| 16 | + it('does not add if exact same property-value pair exists', () => { |
| 17 | + const result = addToQuery({ id: 1 }, 'id', 1) |
| 18 | + |
| 19 | + expect(result).toEqual({ id: 1 }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('adds to $and if property exists with different value', () => { |
| 23 | + const result = addToQuery({ id: 1 }, 'id', 2) |
| 24 | + |
| 25 | + expect(result).toEqual({ id: 1, $and: [{ id: 2 }] }) |
| 26 | + }) |
| 27 | + |
| 28 | + it('does not add to $and if exact same property-value pair exists in $and', () => { |
| 29 | + const result = addToQuery({ id: 1, $and: [{ id: 2 }] }, 'id', 2) |
| 30 | + |
| 31 | + expect(result).toEqual({ id: 1, $and: [{ id: 2 }] }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('adds to $and if property exists with different value and $and already exists', () => { |
| 35 | + const result = addToQuery({ id: 1, $and: [{ id: 2 }] }, 'id', 3) |
| 36 | + |
| 37 | + expect(result).toEqual({ id: 1, $and: [{ id: 2 }, { id: 3 }] }) |
| 38 | + }) |
| 39 | +}) |
0 commit comments