@blac/core 2.0.0-rc-11 → 2.0.0-rc-14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +739 -260
- package/dist/index.es.js +1924 -1237
- package/dist/index.es.js.map +1 -1
- package/package.json +6 -7
- package/src/Blac.ts +82 -69
- package/src/Bloc.ts +45 -3
- package/src/BlocBase.ts +263 -454
- package/src/Cubit.ts +1 -16
- package/src/__tests__/Blac.config.test.ts +10 -17
- package/src/__tests__/Bloc.event.test.ts +27 -52
- package/src/__tests__/BlocBase.subscription.test.ts +349 -0
- package/src/__tests__/Cubit.test.ts +39 -50
- package/src/__tests__/memory-leaks.test.ts +383 -0
- package/src/__tests__/plugins.test.ts +464 -0
- package/src/adapter/BlacAdapter.ts +347 -207
- package/src/adapter/ProxyFactory.ts +196 -255
- package/src/adapter/__tests__/BlacAdapter.test.ts +71 -77
- package/src/adapter/__tests__/ProxyFactory.test.ts +1 -1
- package/src/adapter/index.ts +0 -1
- package/src/errors/BlacError.ts +69 -0
- package/src/errors/ErrorManager.ts +101 -0
- package/src/errors/handleError.ts +114 -0
- package/src/errors/index.ts +13 -0
- package/src/events.ts +7 -1
- package/src/index.ts +10 -1
- package/src/lifecycle/BlocLifecycle.ts +156 -0
- package/src/lifecycle/index.ts +1 -0
- package/src/plugins/BlocPluginRegistry.ts +240 -0
- package/src/plugins/SystemPluginRegistry.ts +242 -0
- package/src/plugins/__tests__/BlocPluginRegistry.test.ts +450 -0
- package/src/plugins/__tests__/SystemPluginRegistry.test.ts +404 -0
- package/src/plugins/index.ts +8 -0
- package/src/plugins/types.ts +129 -0
- package/src/subscription/README.md +132 -0
- package/src/subscription/SubscriptionManager.ts +395 -0
- package/src/subscription/__tests__/SubscriptionManager.test.ts +355 -0
- package/src/subscription/index.ts +2 -0
- package/src/subscription/types.ts +90 -0
- package/src/testing.ts +31 -37
- package/src/types.ts +4 -21
- package/src/utils/BatchingManager.ts +59 -0
- package/src/utils/RerenderLogger.ts +265 -0
- package/src/utils/__tests__/generateInstanceId.test.ts +104 -0
- package/src/utils/__tests__/shallowEqual.test.ts +171 -0
- package/src/utils/__tests__/uuid.test.ts +144 -0
- package/src/utils/generateInstanceId.ts +54 -0
- package/src/utils/shallowEqual.ts +1 -1
- package/README.md +0 -329
- package/src/BlacObserver.ts +0 -186
- package/src/__tests__/BlocBase.lifecycle.test.ts +0 -441
- package/src/__tests__/props.test.ts +0 -256
- package/src/adapter/ConsumerTracker.ts +0 -241
- package/src/adapter/__tests__/ConsumerTracker.test.ts +0 -562
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { Blac } from '../Blac';
|
|
3
|
+
import { Cubit } from '../Cubit';
|
|
4
|
+
import { Bloc } from '../Bloc';
|
|
5
|
+
import { BlacPlugin, BlocPlugin, PluginCapabilities } from '../plugins';
|
|
6
|
+
import { BlocBase } from '../BlocBase';
|
|
7
|
+
|
|
8
|
+
// Test Cubit
|
|
9
|
+
class CounterCubit extends Cubit<number> {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
increment = () => this.emit(this.state + 1);
|
|
15
|
+
decrement = () => this.emit(this.state - 1);
|
|
16
|
+
setValue = (value: number) => this.emit(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Test Bloc
|
|
20
|
+
abstract class CounterEvent {}
|
|
21
|
+
class Increment extends CounterEvent {}
|
|
22
|
+
class Decrement extends CounterEvent {}
|
|
23
|
+
class SetValue extends CounterEvent {
|
|
24
|
+
constructor(public value: number) {
|
|
25
|
+
super();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class CounterBloc extends Bloc<number, CounterEvent> {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(0);
|
|
32
|
+
|
|
33
|
+
this.on(Increment, (event, emit) => emit(this.state + 1));
|
|
34
|
+
this.on(Decrement, (event, emit) => emit(this.state - 1));
|
|
35
|
+
this.on(SetValue, (event, emit) => emit(event.value));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Test implementations of plugins
|
|
40
|
+
class TestLoggingPlugin implements BlacPlugin {
|
|
41
|
+
readonly name = 'logging';
|
|
42
|
+
readonly version = '1.0.0';
|
|
43
|
+
private logLevel: 'debug' | 'info' | 'warn' | 'error';
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
options: { logLevel?: 'debug' | 'info' | 'warn' | 'error' } = {},
|
|
47
|
+
) {
|
|
48
|
+
this.logLevel = options.logLevel || 'info';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onBlocCreated(bloc: BlocBase<any>): void {
|
|
52
|
+
this.log('debug', `Bloc created: ${bloc._name}:${bloc._id}`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
onStateChanged(
|
|
56
|
+
bloc: BlocBase<any>,
|
|
57
|
+
previousState: any,
|
|
58
|
+
currentState: any,
|
|
59
|
+
): void {
|
|
60
|
+
this.log('debug', `State changed in ${bloc._name}:${bloc._id}`, {
|
|
61
|
+
previousState,
|
|
62
|
+
currentState,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private log(
|
|
67
|
+
level: 'debug' | 'info' | 'warn' | 'error',
|
|
68
|
+
message: string,
|
|
69
|
+
data?: any,
|
|
70
|
+
): void {
|
|
71
|
+
const levels = ['debug', 'info', 'warn', 'error'];
|
|
72
|
+
const currentLevelIndex = levels.indexOf(this.logLevel);
|
|
73
|
+
const messageLevelIndex = levels.indexOf(level);
|
|
74
|
+
|
|
75
|
+
if (messageLevelIndex >= currentLevelIndex) {
|
|
76
|
+
const timestamp = new Date().toISOString();
|
|
77
|
+
const logMessage = `[BlaC] [${timestamp}] ${message}`;
|
|
78
|
+
|
|
79
|
+
switch (level) {
|
|
80
|
+
case 'debug':
|
|
81
|
+
console.debug(logMessage, data);
|
|
82
|
+
break;
|
|
83
|
+
case 'info':
|
|
84
|
+
console.log(logMessage, data);
|
|
85
|
+
break;
|
|
86
|
+
case 'warn':
|
|
87
|
+
console.warn(logMessage, data);
|
|
88
|
+
break;
|
|
89
|
+
case 'error':
|
|
90
|
+
console.error(logMessage, data);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface StorageAdapter {
|
|
98
|
+
getItem(key: string): string | null;
|
|
99
|
+
setItem(key: string, value: string): void;
|
|
100
|
+
removeItem(key: string): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class TestPersistencePlugin<TState> implements BlocPlugin<TState> {
|
|
104
|
+
readonly name = 'persistence';
|
|
105
|
+
readonly version = '1.0.0';
|
|
106
|
+
readonly capabilities: PluginCapabilities = {
|
|
107
|
+
readState: true,
|
|
108
|
+
transformState: true,
|
|
109
|
+
interceptEvents: false,
|
|
110
|
+
persistData: true,
|
|
111
|
+
accessMetadata: false,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
private storage: StorageAdapter;
|
|
115
|
+
private key: string;
|
|
116
|
+
private saveDebounceMs: number;
|
|
117
|
+
private saveTimer?: any;
|
|
118
|
+
|
|
119
|
+
constructor(options: {
|
|
120
|
+
key: string;
|
|
121
|
+
storage?: StorageAdapter;
|
|
122
|
+
saveDebounceMs?: number;
|
|
123
|
+
}) {
|
|
124
|
+
this.key = options.key;
|
|
125
|
+
this.storage = options.storage || new MockStorage();
|
|
126
|
+
this.saveDebounceMs = options.saveDebounceMs ?? 100;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
onAttach(bloc: BlocBase<TState>): void {
|
|
130
|
+
const savedData = this.storage.getItem(this.key);
|
|
131
|
+
if (savedData) {
|
|
132
|
+
const restoredState = JSON.parse(savedData);
|
|
133
|
+
(bloc as any)._state = restoredState;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
onStateChange(previousState: TState, currentState: TState): void {
|
|
138
|
+
if (this.saveTimer) {
|
|
139
|
+
clearTimeout(this.saveTimer);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (this.saveDebounceMs > 0) {
|
|
143
|
+
this.saveTimer = setTimeout(() => {
|
|
144
|
+
this.storage.setItem(this.key, JSON.stringify(currentState));
|
|
145
|
+
}, this.saveDebounceMs);
|
|
146
|
+
} else {
|
|
147
|
+
this.storage.setItem(this.key, JSON.stringify(currentState));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
class TestValidationPlugin<T> implements BlocPlugin<T> {
|
|
153
|
+
readonly name = 'validation';
|
|
154
|
+
readonly version = '1.0.0';
|
|
155
|
+
readonly capabilities: PluginCapabilities = {
|
|
156
|
+
readState: true,
|
|
157
|
+
transformState: true,
|
|
158
|
+
interceptEvents: false,
|
|
159
|
+
persistData: false,
|
|
160
|
+
accessMetadata: false,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
constructor(private validator: (state: T) => boolean | string) {}
|
|
164
|
+
|
|
165
|
+
transformState(previousState: T, nextState: T): T {
|
|
166
|
+
const result = this.validator(nextState);
|
|
167
|
+
|
|
168
|
+
if (result === true) {
|
|
169
|
+
return nextState;
|
|
170
|
+
} else if (result === false) {
|
|
171
|
+
console.warn('State change rejected by validation plugin');
|
|
172
|
+
return previousState;
|
|
173
|
+
} else {
|
|
174
|
+
console.error(`State validation failed: ${result}`);
|
|
175
|
+
return previousState;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Mock storage
|
|
181
|
+
class MockStorage implements StorageAdapter {
|
|
182
|
+
private store = new Map<string, string>();
|
|
183
|
+
|
|
184
|
+
getItem(key: string): string | null {
|
|
185
|
+
return this.store.get(key) || null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
setItem(key: string, value: string): void {
|
|
189
|
+
this.store.set(key, value);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
removeItem(key: string): void {
|
|
193
|
+
this.store.delete(key);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
describe('New Plugin System', () => {
|
|
198
|
+
beforeEach(() => {
|
|
199
|
+
Blac.resetInstance();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('System Plugins (BlacPlugin)', () => {
|
|
203
|
+
it('should register and execute system plugins', () => {
|
|
204
|
+
const onBlocCreated = vi.fn();
|
|
205
|
+
const onStateChanged = vi.fn();
|
|
206
|
+
|
|
207
|
+
const plugin: BlacPlugin = {
|
|
208
|
+
name: 'test-system-plugin',
|
|
209
|
+
version: '1.0.0',
|
|
210
|
+
onBlocCreated,
|
|
211
|
+
onStateChanged,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
Blac.instance.plugins.add(plugin);
|
|
215
|
+
|
|
216
|
+
const cubit = Blac.getBloc(CounterCubit);
|
|
217
|
+
expect(onBlocCreated).toHaveBeenCalledWith(cubit);
|
|
218
|
+
|
|
219
|
+
cubit.increment();
|
|
220
|
+
expect(onStateChanged).toHaveBeenCalledWith(cubit, 0, 1);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should handle plugin errors gracefully', () => {
|
|
224
|
+
const errorPlugin: BlacPlugin = {
|
|
225
|
+
name: 'error-plugin',
|
|
226
|
+
version: '1.0.0',
|
|
227
|
+
onBlocCreated: () => {
|
|
228
|
+
throw new Error('Plugin error');
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
Blac.instance.plugins.add(errorPlugin);
|
|
233
|
+
|
|
234
|
+
// Should not throw when creating bloc
|
|
235
|
+
expect(() => Blac.getBloc(CounterCubit)).not.toThrow();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('should execute bootstrap hooks', () => {
|
|
239
|
+
const beforeBootstrap = vi.fn();
|
|
240
|
+
const afterBootstrap = vi.fn();
|
|
241
|
+
|
|
242
|
+
const plugin: BlacPlugin = {
|
|
243
|
+
name: 'bootstrap-plugin',
|
|
244
|
+
version: '1.0.0',
|
|
245
|
+
beforeBootstrap,
|
|
246
|
+
afterBootstrap,
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
Blac.resetInstance();
|
|
250
|
+
Blac.instance.plugins.add(plugin);
|
|
251
|
+
Blac.instance.bootstrap();
|
|
252
|
+
|
|
253
|
+
expect(beforeBootstrap).toHaveBeenCalled();
|
|
254
|
+
expect(afterBootstrap).toHaveBeenCalled();
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should track metrics', () => {
|
|
258
|
+
const plugin: BlacPlugin = {
|
|
259
|
+
name: 'metrics-plugin',
|
|
260
|
+
version: '1.0.0',
|
|
261
|
+
onBlocCreated: () => {
|
|
262
|
+
// Do something
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
Blac.instance.plugins.add(plugin);
|
|
267
|
+
Blac.getBloc(CounterCubit);
|
|
268
|
+
|
|
269
|
+
const metrics = Blac.instance.plugins.getMetrics('metrics-plugin');
|
|
270
|
+
expect(metrics).toBeDefined();
|
|
271
|
+
expect(metrics?.get('onBlocCreated')).toBeDefined();
|
|
272
|
+
expect(metrics?.get('onBlocCreated')?.executionCount).toBe(1);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe('Bloc Plugins (BlocPlugin)', () => {
|
|
277
|
+
it('should attach plugins to specific blocs', () => {
|
|
278
|
+
const onAttach = vi.fn();
|
|
279
|
+
const onStateChange = vi.fn();
|
|
280
|
+
|
|
281
|
+
const plugin: BlocPlugin<number> = {
|
|
282
|
+
name: 'test-bloc-plugin',
|
|
283
|
+
version: '1.0.0',
|
|
284
|
+
onAttach,
|
|
285
|
+
onStateChange,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const cubit = new CounterCubit();
|
|
289
|
+
cubit.addPlugin(plugin);
|
|
290
|
+
|
|
291
|
+
// Activate the bloc
|
|
292
|
+
Blac.activateBloc(cubit as any);
|
|
293
|
+
|
|
294
|
+
expect(onAttach).toHaveBeenCalledWith(cubit);
|
|
295
|
+
|
|
296
|
+
cubit.increment();
|
|
297
|
+
expect(onStateChange).toHaveBeenCalledWith(0, 1);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('should transform state through plugins', () => {
|
|
301
|
+
const transformPlugin: BlocPlugin<number> = {
|
|
302
|
+
name: 'double-plugin',
|
|
303
|
+
version: '1.0.0',
|
|
304
|
+
transformState: (prev, next) => next * 2,
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const cubit = new CounterCubit();
|
|
308
|
+
cubit.addPlugin(transformPlugin);
|
|
309
|
+
Blac.activateBloc(cubit as any);
|
|
310
|
+
|
|
311
|
+
cubit.setValue(5);
|
|
312
|
+
expect(cubit.state).toBe(10); // 5 * 2
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should transform events through plugins', () => {
|
|
316
|
+
const transformPlugin: BlocPlugin<number, CounterEvent> = {
|
|
317
|
+
name: 'event-doubler',
|
|
318
|
+
version: '1.0.0',
|
|
319
|
+
transformEvent: (event) => {
|
|
320
|
+
if (event instanceof SetValue) {
|
|
321
|
+
return new SetValue(event.value * 2);
|
|
322
|
+
}
|
|
323
|
+
return event;
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const bloc = new CounterBloc();
|
|
328
|
+
bloc.addPlugin(transformPlugin);
|
|
329
|
+
Blac.activateBloc(bloc as any);
|
|
330
|
+
|
|
331
|
+
bloc.add(new SetValue(5));
|
|
332
|
+
|
|
333
|
+
// Wait for event processing
|
|
334
|
+
setTimeout(() => {
|
|
335
|
+
expect(bloc.state).toBe(10); // 5 * 2
|
|
336
|
+
}, 10);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('should respect plugin capabilities', () => {
|
|
340
|
+
const readOnlyPlugin: BlocPlugin<number> = {
|
|
341
|
+
name: 'read-only',
|
|
342
|
+
version: '1.0.0',
|
|
343
|
+
capabilities: {
|
|
344
|
+
readState: true,
|
|
345
|
+
transformState: false,
|
|
346
|
+
interceptEvents: false,
|
|
347
|
+
persistData: false,
|
|
348
|
+
accessMetadata: false,
|
|
349
|
+
},
|
|
350
|
+
transformState: () => {
|
|
351
|
+
throw new Error('Should not be called');
|
|
352
|
+
},
|
|
353
|
+
onStateChange: vi.fn(),
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const cubit = new CounterCubit();
|
|
357
|
+
cubit.addPlugin(readOnlyPlugin);
|
|
358
|
+
Blac.activateBloc(cubit as any);
|
|
359
|
+
|
|
360
|
+
cubit.increment();
|
|
361
|
+
expect(cubit.state).toBe(1); // Transform not applied
|
|
362
|
+
expect(readOnlyPlugin.onStateChange).toHaveBeenCalled();
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
describe('Example Plugins', () => {
|
|
367
|
+
it('should log with TestLoggingPlugin', () => {
|
|
368
|
+
const consoleSpy = vi.spyOn(console, 'debug');
|
|
369
|
+
|
|
370
|
+
const plugin = new TestLoggingPlugin({ logLevel: 'debug' });
|
|
371
|
+
Blac.instance.plugins.add(plugin);
|
|
372
|
+
|
|
373
|
+
const cubit = Blac.getBloc(CounterCubit);
|
|
374
|
+
cubit.increment();
|
|
375
|
+
|
|
376
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
377
|
+
expect.stringContaining('Bloc created'),
|
|
378
|
+
undefined,
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
consoleSpy.mockRestore();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('should persist state with TestPersistencePlugin', () => {
|
|
385
|
+
const storage = new MockStorage();
|
|
386
|
+
storage.setItem('counter', '42');
|
|
387
|
+
|
|
388
|
+
const plugin = new TestPersistencePlugin<number>({
|
|
389
|
+
key: 'counter',
|
|
390
|
+
storage,
|
|
391
|
+
saveDebounceMs: 0,
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
const cubit = new CounterCubit();
|
|
395
|
+
cubit.addPlugin(plugin);
|
|
396
|
+
Blac.activateBloc(cubit as any);
|
|
397
|
+
|
|
398
|
+
// Should restore from storage
|
|
399
|
+
expect(cubit.state).toBe(42);
|
|
400
|
+
|
|
401
|
+
// Should save new state
|
|
402
|
+
cubit.setValue(100);
|
|
403
|
+
|
|
404
|
+
// Wait for debounced save
|
|
405
|
+
setTimeout(() => {
|
|
406
|
+
expect(storage.getItem('counter')).toBe('100');
|
|
407
|
+
}, 10);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it('should validate state with TestValidationPlugin', () => {
|
|
411
|
+
const validator = (state: number) => {
|
|
412
|
+
if (state < 0) return 'Value must be non-negative';
|
|
413
|
+
if (state > 100) return 'Value must not exceed 100';
|
|
414
|
+
return true;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const plugin = new TestValidationPlugin(validator);
|
|
418
|
+
|
|
419
|
+
const cubit = new CounterCubit();
|
|
420
|
+
cubit.addPlugin(plugin);
|
|
421
|
+
Blac.activateBloc(cubit as any);
|
|
422
|
+
|
|
423
|
+
cubit.setValue(-5);
|
|
424
|
+
expect(cubit.state).toBe(0); // Rejected
|
|
425
|
+
|
|
426
|
+
cubit.setValue(50);
|
|
427
|
+
expect(cubit.state).toBe(50); // Accepted
|
|
428
|
+
|
|
429
|
+
cubit.setValue(150);
|
|
430
|
+
expect(cubit.state).toBe(50); // Rejected
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
describe('Plugin Composition', () => {
|
|
435
|
+
it('should compose multiple bloc plugins', () => {
|
|
436
|
+
const storage = new MockStorage();
|
|
437
|
+
|
|
438
|
+
const persistencePlugin = new TestPersistencePlugin<number>({
|
|
439
|
+
key: 'validated-counter',
|
|
440
|
+
storage,
|
|
441
|
+
saveDebounceMs: 0,
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
const validationPlugin = new TestValidationPlugin<number>(
|
|
445
|
+
(state) => state >= 0 && state <= 10,
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
const cubit = new CounterCubit();
|
|
449
|
+
cubit.addPlugin(validationPlugin);
|
|
450
|
+
cubit.addPlugin(persistencePlugin);
|
|
451
|
+
Blac.activateBloc(cubit as any);
|
|
452
|
+
|
|
453
|
+
cubit.setValue(5);
|
|
454
|
+
expect(cubit.state).toBe(5);
|
|
455
|
+
|
|
456
|
+
cubit.setValue(15);
|
|
457
|
+
expect(cubit.state).toBe(5); // Validation rejected
|
|
458
|
+
|
|
459
|
+
setTimeout(() => {
|
|
460
|
+
expect(storage.getItem('validated-counter')).toBe('5');
|
|
461
|
+
}, 10);
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
});
|