forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move getPath from expect/utils to jest-util
- Loading branch information
1 parent
611dbf8
commit 6b98ef1
Showing
9 changed files
with
148 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"license": "MIT", | ||
"dependencies": { | ||
"chalk": "^2.0.1", | ||
"jest-util": "^23.1.0", | ||
"pretty-format": "^23.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import getPath from '../get_path'; | ||
|
||
describe('getPath()', () => { | ||
test('property exists', () => { | ||
expect(getPath({a: {b: {c: 5}}}, 'a.b.c')).toEqual({ | ||
hasEndProp: true, | ||
lastTraversedObject: {c: 5}, | ||
traversedPath: ['a', 'b', 'c'], | ||
value: 5, | ||
}); | ||
|
||
expect(getPath({a: {b: {c: {d: 1}}}}, 'a.b.c.d')).toEqual({ | ||
hasEndProp: true, | ||
lastTraversedObject: {d: 1}, | ||
traversedPath: ['a', 'b', 'c', 'd'], | ||
value: 1, | ||
}); | ||
}); | ||
|
||
test('property doesnt exist', () => { | ||
expect(getPath({a: {b: {}}}, 'a.b.c')).toEqual({ | ||
hasEndProp: false, | ||
lastTraversedObject: {}, | ||
traversedPath: ['a', 'b'], | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('property exist but undefined', () => { | ||
expect(getPath({a: {b: {c: undefined}}}, 'a.b.c')).toEqual({ | ||
hasEndProp: true, | ||
lastTraversedObject: {c: undefined}, | ||
traversedPath: ['a', 'b', 'c'], | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('property is a getter on class instance', () => { | ||
class A { | ||
get a() { | ||
return 'a'; | ||
} | ||
get b() { | ||
return {c: 'c'}; | ||
} | ||
} | ||
|
||
expect(getPath(new A(), 'a')).toEqual({ | ||
hasEndProp: true, | ||
lastTraversedObject: new A(), | ||
traversedPath: ['a'], | ||
value: 'a', | ||
}); | ||
expect(getPath(new A(), 'b.c')).toEqual({ | ||
hasEndProp: true, | ||
lastTraversedObject: {c: 'c'}, | ||
traversedPath: ['b', 'c'], | ||
value: 'c', | ||
}); | ||
}); | ||
|
||
test('path breaks', () => { | ||
expect(getPath({a: {}}, 'a.b.c')).toEqual({ | ||
hasEndProp: false, | ||
lastTraversedObject: {}, | ||
traversedPath: ['a'], | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
test('empty object at the end', () => { | ||
expect(getPath({a: {b: {c: {}}}}, 'a.b.c.d')).toEqual({ | ||
hasEndProp: false, | ||
lastTraversedObject: {}, | ||
traversedPath: ['a', 'b', 'c'], | ||
value: undefined, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
type GetPath = { | ||
hasEndProp?: boolean, | ||
lastTraversedObject: ?Object, | ||
traversedPath: Array<string>, | ||
value?: any, | ||
}; | ||
|
||
const hasOwnProperty = (object: Object, value: string) => | ||
Object.prototype.hasOwnProperty.call(object, value) || | ||
Object.prototype.hasOwnProperty.call(object.constructor.prototype, value); | ||
|
||
const getPath = ( | ||
object: Object, | ||
propertyPath: string | Array<string>, | ||
): GetPath => { | ||
if (!Array.isArray(propertyPath)) { | ||
propertyPath = propertyPath.split('.'); | ||
} | ||
|
||
if (propertyPath.length) { | ||
const lastProp = propertyPath.length === 1; | ||
const prop = propertyPath[0]; | ||
const newObject = object[prop]; | ||
|
||
if (!lastProp && (newObject === null || newObject === undefined)) { | ||
// This is not the last prop in the chain. If we keep recursing it will | ||
// hit a `can't access property X of undefined | null`. At this point we | ||
// know that the chain has broken and we can return right away. | ||
return { | ||
hasEndProp: false, | ||
lastTraversedObject: object, | ||
traversedPath: [], | ||
}; | ||
} | ||
|
||
const result = getPath(newObject, propertyPath.slice(1)); | ||
|
||
if (result.lastTraversedObject === null) { | ||
result.lastTraversedObject = object; | ||
} | ||
|
||
result.traversedPath.unshift(prop); | ||
|
||
if (lastProp) { | ||
result.hasEndProp = hasOwnProperty(object, prop); | ||
if (!result.hasEndProp) { | ||
result.traversedPath.shift(); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return { | ||
lastTraversedObject: null, | ||
traversedPath: [], | ||
value: object, | ||
}; | ||
}; | ||
|
||
export default getPath; |
Oops, something went wrong.