|
| 1 | +import assertRevert from '../../helpers/assertRevert'; |
| 2 | +const BigNumber = web3.BigNumber; |
| 3 | +const ERC721TokenMetadata = artifacts.require('ERC721TokenMetadataMock.sol'); |
| 4 | + |
| 5 | +require('chai') |
| 6 | + .use(require('chai-as-promised')) |
| 7 | + .use(require('chai-bignumber')(BigNumber)) |
| 8 | + .should(); |
| 9 | + |
| 10 | +contract('ERC721TokenMetadata', accounts => { |
| 11 | + let token = null; |
| 12 | + const _name = 'Non Fungible Token'; |
| 13 | + const _symbol = 'NFT'; |
| 14 | + const _firstTokenId = 1; |
| 15 | + const _secondTokenId = 2; |
| 16 | + const _unknownTokenId = 3; |
| 17 | + const _creator = accounts[0]; |
| 18 | + |
| 19 | + beforeEach(async function () { |
| 20 | + token = await ERC721TokenMetadata.new(_name, _symbol, { from: _creator }); |
| 21 | + await token.mint(_creator, _firstTokenId, { from: _creator }); |
| 22 | + await token.mint(_creator, _secondTokenId, { from: _creator }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('tokenMetadata', function () { |
| 26 | + describe('when no metadata was set', function () { |
| 27 | + it('the given token has no metadata', async function () { |
| 28 | + const metadata = await token.tokenMetadata(_firstTokenId); |
| 29 | + |
| 30 | + metadata.should.be.equal(''); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('when some metadata was set', function () { |
| 35 | + it('returns the metadata of the given token', async function () { |
| 36 | + await token.setTokenMetadata(_firstTokenId, 'dummy metadata', { from: _creator }); |
| 37 | + |
| 38 | + const metadata = await token.tokenMetadata(_firstTokenId); |
| 39 | + |
| 40 | + metadata.should.be.equal('dummy metadata'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('setTokenMetadata', function () { |
| 46 | + describe('when the sender is not the token owner', function () { |
| 47 | + const sender = accounts[1]; |
| 48 | + |
| 49 | + it('reverts', async function () { |
| 50 | + await assertRevert(token.setTokenMetadata(_firstTokenId, 'new metadata', { from: sender })); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('when the sender is the owner of the token', function () { |
| 55 | + const sender = _creator; |
| 56 | + |
| 57 | + describe('when the given token ID was tracked by this contract before', function () { |
| 58 | + const tokenId = _firstTokenId; |
| 59 | + |
| 60 | + it('updates the metadata of the given token ID', async function () { |
| 61 | + await token.setTokenMetadata(_firstTokenId, 'new metadata', { from: sender }); |
| 62 | + |
| 63 | + const metadata = await token.tokenMetadata(tokenId); |
| 64 | + |
| 65 | + metadata.should.be.equal('new metadata'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('emits a metadata updated event', async function () { |
| 69 | + const { logs } = await token.setTokenMetadata(tokenId, 'new metadata', { from: sender }); |
| 70 | + |
| 71 | + logs.length.should.be.equal(1); |
| 72 | + logs[0].event.should.be.eq('MetadataUpdated'); |
| 73 | + logs[0].args._owner.should.be.equal(sender); |
| 74 | + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); |
| 75 | + logs[0].args._newMetadata.should.be.equal('new metadata'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('when the given token ID was not tracked by this contract before', function () { |
| 80 | + const tokenId = _unknownTokenId; |
| 81 | + |
| 82 | + it('reverts', async function () { |
| 83 | + await assertRevert(token.setTokenMetadata(tokenId, 'new metadata'), { from: sender }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments