deleteNodesVisitor.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { numberTypeNode, publicKeyTypeNode, tupleTypeNode } from '@codama/nodes';
  2. import { expect, test } from 'vitest';
  3. import { deleteNodesVisitor, visit } from '../src';
  4. test('it can delete nodes using selectors', () => {
  5. // Given the following tree.
  6. const node = tupleTypeNode([numberTypeNode('u32'), tupleTypeNode([numberTypeNode('u32'), publicKeyTypeNode()])]);
  7. // And a visitor that deletes all number nodes.
  8. const visitor = deleteNodesVisitor(['[numberTypeNode]']);
  9. // When we visit the tree using that visitor.
  10. const result = visit(node, visitor);
  11. // Then we expect the number nodes to have been deleted.
  12. expect(result).toEqual(tupleTypeNode([tupleTypeNode([publicKeyTypeNode()])]));
  13. });
  14. test('it can create partial visitors', () => {
  15. // Given the following tree.
  16. const node = tupleTypeNode([numberTypeNode('u32'), tupleTypeNode([numberTypeNode('u32'), publicKeyTypeNode()])]);
  17. // And a visitor that deletes all number nodes and public key nodes
  18. // but does not support public key nodes.
  19. const visitor = deleteNodesVisitor(['[numberTypeNode]', '[publicKeyTypeNode]'], {
  20. keys: ['tupleTypeNode', 'numberTypeNode'],
  21. });
  22. // When we visit the tree using that visitor.
  23. const result = visit(node, visitor);
  24. // Then only the number nodes have been deleted.
  25. expect(result).toEqual(tupleTypeNode([tupleTypeNode([publicKeyTypeNode()])]));
  26. // And the public key node cannot be visited.
  27. // @ts-expect-error PublicKeyTypeNode is not supported.
  28. expect(() => visit(publicKeyTypeNode(), visitor)).toThrow();
  29. });