Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Added tests; minor refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Bartosz Solka <[email protected]>
  • Loading branch information
BartoszSolkaArianelabs committed Feb 8, 2024
1 parent 4263d25 commit 6b80f08
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/HederaNFTSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class HederaNFTSDK {
nftId: NftId;
amount: number;
batchSize?: number;
supplyKey: PrivateKey;
supplyKey?: PrivateKey;
mirrorNodeUrl?: string;
}) {
return increaseNFTSupply({
Expand All @@ -130,7 +130,7 @@ export class HederaNFTSDK {
nftId: nftId,
amount: amount,
batchSize,
supplyKey,
supplyKey: supplyKey || PrivateKey.fromString(this.privateKey),
mirrorNodeUrl,
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/functions/mintSharedMetadataFunction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MintedNFTType, MintTokenType } from '../types/mintToken';
import { mintToken } from './mintToken';
import { validateProps } from '../utils/validateProps';
import { MintingError } from '../utils/mintingError';
import { dictionary } from '../utils/constants/dictionary';
import { MintingError } from '../utils/mintingError';
import { validatePropsForSharedNFTMinting } from '../utils/validateProps';
import { mintToken } from './mintToken';

export const mintSharedMetadataFunction = async ({
client,
Expand All @@ -12,7 +12,7 @@ export const mintSharedMetadataFunction = async ({
metaData,
supplyKey,
}: MintTokenType) => {
validateProps({ tokenId, amount, metaData, supplyKey, batchSize });
validatePropsForSharedNFTMinting({ tokenId, amount, metaData, supplyKey, batchSize });

const mintedNFTs: MintedNFTType[] = [];
// Example if amount = 8 and batchSize = 5. NumberOfCalls should be 2. So 8/5 = 1.6. Math.ceil(1.6) = 2. Because Math.ceil rounds up to the next largest integer.
Expand Down
2 changes: 1 addition & 1 deletion src/types/validateProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client, NftId, PrivateKey } from '@hashgraph/sdk';

export type PropsType = {
export type sharedMintingValidationProps = {
batchSize?: number;
tokenId?: string;
amount?: number;
Expand Down
14 changes: 7 additions & 7 deletions src/utils/validateProps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
PropsType,
sharedMintingValidationProps,
validateCreateCollectionProps,
uniqueMintingValidationProps,
increaseNFTSupplyValidationProps,
} from '../types/validateProps';
import { dictionary } from './constants/dictionary';

export const validateProps = (props: PropsType) => {
export const validatePropsForSharedNFTMinting = (props: sharedMintingValidationProps) => {
validateSupplyKey(props);
validateBatchSize(props);
validateTokenId(props);
Expand Down Expand Up @@ -68,21 +68,21 @@ const validateClient = (props: validateCreateCollectionProps) => {
}
};

const validateSupplyKey = (props: PropsType) => {
const validateSupplyKey = (props: sharedMintingValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'supplyKey')) {
if (!props.supplyKey) throw new Error(dictionary.hederaActions.supplyKeyRequired);
}
};

const validateBatchSize = (props: PropsType) => {
const validateBatchSize = (props: sharedMintingValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'batchSize')) {
if (!props.batchSize) throw new Error(dictionary.mintToken.batchSizeUndefined);
if (props.batchSize > 10) throw new Error(dictionary.hederaActions.maxBatchSize);
if (props.batchSize < 1) throw new Error(dictionary.hederaActions.minBatchSize);
}
};

const validateTokenId = (props: PropsType) => {
const validateTokenId = (props: sharedMintingValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'tokenId')) {
if (!props.tokenId) throw new Error(dictionary.hederaActions.tokenIdRequired);
}
Expand All @@ -94,13 +94,13 @@ const validateNFTId = (props: increaseNFTSupplyValidationProps) => {
}
};

const validateAmount = (props: PropsType) => {
const validateAmount = (props: sharedMintingValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'amount')) {
if (!props.amount || props.amount < 1) throw new Error(dictionary.hederaActions.minAmount);
}
};

const validateMetaData = (props: PropsType) => {
const validateMetaData = (props: sharedMintingValidationProps) => {
if (Object.prototype.hasOwnProperty.call(props, 'metaData')) {
if (!props.metaData) throw new Error(dictionary.hederaActions.metadataRequired);
}
Expand Down
74 changes: 62 additions & 12 deletions test/unit/increaseNFTSupply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ jest.mock('../../src/utils/validateProps', () => ({
validatePropsForIncreaseNFTSupply: jest.fn(),
}));
const mockedAxios = axios as jest.Mocked<typeof axios>;
const metadata = 'testMetadata';
const mockResponse = {
//encoding 'testMetadata' as base64 encoded string
metadata: btoa(metadata),
};
beforeAll(() => {
mockedAxios.get.mockResolvedValue({
data: mockResponse
});
});

beforeEach(() => {
jest.clearAllMocks();
});

describe('increaseNFTSupply', () => {
const mockMetaData = {
// mock metadata object
};

const mockNftId = {
tokenId: TokenId.fromString('0.0.453'),
serial: 1,
Expand All @@ -32,7 +38,7 @@ describe('increaseNFTSupply', () => {

const generatedSupplyKey = PrivateKey.generate();
const mockIncreaseNFTSupplyType: IncreaseNFTSupplyType = {
client: {ledgerId: LedgerId.TESTNET} as Client,
client: { ledgerId: LedgerId.TESTNET } as Client,
network: 'testnet',
nftId: mockNftId,
amount: 10,
Expand All @@ -42,13 +48,6 @@ describe('increaseNFTSupply', () => {
};

it('should validate props before increasing NFT supply', async () => {
const mockResponse = {
metadata: "testMetadata"
};
mockedAxios.get.mockResolvedValue({
data: mockResponse
});

await increaseNFTSupply(mockIncreaseNFTSupplyType);

expect((validatePropsForIncreaseNFTSupply as jest.Mock)).toHaveBeenCalledWith({
Expand All @@ -58,4 +57,55 @@ describe('increaseNFTSupply', () => {
batchSize: 5,
});
});

it('should increase supply when called with valid props', async () => {
await increaseNFTSupply(mockIncreaseNFTSupplyType);

expect(mockedAxios.get).toHaveBeenCalledWith('mirrorNodeUrl/tokens/0.0.453/nfts/1');
expect(mockedAxios.get).toHaveBeenCalledTimes(1);
expect((require('../../src/functions/mintSharedMetadataFunction').mintSharedMetadataFunction as jest.Mock)).toHaveBeenCalledTimes(1);
});

it('should call mintSharedMetadataFunction with the correct parameters', async () => {
await increaseNFTSupply(mockIncreaseNFTSupplyType);

expect((require('../../src/functions/mintSharedMetadataFunction').mintSharedMetadataFunction as jest.Mock)).toHaveBeenCalledWith({
client: mockIncreaseNFTSupplyType.client,
tokenId: mockIncreaseNFTSupplyType.nftId.tokenId.toString(),
amount: mockIncreaseNFTSupplyType.amount,
batchSize: mockIncreaseNFTSupplyType.batchSize,
metaData: metadata,
supplyKey: mockIncreaseNFTSupplyType.supplyKey,
});
});

it('should call passed mirrorNodeUrl when provided', async () => {
await increaseNFTSupply(mockIncreaseNFTSupplyType);

expect(mockedAxios.get).toHaveBeenCalledWith('mirrorNodeUrl/tokens/0.0.453/nfts/1');
});

it('should get correct mirror node url for mainnet', async () => {
const mockIncreaseNFTSupplyTypeMainnet = {
...mockIncreaseNFTSupplyType,
network: 'mainnet',
mirrorNodeUrl: undefined,
};

await increaseNFTSupply(mockIncreaseNFTSupplyTypeMainnet);

expect(mockedAxios.get).toHaveBeenCalledWith('https://mainnet-public.mirrornode.hedera.com/api/v1/tokens/0.0.453/nfts/1');
});

it('should get correct mirror node url for testnet', async () => {
const mockIncreaseNFTSupplyTypeTestnet = {
...mockIncreaseNFTSupplyType,
network: 'testnet',
mirrorNodeUrl: undefined,
};

await increaseNFTSupply(mockIncreaseNFTSupplyTypeTestnet);

expect(mockedAxios.get).toHaveBeenCalledWith('https://testnet.mirrornode.hedera.com/api/v1/tokens/0.0.453/nfts/1');
});
});
78 changes: 63 additions & 15 deletions test/unit/validateProps.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PrivateKey } from '@hashgraph/sdk';
import { NftId, PrivateKey } from '@hashgraph/sdk';
import {
validateProps,
validatePropsForSharedNFTMinting,
validatePropsForCreateCollection,
validatePropsForIncreaseNFTSupply,
validatePropsForUniqueNFTMinting,
} from '../../src/utils/validateProps';
import { dictionary } from '../../src/utils/constants/dictionary';
Expand All @@ -10,46 +11,46 @@ import { myAccountId, myPrivateKey } from '../__mocks__/consts';

describe('validateProps_Value_Errors', () => {
it('should throw an error if batchSize is greater than 10', () => {
expect(() => validateProps({ batchSize: 11 })).toThrow(dictionary.hederaActions.maxBatchSize);
expect(() => validatePropsForSharedNFTMinting({ batchSize: 11 })).toThrow(dictionary.hederaActions.maxBatchSize);
});

it('should throw an error if batchSize is less than 1', () => {
expect(() => validateProps({ batchSize: -1 })).toThrow(dictionary.hederaActions.minBatchSize);
expect(() => validatePropsForSharedNFTMinting({ batchSize: -1 })).toThrow(dictionary.hederaActions.minBatchSize);
});

it('should throw an error if tokenId is not provided', () => {
expect(() => validateProps({ tokenId: '' })).toThrow(dictionary.hederaActions.tokenIdRequired);
expect(() => validatePropsForSharedNFTMinting({ tokenId: '' })).toThrow(dictionary.hederaActions.tokenIdRequired);
});

it('should throw an error if metaData is not provided', () => {
expect(() => validateProps({ metaData: '' })).toThrow(
expect(() => validatePropsForSharedNFTMinting({ metaData: '' })).toThrow(
dictionary.hederaActions.metadataRequired
);
});

it('should throw an error if supplyKey is not provided', () => {
expect(() => validateProps({ supplyKey: undefined })).toThrow(
expect(() => validatePropsForSharedNFTMinting({ supplyKey: undefined })).toThrow(
dictionary.hederaActions.supplyKeyRequired
);
});
});

describe('validateProps_MultipleProps_Errors', () => {
it('should throw an error if batchSize is undefined and tokenId is valid', () => {
expect(() => validateProps({ batchSize: undefined, tokenId: 'token123' })).toThrow(
expect(() => validatePropsForSharedNFTMinting({ batchSize: undefined, tokenId: 'token123' })).toThrow(
dictionary.mintToken.batchSizeUndefined
);
});

it('should throw an error if amount is undefined and metaData is valid', () => {
expect(() => validateProps({ amount: undefined, metaData: 'metadata123' })).toThrow(
expect(() => validatePropsForSharedNFTMinting({ amount: undefined, metaData: 'metadata123' })).toThrow(
dictionary.hederaActions.minAmount
);
});

it('should throw an error if supplyKey is undefined and pathToMetadataURIsFile is valid', () => {
expect(() =>
validateProps({
validatePropsForSharedNFTMinting({
supplyKey: undefined,
batchSize: 9,
})
Expand All @@ -59,24 +60,24 @@ describe('validateProps_MultipleProps_Errors', () => {

describe('validateProps_Success', () => {
it('should not throw an error if batchSize is a number between 1 and 10', () => {
expect(() => validateProps({ batchSize: 5 })).not.toThrow();
expect(() => validatePropsForSharedNFTMinting({ batchSize: 5 })).not.toThrow();
});

it('should not throw an error if tokenId is a string', () => {
expect(() => validateProps({ tokenId: 'token123' })).not.toThrow();
expect(() => validatePropsForSharedNFTMinting({ tokenId: 'token123' })).not.toThrow();
});

it('should not throw an error if amount is a number greater than 0', () => {
expect(() => validateProps({ amount: 5 })).not.toThrow();
expect(() => validatePropsForSharedNFTMinting({ amount: 5 })).not.toThrow();
});

it('should not throw an error if metaData is a string', () => {
expect(() => validateProps({ metaData: 'metadata123' })).not.toThrow();
expect(() => validatePropsForSharedNFTMinting({ metaData: 'metadata123' })).not.toThrow();
});

it('should not throw an error if supplyKey is a PrivateKey', () => {
const privateKey = PrivateKey.generate();
expect(() => validateProps({ supplyKey: privateKey })).not.toThrow();
expect(() => validatePropsForSharedNFTMinting({ supplyKey: privateKey })).not.toThrow();
});
});

Expand Down Expand Up @@ -176,3 +177,50 @@ describe('validatePropsForCreateCollection', () => {
).toThrow(new Error(dictionary.createCollection.clientRequired));
});
});

describe('validatePropsForIncreaseNFTSupply', () => {
it('should throw an error if nftId is not provided', () => {
expect(() =>
validatePropsForIncreaseNFTSupply({
amount: 10,
batchSize: 5,
supplyKey: PrivateKey.generate(),
nftId: undefined,
})
).toThrow(new Error(dictionary.hederaActions.nftIdRequired));
});

it('should throw an error if amount is not provided', () => {
expect(() =>
validatePropsForIncreaseNFTSupply({
nftId: NftId.fromString('0.0.453/1'),
batchSize: 5,
supplyKey: PrivateKey.generate(),
amount: undefined,
})
).toThrow(new Error(dictionary.hederaActions.minAmount));
});

it('should throw an error if supplyKey is not provided', () => {
expect(() =>
validatePropsForIncreaseNFTSupply({
nftId: NftId.fromString('0.0.453/1'),
amount: 10,
batchSize: 5,
supplyKey: undefined,
})
).toThrow(new Error(dictionary.hederaActions.supplyKeyRequired));
});

it('should throw an error if batchSize is not provided', () => {
expect(() =>
validatePropsForIncreaseNFTSupply({
nftId: NftId.fromString('0.0.453/1'),
amount: 10,
supplyKey: PrivateKey.generate(),
batchSize: undefined,
})
).toThrow(new Error(dictionary.mintToken.batchSizeUndefined));
});

});

0 comments on commit 6b80f08

Please sign in to comment.