Skip to content

Instantly share code, notes, and snippets.

@polluterofminds
Created July 19, 2021 14:23
Show Gist options
  • Save polluterofminds/6783071070aec164d6748cfd088017c7 to your computer and use it in GitHub Desktop.
Save polluterofminds/6783071070aec164d6748cfd088017c7 to your computer and use it in GitHub Desktop.
Tests
import { init, mint_to, get_token_uri, get_token_owner, grant_access, check_access, revoke_access, transfer_from } from '../main';
import { VMContext } from "near-sdk-as";
const ALICE = 'alice'
const BOB = 'bob'
const CAROL = 'carol'
const METADATA_URI = 'ipfs://QmTUj7kVQLxvnK8adcVhRV3GR9xxrtmHXddtKJT8AM9MqP'
describe("Initializing Contract", () => {
it("should not allow init to be called after contract is deployed", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
expect(() => {
VMContext.setPredecessor_account_id(BOB)
init()
}).toThrow()
})
})
describe("Minting", () => {
it("should mint a new token to non-contract owner", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
const tokenId = mint_to(CAROL, METADATA_URI)
expect(tokenId).toBe(1);
});
it("should get correct token owner", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
const tokenId = mint_to(CAROL, METADATA_URI)
expect(tokenId).toBe(1);
const owner = get_token_owner(tokenId)
expect(owner).toBe(CAROL)
})
it("should get the tokenURI", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
const tokenId = mint_to(CAROL, METADATA_URI)
const metadataUri = get_token_uri(tokenId)
expect(metadataUri).toBe(METADATA_URI)
})
})
describe("Access", () => {
it("should successfuly grant token acces to another user", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(CAROL)
mint_to(CAROL, METADATA_URI)
grant_access(BOB)
VMContext.setPredecessor_account_id(BOB)
expect(check_access(CAROL)).toBe(true)
});
it("should revoke access for a user", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
mint_to(BOB, METADATA_URI)
grant_access(CAROL)
revoke_access(CAROL)
VMContext.setPredecessor_account_id(CAROL)
expect(check_access(BOB)).toBe(false)
})
});
describe("Transfers", () => {
it("should transfer token owner by Alice to Bob", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
const carolToken = mint_to(CAROL, METADATA_URI)
expect(get_token_owner(carolToken)).toBe(CAROL)
expect(get_token_owner(carolToken)).not.toBe(BOB)
VMContext.setPredecessor_account_id(CAROL)
transfer_from(CAROL, BOB, carolToken)
expect(get_token_owner(carolToken)).toBe(BOB)
expect(get_token_owner(carolToken)).not.toBe(CAROL)
})
it("allows escrowed tokens to be transferred by escrowee", () => {
VMContext.setPredecessor_account_id(ALICE)
init()
VMContext.setPredecessor_account_id(BOB)
const carolToken = mint_to(CAROL, METADATA_URI)
expect(get_token_owner(carolToken)).toBe(CAROL)
expect(get_token_owner(carolToken)).not.toBe(BOB)
VMContext.setPredecessor_account_id(BOB)
transfer_from(CAROL, BOB, carolToken)
expect(get_token_owner(carolToken)).toBe(BOB)
expect(get_token_owner(carolToken)).not.toBe(CAROL)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment