12import { ApiPromise, Keyring } from '@polkadot/api';3import { IKeyringPair } from '@polkadot/types/types';4import BN from 'bn.js';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import usingApi, { submitTransactionAsync } from './substrate/substrate-api';8import { ICollectionInterface } from './types';9import { createCollectionExpectSuccess, destroyCollectionExpectSuccess, getCreateItemResult } from './util/helpers';1011chai.use(chaiAsPromised);12const expect = chai.expect;1314let alice: IKeyringPair;15let collectionIdForTesting: number;1617181920212223const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)24 : Promise<ICollectionInterface | null> => {25 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;26};2728const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {29 30 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();31};3233describe('hooks', () => {34 before(async () => {35 await usingApi(async () => {36 const keyring = new Keyring({ type: 'sr25519' });37 alice = keyring.addFromUri('//Alice');38 });39 });40 it('choose or create collection for testing', async () => {41 await usingApi(async () => {42 collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});43 });44 });45});4647describe('setSchemaVersion positive', () => {48 let tx;49 before(async () => {50 await usingApi(async () => {51 const keyring = new Keyring({ type: 'sr25519' });52 alice = keyring.addFromUri('//Alice');53 });54 });55 it('execute setSchemaVersion with image url and unique ', async () => {56 await usingApi(async (api: ApiPromise) => {57 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');58 const events = await submitTransactionAsync(alice, tx);59 const result = getCreateItemResult(events);60 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);61 62 expect(result.success).to.be.true;63 64 expect(collectionInfo).to.be.exist;65 66 expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');67 });68 });6970 it('validate schema version with just entered data', async () => {71 await usingApi(async (api: ApiPromise) => {72 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');73 const events = await submitTransactionAsync(alice, tx);74 const result = getCreateItemResult(events);75 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);76 77 expect(result.success).to.be.true;78 79 expect(collectionInfo).to.be.exist;80 81 expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');82 });83 });84});8586describe('setSchemaVersion negative', () => {87 let tx;88 before(async () => {89 await usingApi(async () => {90 const keyring = new Keyring({ type: 'sr25519' });91 alice = keyring.addFromUri('//Alice');92 });93 });94 it('execute setSchemaVersion for not exists collection', async () => {95 await usingApi(async (api: ApiPromise) => {96 const collectionCount = await getCreatedCollectionCount(api);97 const nonExistedCollectionId = collectionCount + 1;98 tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');99 try {100 await submitTransactionAsync(alice, tx);101 } catch (e) {102 103 expect(e).to.be.exist;104 }105 });106 });107108 it('execute setSchemaVersion with not correct schema version', async () => {109 await usingApi(async (api: ApiPromise) => {110 try {111 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');112 await submitTransactionAsync(alice, tx);113 } catch (e) {114 115 expect(e).to.be.exist;116 }117 });118 });119120 it('execute setSchemaVersion for deleted collection', async () => {121 await usingApi(async (api: ApiPromise) => {122 await destroyCollectionExpectSuccess(collectionIdForTesting);123 try {124 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');125 await submitTransactionAsync(alice, tx);126 } catch (e) {127 128 expect(e).to.be.exist;129 }130 });131 });132});