Skip to content

Commit 08e1c07

Browse files
committed
Add test case for #79
1 parent 4e1124d commit 08e1c07

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/__tests__/cache.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
createCachingMethods,
88
idToString,
99
isValidObjectIdString,
10-
prepFields
10+
prepFields,
11+
getNestedValue
1112
} from '../cache'
1213

1314
import { log } from '../helpers'
@@ -313,3 +314,16 @@ describe('isValidObjectIdString', () => {
313314
expect(isValidObjectIdString(hexId)).toBe(true)
314315
})
315316
})
317+
318+
describe('getNestedValue', () => {
319+
it('works', () => {
320+
const obj = {
321+
nested: { foo: 'bar', fooB: '', fooC: null, fooE: { inner: true } }
322+
}
323+
expect(getNestedValue(obj, 'nested.foo')).toEqual('bar')
324+
expect(getNestedValue(obj, 'nested.fooB')).toEqual('')
325+
expect(getNestedValue(obj, 'nested.fooC')).toEqual(null)
326+
expect(getNestedValue(obj, 'nested.fooD')).toBeUndefined()
327+
expect(getNestedValue(obj, 'nested.fooE.inner')).toBe(true)
328+
})
329+
})

src/__tests__/datasource.test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Mongoose', () => {
6464

6565
nestedBob = await userCollection.findOneAndReplace(
6666
{ name: 'Bob' },
67-
{ name: 'Bob', nested: [{ _id: objectID }] },
67+
{ name: 'Bob', nested: { _id: objectID, field1: 'value1', field2: '' } },
6868
{ new: true, upsert: true }
6969
)
7070
})
@@ -128,5 +128,11 @@ describe('Mongoose', () => {
128128

129129
expect(user).toBeDefined()
130130
expect(user.name).toBe('Bob')
131+
132+
const res1 = await users.findByFields({ 'nested.field1': 'value1' })
133+
const res2 = await users.findByFields({ 'nested.field2': 'value1' })
134+
135+
expect(res1[0].name).toBe('Bob')
136+
expect(res2[0]).toBeUndefined()
131137
})
132138
})

src/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function prepFields(fields) {
4646

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

0 commit comments

Comments
 (0)