accountsPage.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {
  2. accountNode,
  3. constantPdaSeedNodeFromProgramId,
  4. constantPdaSeedNodeFromString,
  5. pdaLinkNode,
  6. pdaNode,
  7. programNode,
  8. publicKeyTypeNode,
  9. variablePdaSeedNode,
  10. } from '@codama/nodes';
  11. import { visit } from '@codama/visitors-core';
  12. import { test } from 'vitest';
  13. import { getRenderMapVisitor } from '../src';
  14. import { renderMapContains } from './_setup';
  15. test('it renders PDA helpers for PDA', async () => {
  16. // Given the following program with 1 account and 1 pda with seeds.
  17. const node = programNode({
  18. accounts: [accountNode({ name: 'foo', pda: pdaLinkNode('bar') })],
  19. name: 'myProgram',
  20. pdas: [
  21. pdaNode({
  22. name: 'bar',
  23. seeds: [
  24. constantPdaSeedNodeFromProgramId(),
  25. constantPdaSeedNodeFromString('utf8', 'bar'),
  26. variablePdaSeedNode('authority', publicKeyTypeNode()),
  27. ],
  28. }),
  29. ],
  30. publicKey: '1111',
  31. });
  32. // When we render it.
  33. const renderMap = visit(node, getRenderMapVisitor());
  34. // Then we expect the following fetch helper functions delegating to findBarPda.
  35. await renderMapContains(renderMap, 'accounts/foo.ts', [
  36. 'export function findFooPda',
  37. 'export async function fetchFooFromSeeds',
  38. 'export async function safeFetchFooFromSeeds',
  39. 'publicKeySerializer().serialize(programId)',
  40. "string({ size: 'variable' }).serialize('bar')",
  41. 'publicKeySerializer().serialize(seeds.authority)',
  42. ]);
  43. });