From a7506e713964ce2e494e87a0de561fa022dd0ad6 Mon Sep 17 00:00:00 2001 From: Pedro Tacla Yamada Date: Wed, 5 Feb 2025 17:25:51 +1100 Subject: [PATCH] Add ContentGraph::getContentKeyByNodeId Adds a method to retrieve content keys associated with node IDs. Test Plan: Run unit-tests (yarn test) Reviewers: MonicaOlejniczak Reviewed By: MonicaOlejniczak Pull Request: https://github.com/atlassian-labs/atlaspack/pull/327 --- packages/core/graph/src/ContentGraph.js | 7 +++++++ packages/core/graph/test/ContentGraph.test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/packages/core/graph/src/ContentGraph.js b/packages/core/graph/src/ContentGraph.js index 033fe76be..bac29978d 100644 --- a/packages/core/graph/src/ContentGraph.js +++ b/packages/core/graph/src/ContentGraph.js @@ -75,6 +75,13 @@ export default class ContentGraph extends Graph< } } + getContentKeyByNodeId(nodeId: NodeId): ContentKey { + return nullthrows( + this._nodeIdToContentKey.get(nodeId), + `Expected node id ${nodeId} to exist`, + ); + } + getNodeIdByContentKey(contentKey: ContentKey): NodeId { return nullthrows( this._contentKeyToNodeId.get(contentKey), diff --git a/packages/core/graph/test/ContentGraph.test.js b/packages/core/graph/test/ContentGraph.test.js index 3cb0e66d9..8762daca2 100644 --- a/packages/core/graph/test/ContentGraph.test.js +++ b/packages/core/graph/test/ContentGraph.test.js @@ -39,4 +39,22 @@ describe('ContentGraph', () => { assert(!graph.hasContentKey('contentKey')); }); + + describe('getContentKeyByNodeId', () => { + it('returns the content key for a node', () => { + const graph = new ContentGraph(); + + const node1 = graph.addNodeByContentKey('node1', {}); + assert.equal(graph.getContentKeyByNodeId(node1), 'node1'); + const node2 = graph.addNodeByContentKey('node2', {}); + assert.equal(graph.getContentKeyByNodeId(node2), 'node2'); + }); + + it('throws if the node does not have a content key', () => { + const graph = new ContentGraph(); + + const node1 = graph.addNode({}); + assert.throws(() => graph.getContentKeyByNodeId(node1)); + }); + }); });