Skip to content

Commit

Permalink
Add test case for #79
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Sep 13, 2021
1 parent 4e1124d commit 08e1c07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/__tests__/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
createCachingMethods,
idToString,
isValidObjectIdString,
prepFields
prepFields,
getNestedValue
} from '../cache'

import { log } from '../helpers'
Expand Down Expand Up @@ -313,3 +314,16 @@ describe('isValidObjectIdString', () => {
expect(isValidObjectIdString(hexId)).toBe(true)
})
})

describe('getNestedValue', () => {
it('works', () => {
const obj = {
nested: { foo: 'bar', fooB: '', fooC: null, fooE: { inner: true } }
}
expect(getNestedValue(obj, 'nested.foo')).toEqual('bar')
expect(getNestedValue(obj, 'nested.fooB')).toEqual('')
expect(getNestedValue(obj, 'nested.fooC')).toEqual(null)
expect(getNestedValue(obj, 'nested.fooD')).toBeUndefined()
expect(getNestedValue(obj, 'nested.fooE.inner')).toBe(true)
})
})
8 changes: 7 additions & 1 deletion src/__tests__/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Mongoose', () => {

nestedBob = await userCollection.findOneAndReplace(
{ name: 'Bob' },
{ name: 'Bob', nested: [{ _id: objectID }] },
{ name: 'Bob', nested: { _id: objectID, field1: 'value1', field2: '' } },
{ new: true, upsert: true }
)
})
Expand Down Expand Up @@ -128,5 +128,11 @@ describe('Mongoose', () => {

expect(user).toBeDefined()
expect(user.name).toBe('Bob')

const res1 = await users.findByFields({ 'nested.field1': 'value1' })
const res2 = await users.findByFields({ 'nested.field2': 'value1' })

expect(res1[0].name).toBe('Bob')
expect(res2[0]).toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function prepFields(fields) {

// getNestedValue({ nested: { foo: 'bar' } }, 'nested.foo')
// => 'bar'
function getNestedValue(object, string) {
export function getNestedValue(object, string) {
string = string.replace(/\[(\w+)\]/g, '.$1') // convert indexes to properties
string = string.replace(/^\./, '') // strip a leading dot
var a = string.split('.')
Expand Down

0 comments on commit 08e1c07

Please sign in to comment.