Skip to content

Commit

Permalink
create js test helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 8, 2024
1 parent 5f21cd4 commit 49a780b
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 158 deletions.
9 changes: 1 addition & 8 deletions clients/js/src/generated/instructions/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ export type CreateCollectionInstructionAccounts = {
updateAuthority?: PublicKey | Pda;
/** The account paying for the storage fees */
payer?: Signer;
/** The owner of the new asset. Defaults to the authority if not present. */
owner?: PublicKey | Pda;
/** The system program */
systemProgram?: PublicKey | Pda;
};
Expand Down Expand Up @@ -114,13 +112,8 @@ export function createCollection(
isWritable: true as boolean,
value: input.payer ?? null,
},
owner: {
index: 3,
isWritable: false as boolean,
value: input.owner ?? null,
},
systemProgram: {
index: 4,
index: 3,
isWritable: false as boolean,
value: input.systemProgram ?? null,
},
Expand Down
190 changes: 189 additions & 1 deletion clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,193 @@
/* eslint-disable import/no-extraneous-dependencies */
import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-tests';
import { mplCore } from '../src';
import { Assertions } from 'ava';
import { PublicKey, Signer, Umi, generateSigner, publicKey } from '@metaplex-foundation/umi';
import {
Collection,
DataState,
Key,
create,
fetchAssetWithPlugins,
fetchCollectionWithPlugins,
mplCore,
PluginArgs,
createCollection as baseCreateCollection,
CollectionWithPlugins,
AssetWithPlugins,
UpdateAuthority,
Plugin,
} from '../src';

export const createUmi = async () => (await basecreateUmi()).use(mplCore());

export type CreateAssetHelperArgs = {
owner?: PublicKey | Signer;
payer?: Signer;
asset?: Signer
dataState?: DataState;
name?: string;
uri?: string;
updateAuthority?: PublicKey | Signer
collection?: PublicKey
// TODO use PluginList type here
plugins?: PluginArgs[]
}

export const DEFAULT_ASSET = {
name: 'Test Asset',
uri: 'https://example.com/asset',
}

export const DEFAULT_COLLECTION = {
name: 'Test Collection',
uri: 'https://example.com/collection',
}

export const createAsset = async (
umi: Umi,
input: CreateAssetHelperArgs
) => {
const payer = input.payer || umi.identity;
const owner = publicKey(input.owner || input.payer || umi.identity);
const asset = input.asset || generateSigner(umi)
const updateAuthority = publicKey(input.updateAuthority || payer)
await create(umi, {
owner,
payer,
dataState: input.dataState || DataState.AccountState,
asset,
updateAuthority,
name: input.name || DEFAULT_ASSET.name,
uri: input.uri || DEFAULT_ASSET.uri,
plugins: input.plugins || [],
collection: input.collection,
}).sendAndConfirm(umi);

return fetchAssetWithPlugins(umi, publicKey(asset));
};

export type CreateCollectionHelperArgs = {
payer?: Signer;
collection?: Signer
name?: string;
uri?: string;
updateAuthority?: PublicKey | Signer
// TODO use CollectionPluginList type here
plugins?: PluginArgs[]
}

export const createCollection = async (umi: Umi, input:CreateCollectionHelperArgs) => {
const payer = input.payer || umi.identity;
const collection = input.collection || generateSigner(umi);
const updateAuthority = publicKey(input.updateAuthority || payer);
await baseCreateCollection(umi, {
...DEFAULT_COLLECTION,
collection,
payer,
updateAuthority,
plugins: input.plugins || []
}).sendAndConfirm(umi);

return fetchCollectionWithPlugins(umi, publicKey(collection));
}

export const createAssetWithCollection: (
umi: Umi,
assetInput: CreateAssetHelperArgs & { collection?: Collection },
collectionInput?: CreateCollectionHelperArgs
) => Promise<{ asset: AssetWithPlugins; collection: CollectionWithPlugins }> = async (umi, assetInput, collectionInput = {}) => {
const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, assetInput.collection.publicKey) : await createCollection(umi, {
payer: assetInput.payer,
updateAuthority: assetInput.updateAuthority,
...collectionInput
});

const asset = await createAsset(umi, {...assetInput, collection: collection.publicKey});

return {
asset,
collection
}
};

export const assertAsset = async (
t: Assertions,
umi: Umi,
input: {
asset: PublicKey | Signer;
owner: PublicKey | Signer;
updateAuthority: UpdateAuthority;
name?: string | RegExp;
uri?: string | RegExp;
// TODO replace with remapped PluginList type
plugins?: Plugin[]
}
) => {
const assetAddress = publicKey(input.asset);
const owner = publicKey(input.owner);
const { name, uri, updateAuthority } = input;
const asset = await fetchAssetWithPlugins(umi, assetAddress);

// Name.
if (typeof name === 'string') t.is(asset.name, name);
else if (name !== undefined) t.regex(asset.name, name);

// Uri.
if (typeof uri === 'string') t.is(asset.uri, uri);
else if (uri !== undefined) t.regex(asset.uri, uri);

t.like(asset, <AssetWithPlugins>{
key: Key.Asset,
publicKey: assetAddress,
owner,
updateAuthority,
// TODO test plugins
});

};

export const assertCollection = async (
t: Assertions,
umi: Umi,
input: {
collection: PublicKey | Signer;
updateAuthority: PublicKey | Signer;
name?: string | RegExp;
uri?: string | RegExp;
numMinted?: number;
currentSize?: number;
// TODO replace with remapped PluginList type
plugins?: Plugin[]
}
) => {
const collectionAddress = publicKey(input.collection);
const updateAuthority = publicKey(input.updateAuthority);
const { name, uri, numMinted, currentSize } = input;
const collection = await fetchCollectionWithPlugins(umi, collectionAddress);

// Name.
if (typeof name === 'string') t.is(collection.name, name);
else if (name !== undefined) t.regex(collection.name, name);

// Uri.
if (typeof uri === 'string') t.is(collection.uri, uri);
else if (uri !== undefined) t.regex(collection.uri, uri);

const testObj = <CollectionWithPlugins>{
key: Key.Collection,
publicKey: collectionAddress,
updateAuthority,
// TODO test plugins
}

if(numMinted !== undefined) {
testObj.numMinted = numMinted;
};

if(currentSize !== undefined) {
testObj.currentSize = currentSize;
}

t.like(collection, testObj);

};
25 changes: 8 additions & 17 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,23 @@ import {
getAssetAccountDataSerializer,
updateAuthority,
} from '../src';
import { createUmi } from './_setup';
import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup';

test('it can create a new asset in account state', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const assetAddress = generateSigner(umi);

// When we create a new account.
await create(umi, {
dataState: DataState.AccountState,
await createAsset(umi, {
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);
})

// Then an account was created with the correct data.
const asset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", asset);
t.like(asset, <Asset>{
publicKey: assetAddress.publicKey,
await assertAsset(t, umi, {
...DEFAULT_ASSET,
asset: assetAddress.publicKey,
owner: umi.identity,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});
})
});

test('it can create a new asset with a different payer', async (t) => {
Expand Down
Loading

0 comments on commit 49a780b

Please sign in to comment.