removeDocsVisitor.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { numberTypeNode, publicKeyTypeNode, structFieldTypeNode, structTypeNode } from '@codama/nodes';
  2. import { expect, test } from 'vitest';
  3. import { removeDocsVisitor, visit } from '../src';
  4. test('it empties the docs array of any node that contains docs', () => {
  5. // Given the following struct node with docs.
  6. const node = structTypeNode([
  7. structFieldTypeNode({
  8. docs: ['The owner of the account.'],
  9. name: 'owner',
  10. type: publicKeyTypeNode(),
  11. }),
  12. structFieldTypeNode({
  13. docs: ['The wallet allowed to modify the account.'],
  14. name: 'authority',
  15. type: publicKeyTypeNode(),
  16. }),
  17. structFieldTypeNode({
  18. docs: ['The amount of tokens in basis points.'],
  19. name: 'amount',
  20. type: numberTypeNode('u64'),
  21. }),
  22. ]);
  23. // When we remove the docs from the node.
  24. const result = visit(node, removeDocsVisitor());
  25. // Then we expect the following node.
  26. expect(result).toEqual(
  27. structTypeNode([
  28. structFieldTypeNode({
  29. docs: [],
  30. name: 'owner',
  31. type: publicKeyTypeNode(),
  32. }),
  33. structFieldTypeNode({
  34. docs: [],
  35. name: 'authority',
  36. type: publicKeyTypeNode(),
  37. }),
  38. structFieldTypeNode({
  39. docs: [],
  40. name: 'amount',
  41. type: numberTypeNode('u64'),
  42. }),
  43. ]),
  44. );
  45. });
  46. test('it freezes the returned node', () => {
  47. // Given the following struct node with docs.
  48. const node = structTypeNode([
  49. structFieldTypeNode({
  50. docs: ['The owner of the account.'],
  51. name: 'owner',
  52. type: publicKeyTypeNode(),
  53. }),
  54. ]);
  55. // When we remove the docs from the node.
  56. const result = visit(node, removeDocsVisitor());
  57. // Then we expect the returned node to be frozen.
  58. expect(Object.isFrozen(result)).toBe(true);
  59. });
  60. test('it can create partial visitors', () => {
  61. // Given the following struct node with docs.
  62. const node = structTypeNode([
  63. structFieldTypeNode({
  64. docs: ['The owner of the account.'],
  65. name: 'owner',
  66. type: publicKeyTypeNode(),
  67. }),
  68. ]);
  69. // And a remove docs visitor that only supports struct type nodes.
  70. const visitor = removeDocsVisitor({ keys: ['structTypeNode'] });
  71. // When we use it on our struct node.
  72. const result = visit(node, visitor);
  73. // Then we expect the same node back.
  74. expect(result).toEqual(node);
  75. // And we expect an error when visiting an unsupported node.
  76. // @ts-expect-error StructFieldTypeNode is not supported.
  77. expect(() => visit(node.fields[0], visitor)).toThrow();
  78. });