-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
ERC721URIStorage.test.js
121 lines (93 loc) · 4.19 KB
/
ERC721URIStorage.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const baseURI = 'https://api.example.com/v1/';
const otherBaseURI = 'https://api.example.com/v2/';
const sampleUri = 'mock://mytoken';
const tokenId = 1n;
const nonExistentTokenId = 2n;
async function fixture() {
const [owner] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721URIStorageMock', [name, symbol]);
return { owner, token };
}
describe('ERC721URIStorage', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
shouldSupportInterfaces(['0x49064906']);
describe('token URI', function () {
beforeEach(async function () {
await this.token.$_mint(this.owner, tokenId);
});
it('it is empty by default', async function () {
expect(await this.token.tokenURI(tokenId)).to.equal('');
});
it('reverts when queried for non existent token id', async function () {
await expect(this.token.tokenURI(nonExistentTokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(nonExistentTokenId);
});
it('can be set for a token id', async function () {
await this.token.$_setTokenURI(tokenId, sampleUri);
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
});
it('setting the uri emits an event', async function () {
await expect(this.token.$_setTokenURI(tokenId, sampleUri))
.to.emit(this.token, 'MetadataUpdate')
.withArgs(tokenId);
});
it('setting the uri for non existent token id is allowed', async function () {
await expect(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri))
.to.emit(this.token, 'MetadataUpdate')
.withArgs(nonExistentTokenId);
// value will be accessible after mint
await this.token.$_mint(this.owner, nonExistentTokenId);
expect(await this.token.tokenURI(nonExistentTokenId)).to.equal(sampleUri);
});
it('base URI can be set', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.$_baseURI()).to.equal(baseURI);
});
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.$_setTokenURI(tokenId, sampleUri);
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + sampleUri);
});
it('token URI can be changed by changing the base URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.$_setTokenURI(tokenId, sampleUri);
await this.token.setBaseURI(otherBaseURI);
expect(await this.token.tokenURI(tokenId)).to.equal(otherBaseURI + sampleUri);
});
it('tokenId is appended to base URI for tokens with no URI', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
});
it('tokens without URI can be burnt ', async function () {
await this.token.$_burn(tokenId);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
});
it('tokens with URI can be burnt ', async function () {
await this.token.$_setTokenURI(tokenId, sampleUri);
await this.token.$_burn(tokenId);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
});
it('tokens URI is kept if token is burnt and reminted ', async function () {
await this.token.$_setTokenURI(tokenId, sampleUri);
await this.token.$_burn(tokenId);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
await this.token.$_mint(this.owner, tokenId);
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
});
});
});