|
| 1 | +import Neo from '../../../../src/Neo.mjs'; |
| 2 | +import * as core from '../../../../src/core/_export.mjs'; |
| 3 | +import Effect from '../../../../src/core/Effect.mjs'; |
| 4 | +import EffectManager from '../../../../src/core/EffectManager.mjs'; |
| 5 | +import Config from '../../../../src/core/Config.mjs'; |
| 6 | + |
| 7 | +StartTest(t => { |
| 8 | + t.it('EffectManager should manage active effects', t => { |
| 9 | + const effect1 = new Effect({fn: () => {}}); |
| 10 | + const effect2 = new Effect({fn: () => {}}); |
| 11 | + |
| 12 | + t.is(EffectManager.getActiveEffect(), null, 'No active effect initially'); |
| 13 | + |
| 14 | + EffectManager.push(effect1); |
| 15 | + t.is(EffectManager.getActiveEffect(), effect1, 'Effect1 is active'); |
| 16 | + |
| 17 | + EffectManager.push(effect2); |
| 18 | + t.is(EffectManager.getActiveEffect(), effect2, 'Effect2 is active'); |
| 19 | + |
| 20 | + EffectManager.pop(); |
| 21 | + t.is(EffectManager.getActiveEffect(), effect1, 'Effect1 is active after pop'); |
| 22 | + |
| 23 | + EffectManager.pop(); |
| 24 | + t.is(EffectManager.getActiveEffect(), null, 'No active effect after all pops'); |
| 25 | + |
| 26 | + effect1.destroy(); |
| 27 | + effect2.destroy(); |
| 28 | + }); |
| 29 | + |
| 30 | + t.it('Effect should run its function and track dependencies', t => { |
| 31 | + let runCount = 0; |
| 32 | + const configA = new Config(1); |
| 33 | + const configB = new Config(10); |
| 34 | + |
| 35 | + // Identity check |
| 36 | + t.is(configA, configA, 'configA is strictly equal to itself'); |
| 37 | + t.is(configB, configB, 'configB is strictly equal to itself'); |
| 38 | + t.isNot(configA, configB, 'configA is not strictly equal to configB'); |
| 39 | + |
| 40 | + const effect = new Effect({ |
| 41 | + fn: () => { |
| 42 | + runCount++; |
| 43 | + // Access configs to register them as dependencies |
| 44 | + const sum = configA.get() + configB.get(); |
| 45 | + t.pass(`Effect ran. Sum: ${sum}`); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + t.is(runCount, 1, 'Effect function ran once on creation'); |
| 50 | + t.is(effect.dependencies.size, 2, 'Effect tracked 2 dependencies'); |
| 51 | + |
| 52 | + // Change a dependency, effect should re-run |
| 53 | + configA.set(2); |
| 54 | + t.is(runCount, 2, 'Effect function ran again after configA change'); |
| 55 | + |
| 56 | + // Change another dependency, effect should re-run |
| 57 | + configB.set(20); |
| 58 | + t.is(runCount, 3, 'Effect function ran again after configB change'); |
| 59 | + |
| 60 | + // Change a dependency to the same value, effect should not re-run (Config handles this) |
| 61 | + configA.set(2); |
| 62 | + t.is(runCount, 3, 'Effect function did not run after no-change configA update'); |
| 63 | + |
| 64 | + effect.destroy(); |
| 65 | + t.is(effect.isDestroyed, true, 'Effect is destroyed'); |
| 66 | + t.is(effect.dependencies.size, 0, 'Effect dependencies cleared after destroy'); |
| 67 | + |
| 68 | + // Changing config after effect is destroyed should not re-run effect |
| 69 | + configA.set(3); |
| 70 | + t.is(runCount, 3, 'Effect function did not run after configA change when destroyed'); |
| 71 | + }); |
| 72 | + |
| 73 | + t.it('Effect should clean up old dependencies on re-run', t => { |
| 74 | + let runCount = 0; |
| 75 | + const configX = new Config('X'); |
| 76 | + const configY = new Config('Y'); |
| 77 | + |
| 78 | + // Initial effect: depends on configX |
| 79 | + const effect = new Effect({ |
| 80 | + fn: () => { |
| 81 | + runCount++; |
| 82 | + t.is(configX.get(), 'X', 'Effect ran (1st): configX value'); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + t.is(runCount, 1, 'Effect ran once initially'); |
| 87 | + t.is(effect.dependencies.size, 1, 'Effect has 1 dependency (configX)'); |
| 88 | + |
| 89 | + // --- Transition to configY dependency --- |
| 90 | + // Reassign the effect's function to depend on configY |
| 91 | + effect.fn = () => { |
| 92 | + runCount++; |
| 93 | + |
| 94 | + if (runCount === 2) { |
| 95 | + t.is(configY.get(), 'Y', 'Effect ran (2nd): configY value'); |
| 96 | + } |
| 97 | + |
| 98 | + if (runCount === 3) { |
| 99 | + t.is(configY.get(), 'Y_new', 'Effect ran (3rd): configY value'); |
| 100 | + } |
| 101 | + |
| 102 | + else if (runCount === 4) { |
| 103 | + t.is(configY.get(), 'Y_final', 'Effect ran (4th): configY value'); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + t.is(runCount, 2, 'Effect ran a second time after fn reassignment'); |
| 108 | + |
| 109 | + // Changing the config value will trigger a re-run. |
| 110 | + configY.set('Y_new'); |
| 111 | + |
| 112 | + t.is(runCount, 3, 'Effect ran a second time after fn reassignment'); |
| 113 | + t.is(effect.dependencies.size, 1, 'Effect now has 1 dependency (configY)'); |
| 114 | + |
| 115 | + // Change configX: should NOT trigger the effect (old dependency cleaned up) |
| 116 | + configX.set('X_new'); |
| 117 | + t.is(runCount, 3, 'Effect did not re-run after old dependency (configX) changed'); |
| 118 | + |
| 119 | + // Change configY: should trigger the effect (new dependency) |
| 120 | + configY.set('Y_final'); // This will trigger runCount to become 3 |
| 121 | + t.is(runCount, 4, 'Effect re-ran after new dependency (configY) changed'); |
| 122 | + |
| 123 | + effect.destroy(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments