account.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { accountLinkNode, accountNode, instructionByteDeltaNode, instructionNode, programNode } from '@kinobi-so/nodes';
  2. import { visit } from '@kinobi-so/visitors-core';
  3. import { test } from 'vitest';
  4. import { getRenderMapVisitor } from '../../src';
  5. import { renderMapContains, renderMapContainsImports } from '../_setup';
  6. test('it imports functions from the linked account', async () => {
  7. // Given the following node.
  8. const node = programNode({
  9. accounts: [accountNode({ name: 'counter', size: 42 })],
  10. instructions: [
  11. instructionNode({
  12. byteDeltas: [instructionByteDeltaNode(accountLinkNode('counter'))],
  13. name: 'createCounter',
  14. }),
  15. ],
  16. name: 'myProgram',
  17. publicKey: '1111',
  18. });
  19. // When we render it.
  20. const renderMap = visit(node, getRenderMapVisitor());
  21. // Then we expect the following to be exported.
  22. await renderMapContains(renderMap, 'instructions/createCounter.ts', ['getCounterSize()']);
  23. // And we expect the following imports.
  24. await renderMapContainsImports(renderMap, 'instructions/createCounter.ts', {
  25. '../accounts': ['getCounterSize'],
  26. });
  27. });
  28. test('it can override the import of a linked account', async () => {
  29. // Given the following node.
  30. const node = programNode({
  31. accounts: [accountNode({ name: 'counter', size: 42 })],
  32. instructions: [
  33. instructionNode({
  34. byteDeltas: [instructionByteDeltaNode(accountLinkNode('counter'))],
  35. name: 'createCounter',
  36. }),
  37. ],
  38. name: 'myProgram',
  39. publicKey: '1111',
  40. });
  41. // When we render it using a custom import.
  42. const renderMap = visit(
  43. node,
  44. getRenderMapVisitor({
  45. linkOverrides: {
  46. accounts: { counter: 'hooked' },
  47. },
  48. }),
  49. );
  50. // Then we expect the following to be exported.
  51. await renderMapContains(renderMap, 'instructions/createCounter.ts', ['getCounterSize()']);
  52. // And we expect the imports to be overridden.
  53. await renderMapContainsImports(renderMap, 'instructions/createCounter.ts', {
  54. '../../hooked': ['getCounterSize'],
  55. });
  56. });