@@ -5,6 +5,7 @@ import {Test} from "forge-std/Test.sol";
5
5
import {DeployRaffle} from "../../script/DeployRaffle.s.sol " ;
6
6
import {HelperConfig} from "../../script/HelperConfig.s.sol " ;
7
7
import {Raffle} from "../../src/Raffle.sol " ;
8
+ import {Vm} from "forge-std/Vm.sol " ;
8
9
9
10
contract RaffleTest is Test {
10
11
Raffle public raffle;
@@ -37,6 +38,15 @@ contract RaffleTest is Test {
37
38
callbackGasLimit = config.callbackGasLimit;
38
39
}
39
40
41
+ modifier raffleEntered () {
42
+ vm.prank (PLAYER);
43
+ vm.deal (PLAYER, STARTING_PLAYER_BALANCE);
44
+ raffle.enterRaffle {value: entranceFee}();
45
+ vm.warp (block .timestamp + interval + 1 );
46
+ vm.roll (block .number + 1 );
47
+ _;
48
+ }
49
+
40
50
function testRaffleInitializesInOpesState () public view {
41
51
assert (raffle.getRaffleState () == Raffle.RaffleState.OPEN);
42
52
}
@@ -65,7 +75,7 @@ contract RaffleTest is Test {
65
75
raffle.enterRaffle {value: entranceFee}();
66
76
}
67
77
68
- function testDontAllowPlayersToEnterWhileRaffleIsCalculting () public {
78
+ function testDontAllowPlayersToEnterWhileRaffleIsCalculting () public raffleEntered {
69
79
vm.prank (PLAYER);
70
80
vm.deal (PLAYER, STARTING_PLAYER_BALANCE);
71
81
raffle.enterRaffle {value: entranceFee}();
@@ -87,15 +97,43 @@ contract RaffleTest is Test {
87
97
assert (! upkeepNeeded);
88
98
}
89
99
90
- function testCheckUpkeepReturnsFalseIfRaffleIsntOpen () public {
100
+ function testCheckUpkeepReturnsFalseIfRaffleIsntOpen () public raffleEntered {
101
+ raffle.performUpkeep ("" );
102
+
103
+ (bool upkeepNeeded ,) = raffle.checkUpKeep ("" );
104
+ assert (! upkeepNeeded);
105
+ }
106
+
107
+ // TEST PERFORM UPKEEP
108
+ function testPerformUpkeepCanOnlyRunIfCheckUpkeepIsTrue () public raffleEntered {
109
+ raffle.performUpkeep ("" );
110
+ }
111
+
112
+ function testPerformUpkeepRevertsIfCheckUpkeepIsFalse () public {
113
+ uint256 currentBalance = 0 ;
114
+ uint256 numPlayers = 0 ;
115
+ Raffle.RaffleState rState = raffle.getRaffleState ();
116
+
91
117
vm.prank (PLAYER);
92
118
vm.deal (PLAYER, STARTING_PLAYER_BALANCE);
93
119
raffle.enterRaffle {value: entranceFee}();
94
- vm.warp (block .timestamp + interval + 1 );
95
- vm.roll (block .number + 1 );
120
+ currentBalance += entranceFee;
121
+ numPlayers = 1 ;
122
+
123
+ vm.expectRevert (
124
+ abi.encodeWithSelector (Raffle.Raffle__UpkeepNotNeeded.selector , currentBalance, numPlayers, rState)
125
+ );
96
126
raffle.performUpkeep ("" );
127
+ }
97
128
98
- (bool upkeepNeeded ,) = raffle.checkUpKeep ("" );
99
- assert (! upkeepNeeded);
129
+ function testPerformUpkeepUpdatesRaffleStateAndEmitsRequestId () public raffleEntered {
130
+ vm.recordLogs ();
131
+ raffle.performUpkeep ("" );
132
+ Vm.Log[] memory entries = vm.getRecordedLogs ();
133
+ bytes32 requestId = entries[1 ].topics[1 ];
134
+
135
+ Raffle.RaffleState raffleState = raffle.getRaffleState ();
136
+ assert (uint256 (requestId) > 0 );
137
+ assert (uint256 (raffleState) == 1 );
100
138
}
101
139
}
0 commit comments