16
16
package com .vaadin .flow .component ;
17
17
18
18
import static org .junit .Assert .assertEquals ;
19
- import static org .junit .Assert .assertFalse ;
20
- import static org .junit .Assert .assertNotNull ;
21
- import static org .junit .Assert .assertThrows ;
22
- import static org .junit .Assert .assertTrue ;
23
- import static org .junit .Assert .fail ;
24
19
import static org .mockito .ArgumentMatchers .any ;
25
20
import static org .mockito .Mockito .mock ;
26
21
import static org .mockito .Mockito .mockStatic ;
27
22
import static org .mockito .Mockito .when ;
28
23
29
- import java .util .ArrayList ;
30
- import java .util .List ;
31
24
import java .util .Locale ;
32
- import java .util .concurrent .Executor ;
33
- import java .util .concurrent .Executors ;
34
- import java .util .concurrent .atomic .AtomicBoolean ;
35
25
import java .util .concurrent .atomic .AtomicInteger ;
36
- import java .util .concurrent .atomic .AtomicReference ;
37
26
38
- import com .fasterxml .jackson .databind .ObjectMapper ;
39
- import org .junit .AfterClass ;
40
- import org .junit .Assert ;
41
- import org .junit .Before ;
42
- import org .junit .BeforeClass ;
43
27
import org .junit .Test ;
44
28
45
- import com .vaadin .experimental .DisabledFeatureException ;
46
29
import com .vaadin .experimental .FeatureFlags ;
47
30
import com .vaadin .flow .dom .Element ;
48
- import com .vaadin .flow .function . SerializableBiConsumer ;
49
- import com .vaadin .flow .server .MockVaadinServletService ;
31
+ import com .vaadin .flow .internal . CurrentInstance ;
32
+ import com .vaadin .flow .server .VaadinService ;
50
33
import com .vaadin .flow .shared .Registration ;
51
34
import com .vaadin .signals .NumberSignal ;
52
- import com .vaadin .signals .Signal ;
53
- import com .vaadin .signals .SignalEnvironment ;
54
35
import com .vaadin .signals .ValueSignal ;
55
36
import com .vaadin .tests .util .MockUI ;
56
37
57
38
public class ComponentEffectTest {
58
- private static final Executor executor = Runnable ::run ;
59
- private static final ObjectMapper objectMapper = new ObjectMapper ();
60
-
61
39
@ Test
62
40
public void effect_componentAttachedAndDetached_effectEnabledAndDisabled () {
63
- runWithSignalEnvironmentMocks (() -> {
41
+ runWithFeatureFlagEnabled (() -> {
64
42
TestComponent component = new TestComponent ();
65
43
ValueSignal <String > signal = new ValueSignal <>("initial" );
66
44
AtomicInteger count = new AtomicInteger ();
@@ -106,7 +84,7 @@ public void effect_componentAttachedAndDetached_effectEnabledAndDisabled() {
106
84
107
85
@ Test
108
86
public void bind_signalValueChanges_componentUpdated () {
109
- runWithSignalEnvironmentMocks (() -> {
87
+ runWithFeatureFlagEnabled (() -> {
110
88
TestComponent component = new TestComponent ();
111
89
ValueSignal <String > signal = new ValueSignal <>("initial" );
112
90
@@ -144,7 +122,7 @@ public void bind_signalValueChanges_componentUpdated() {
144
122
145
123
@ Test
146
124
public void format_customLocale_signalValuesChange_formattedStringUpdated () {
147
- runWithSignalEnvironmentMocks (() -> {
125
+ runWithFeatureFlagEnabled (() -> {
148
126
TestComponent component = new TestComponent ();
149
127
150
128
MockUI ui = new MockUI ();
@@ -187,7 +165,7 @@ public void format_customLocale_signalValuesChange_formattedStringUpdated() {
187
165
188
166
@ Test
189
167
public void format_defaultLocale_signalValuesChange_formattedStringUpdated () {
190
- runWithSignalEnvironmentMocks (() -> {
168
+ runWithFeatureFlagEnabled (() -> {
191
169
TestComponent component = new TestComponent ();
192
170
193
171
MockUI ui = new MockUI ();
@@ -204,31 +182,17 @@ public void format_defaultLocale_signalValuesChange_formattedStringUpdated() {
204
182
});
205
183
}
206
184
207
- /**
208
- * Other tests may already have initialized the environment with the feature
209
- * flag off and executors that would throw an exception, so it's too late
210
- * now to mock the feature flags. Thus we need to "reinitialize" the
211
- * environment.
212
- */
213
- private static void runWithSignalEnvironmentMocks (Runnable test ) {
214
- try (var environment = mockStatic (SignalEnvironment .class );
215
- var featureFlagStaticMock = mockStatic (FeatureFlags .class )) {
185
+ private static void runWithFeatureFlagEnabled (Runnable test ) {
186
+ try (var featureFlagStaticMock = mockStatic (FeatureFlags .class )) {
216
187
FeatureFlags flags = mock (FeatureFlags .class );
217
188
when (flags .isEnabled (FeatureFlags .FLOW_FULLSTACK_SIGNALS .getId ()))
218
189
.thenReturn (true );
219
190
featureFlagStaticMock .when (() -> FeatureFlags .get (any ()))
220
191
.thenReturn (flags );
221
- environment .when (() -> SignalEnvironment .initialized ())
222
- .thenReturn (true );
223
- environment .when (() -> SignalEnvironment .defaultDispatcher ())
224
- .thenReturn (executor );
225
- environment .when (() -> SignalEnvironment .synchronousDispatcher ())
226
- .thenReturn (executor );
227
- environment .when (() -> SignalEnvironment .asynchronousDispatcher ())
228
- .thenReturn (executor );
229
- environment .when (() -> SignalEnvironment .objectMapper ())
230
- .thenReturn (objectMapper );
231
192
test .run ();
193
+ } finally {
194
+ VaadinService .getCurrent ().destroy ();
195
+ CurrentInstance .clearAll ();
232
196
}
233
197
}
234
198
0 commit comments