From f3b64de5cd1777abaf9e6121d8387f55a4475a27 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Wed, 31 Jan 2024 15:25:33 +0000 Subject: [PATCH] Add accountsPage test for empty seed PDAs --- .../js-experimental/accountsPage.test.ts | 30 ++++++++++++++++++ .../js-experimental/pdasPage.test.ts | 31 +++++-------------- 2 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 test/renderers/js-experimental/accountsPage.test.ts diff --git a/test/renderers/js-experimental/accountsPage.test.ts b/test/renderers/js-experimental/accountsPage.test.ts new file mode 100644 index 000000000..a8782a91b --- /dev/null +++ b/test/renderers/js-experimental/accountsPage.test.ts @@ -0,0 +1,30 @@ +import test from 'ava'; +import { + accountNode, + pdaLinkNode, + pdaNode, + programNode, + visit, +} from '../../../src'; +import { getRenderMapVisitor } from '../../../src/renderers/js-experimental/getRenderMapVisitor'; +import { renderMapContains } from './_setup'; + +test('it renders PDA helpers for PDA with no seeds', (t) => { + // Given the following program with 1 account and 1 pda with empty seeds. + const node = programNode({ + name: 'myProgram', + publicKey: '1111', + accounts: [accountNode({ name: 'foo', pda: pdaLinkNode('bar') })], + pdas: [pdaNode('bar', [])], + }); + + // When we render it. + const renderMap = visit(node, getRenderMapVisitor()); + + // Then we expect the following fetch helper functions delegating to findBarPda. + renderMapContains(t, renderMap, 'accounts/foo.ts', [ + 'export async function fetchFooFromSeeds', + 'export async function fetchMaybeFooFromSeeds', + 'await findBarPda({ programAddress })', + ]); +}); diff --git a/test/renderers/js-experimental/pdasPage.test.ts b/test/renderers/js-experimental/pdasPage.test.ts index 9b0825127..7070e631d 100644 --- a/test/renderers/js-experimental/pdasPage.test.ts +++ b/test/renderers/js-experimental/pdasPage.test.ts @@ -1,39 +1,22 @@ import test from 'ava'; -import { - accountNode, - pdaLinkNode, - pdaNode, - programNode, - visit, -} from '../../../src'; +import { pdaNode, programNode, visit } from '../../../src'; import { getRenderMapVisitor } from '../../../src/renderers/js-experimental/getRenderMapVisitor'; import { renderMapContains } from './_setup'; test('it renders an empty array seed used on a pda', (t) => { // Given the following program with 1 account and 1 pda with empty seeds. const node = programNode({ - name: 'splToken', - publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', - accounts: [ - accountNode({ - name: 'testAccount', - discriminators: [], - pda: pdaLinkNode('testPda'), - }), - ], - pdas: [ - // Empty array seeds. - pdaNode('testPda', []), - ], + name: 'myProgram', + publicKey: '1111', + pdas: [pdaNode('foo', [])], }); // When we render it. const renderMap = visit(node, getRenderMapVisitor()); - // Then we expect the following function and and empty seeds - // array used on program derived address function. - renderMapContains(t, renderMap, 'pdas/testPda.ts', [ - 'export async function findTestPdaPda', + // Then we expect the following PDA function using an empty seeds array to derive the address. + renderMapContains(t, renderMap, 'pdas/foo.ts', [ + 'export async function findFooPda', 'getProgramDerivedAddress({ programAddress, seeds: [] })', ]); });