Skip to content

Commit

Permalink
fix(gatsby): call predicate for the root ancestor in findRootNodeAnce…
Browse files Browse the repository at this point in the history
…stor (#25974)
  • Loading branch information
vladar authored and KyleAMathews committed Jul 29, 2020
1 parent 34d0ac4 commit c414566
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/gatsby/src/schema/__tests__/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@ describe(`NodeModel`, () => {
const result = nodeModel.findRootNodeAncestor(obj, predicate)
expect(result.id).toBe(`file1`)
})

it(`returns null when object's top-most ancestor doesn't match the provided predicate`, () => {
const node = nodeModel.getNodeById({ id: `post1` })
const obj = node.frontmatter.authors
const predicate = () => false
const result = nodeModel.findRootNodeAncestor(obj, predicate)
expect(result).toBe(null)
})
})

describe(`createPageDependency`, () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ class LocalNodeModel {
const id = this._rootNodeMap.get(node)
const trackedParent = getNodeById(id)

if (!parent && !trackedParent) return node
if (!parent && !trackedParent) {
const isMatchingRoot = !predicate || predicate(node)
return isMatchingRoot ? node : null
}

node = parent || trackedParent
}
Expand Down

0 comments on commit c414566

Please sign in to comment.