testUtils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Neo4j, * as Neo4jMock from 'neo4j-driver';
  2. import { CompletionEvent } from '../src/events';
  3. jest.mock('neo4j-driver');
  4. let counter = 1;
  5. export function clearIdCounter() {
  6. counter = 1;
  7. }
  8. export function makeNode(labels, properties = {}) {
  9. return new Neo4j.types.Node(Neo4j.int(counter++), labels, properties);
  10. }
  11. export function makeRelationship(type, startNode, endNode, properties = {}) {
  12. return new Neo4j.types.Relationship(Neo4j.int(counter++), startNode.identity, endNode.identity, type, properties);
  13. }
  14. export function makePathFromNodes(nodes, relationshipType) {
  15. const pathSegments = [];
  16. for (let i = 0; i < nodes.length - 1; i++) {
  17. pathSegments.push(new Neo4j.types.PathSegment(
  18. nodes[i],
  19. makeRelationship(relationshipType, nodes[i], nodes[i + 1]),
  20. nodes[i + 1]
  21. ));
  22. }
  23. return new Neo4j.types.Path(nodes[0], nodes[nodes.length - 1], pathSegments);
  24. }
  25. export function makeRecord(parameters) {
  26. const recordKeys = parameters.map((_, index) => index.toString());
  27. return new Neo4j.types.Record(recordKeys, parameters);
  28. }
  29. export function assertNodes(neovis, nodes, assertFunction) {
  30. nodes.forEach(node => {
  31. const dataSetNode = neovis._data.nodes.get(node.identity.toInt());
  32. assertFunction(node, dataSetNode);
  33. });
  34. }
  35. export function assertEdges(neovis, edges, assertFunction) {
  36. edges.forEach(edges => {
  37. const dataSetEdge = neovis._data.edges.get(edges.identity.toInt());
  38. assertFunction(edges, dataSetEdge);
  39. });
  40. }
  41. export function mockNormalRunSubscribe(records = []) {
  42. Neo4jMock.mockSessionRun.mockImplementation(() => {
  43. const observablePromise = Promise.resolve({records});
  44. observablePromise.subscribe = ({onNext, onCompleted}) => {
  45. records.forEach(onNext);
  46. onCompleted();
  47. };
  48. return observablePromise;
  49. });
  50. }
  51. export function mockFullRunSubscribe(cypherIdsAndAnswers) {
  52. Neo4jMock.mockSessionRun.mockImplementation((cypher, parameters) => {
  53. if (!cypherIdsAndAnswers[cypher]) {
  54. throw new Error(`the cypher '${cypher}' was not expected`);
  55. }
  56. if (!cypherIdsAndAnswers[cypher].default && !cypherIdsAndAnswers[cypher][parameters.id.toInt()]) {
  57. throw new Error(`the id '${parameters.id}' was not expected for cypher ${cypher}`);
  58. }
  59. const records = cypherIdsAndAnswers[cypher].default || cypherIdsAndAnswers[cypher][parameters.id.toInt()];
  60. const observablePromise = Promise.resolve({records});
  61. observablePromise.subscribe = ({onNext, onCompleted}) => {
  62. records.forEach(onNext);
  63. onCompleted();
  64. };
  65. return observablePromise;
  66. });
  67. }
  68. export function neovisRenderDonePromise(neovis) {
  69. return new Promise(res => neovis.registerOnEvent(CompletionEvent, res));
  70. }