@@ -5,7 +5,7 @@ import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
5
5
import { Contract } from "ethers" ;
6
6
7
7
// Import the contract type
8
- import { XXXToken } from "../typechain-types" ;
8
+ import { XXXToken , XXXTokenV2 } from "../typechain-types" ;
9
9
10
10
describe ( "XXXToken" , function ( ) {
11
11
// Test roles
@@ -157,19 +157,29 @@ describe("XXXToken", function () {
157
157
} ) ;
158
158
} ) ;
159
159
160
- // describe("Upgrading", function () {
161
- // it("Should allow upgrader to upgrade the contract", async function () {
162
- // const XXXTokenV2 = await ethers.getContractFactory("XXXTokenV2");
163
- // await expect(
164
- // token.connect(upgrader).upgradeTo(await XXXTokenV2.getAddress())
165
- // ).to.not.be.reverted;
166
- // });
167
-
168
- // it("Should not allow non-upgrader to upgrade the contract", async function () {
169
- // const XXXTokenV2 = await ethers.getContractFactory("XXXTokenV2");
170
- // await expect(
171
- // token.connect(user1).upgradeTo(await XXXTokenV2.getAddress())
172
- // ).to.be.revertedWithCustomError(token, "AccessControlUnauthorizedAccount");
173
- // });
174
- // });
160
+ describe ( "Upgrading" , function ( ) {
161
+ it ( "Should allow upgrader to upgrade the contract" , async function ( ) {
162
+ const XXXTokenV2Factory = await ethers . getContractFactory ( "XXXTokenV2" ) ;
163
+ const upgraded = await upgrades . upgradeProxy ( await token . getAddress ( ) , XXXTokenV2Factory ) as unknown as XXXTokenV2 ;
164
+ await upgraded . initializeV2 ( ) ;
165
+ expect ( await upgraded . version ( ) ) . to . equal ( 2 ) ;
166
+ } ) ;
167
+
168
+ it ( "Should not allow initializing V2 twice" , async function ( ) {
169
+ const XXXTokenV2Factory = await ethers . getContractFactory ( "XXXTokenV2" ) ;
170
+ const upgraded = await upgrades . upgradeProxy ( await token . getAddress ( ) , XXXTokenV2Factory ) as unknown as XXXTokenV2 ;
171
+ await upgraded . initializeV2 ( ) ;
172
+
173
+ await expect (
174
+ upgraded . initializeV2 ( )
175
+ ) . to . be . revertedWithCustomError ( upgraded , "InvalidInitialization" ) ;
176
+ } ) ;
177
+
178
+ it ( "Should not allow non-upgrader to upgrade the contract" , async function ( ) {
179
+ const XXXTokenV2Factory = await ethers . getContractFactory ( "XXXTokenV2" ) ;
180
+ await expect (
181
+ upgrades . upgradeProxy ( await token . getAddress ( ) , XXXTokenV2Factory . connect ( user1 ) )
182
+ ) . to . be . revertedWithCustomError ( token , "AccessControlUnauthorizedAccount" ) ;
183
+ } ) ;
184
+ } ) ;
175
185
} ) ;
0 commit comments