|
| 1 | +import { metadataCacheUpdateCycleThreshold, MetadataManager } from '../../src/metadata/MetadataManager'; |
| 2 | +import { MetadataSubscription } from '../../src/metadata/MetadataSubscription'; |
| 3 | +import { ComputedMetadataSubscription, ComputedSubscriptionDependency, ComputeFunction } from '../../src/metadata/ComputedMetadataSubscription'; |
| 4 | +import { getUUID } from '../../src/utils/Utils'; |
| 5 | +import { Signal } from '../../src/utils/Signal'; |
| 6 | +import { FullBindTarget } from '../../src/parsers/inputFieldParser/InputFieldDeclaration'; |
| 7 | +import { TestMetadataAdapter } from './mocks/TestMetadataAdapter'; |
| 8 | + |
| 9 | +type SubscribeArgs = Parameters<typeof MetadataManager.prototype.subscribe>; |
| 10 | +type ComputedSubscribeArgs = Parameters<typeof MetadataManager.prototype.subscribeComputed>; |
| 11 | + |
| 12 | +const testFilePath = 'testFile'; |
| 13 | + |
| 14 | +describe('metadata manager', () => { |
| 15 | + let manager: MetadataManager; |
| 16 | + |
| 17 | + let subscriptionSignals: Signal<unknown>[]; |
| 18 | + let subscriptionSignalSpys: jest.SpyInstance[]; |
| 19 | + let subscriptionBindTargets: FullBindTarget[]; |
| 20 | + let subscriptionArgs: SubscribeArgs[]; |
| 21 | + let subscriptions: MetadataSubscription[]; |
| 22 | + |
| 23 | + let computedSubscriptionSignals: Signal<unknown>[]; |
| 24 | + let computedSubscriptionSignalSpys: jest.SpyInstance[]; |
| 25 | + let computedSubscriptionBindTargets: (FullBindTarget | undefined)[]; |
| 26 | + let computedSubscriptionDependencies: ComputedSubscriptionDependency[][]; |
| 27 | + let computedSubscriptionComputeFunctions: ComputeFunction[]; |
| 28 | + let computedSubscriptionArgs: ComputedSubscribeArgs[]; |
| 29 | + let computedSubscriptions: ComputedMetadataSubscription[]; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + // --- Subscriptions Setup --- |
| 33 | + |
| 34 | + subscriptionSignals = [new Signal<unknown>(undefined), new Signal<unknown>(undefined), new Signal<unknown>(undefined), new Signal<unknown>(undefined)]; |
| 35 | + |
| 36 | + subscriptionBindTargets = [ |
| 37 | + { |
| 38 | + filePath: testFilePath, |
| 39 | + metadataPath: ['numberField'], |
| 40 | + listenToChildren: false, |
| 41 | + boundToLocalScope: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + filePath: testFilePath, |
| 45 | + metadataPath: ['stringField'], |
| 46 | + listenToChildren: false, |
| 47 | + boundToLocalScope: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + filePath: testFilePath, |
| 51 | + metadataPath: ['booleanField'], |
| 52 | + listenToChildren: false, |
| 53 | + boundToLocalScope: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + filePath: testFilePath, |
| 57 | + metadataPath: ['arrayField'], |
| 58 | + listenToChildren: false, |
| 59 | + boundToLocalScope: false, |
| 60 | + }, |
| 61 | + ]; |
| 62 | + |
| 63 | + expect(subscriptionSignals.length).toBe(subscriptionBindTargets.length); |
| 64 | + |
| 65 | + subscriptionSignalSpys = []; |
| 66 | + subscriptionArgs = []; |
| 67 | + for (let i = 0; i < subscriptionSignals.length; i++) { |
| 68 | + subscriptionSignalSpys[i] = jest.spyOn(subscriptionSignals[i], 'set'); |
| 69 | + subscriptionArgs[i] = [getUUID(), subscriptionSignals[i], subscriptionBindTargets[i]]; |
| 70 | + } |
| 71 | + |
| 72 | + subscriptions = []; |
| 73 | + |
| 74 | + // --- Computed Subscriptions Setup --- |
| 75 | + |
| 76 | + computedSubscriptionSignals = [new Signal<unknown>(undefined), new Signal<unknown>(undefined)]; |
| 77 | + |
| 78 | + computedSubscriptionBindTargets = [ |
| 79 | + { |
| 80 | + filePath: testFilePath, |
| 81 | + metadataPath: ['computedField'], |
| 82 | + listenToChildren: false, |
| 83 | + boundToLocalScope: false, |
| 84 | + }, |
| 85 | + undefined, |
| 86 | + ]; |
| 87 | + |
| 88 | + computedSubscriptionDependencies = [ |
| 89 | + [ |
| 90 | + { |
| 91 | + bindTarget: subscriptionBindTargets[0], |
| 92 | + callbackSignal: new Signal<unknown>(undefined), |
| 93 | + }, |
| 94 | + { |
| 95 | + bindTarget: subscriptionBindTargets[2], |
| 96 | + callbackSignal: new Signal<unknown>(undefined), |
| 97 | + }, |
| 98 | + ], |
| 99 | + [ |
| 100 | + { |
| 101 | + bindTarget: subscriptionBindTargets[1], |
| 102 | + callbackSignal: new Signal<unknown>(undefined), |
| 103 | + }, |
| 104 | + { |
| 105 | + bindTarget: subscriptionBindTargets[2], |
| 106 | + callbackSignal: new Signal<unknown>(undefined), |
| 107 | + }, |
| 108 | + ], |
| 109 | + ]; |
| 110 | + |
| 111 | + computedSubscriptionComputeFunctions = [ |
| 112 | + (value: unknown[]): unknown => { |
| 113 | + return (value[0] as number) * (value[1] as number); |
| 114 | + }, |
| 115 | + (value: unknown[]): unknown => { |
| 116 | + return (value[0] as number) + (value[1] as number); |
| 117 | + }, |
| 118 | + ]; |
| 119 | + |
| 120 | + computedSubscriptionArgs = []; |
| 121 | + computedSubscriptionSignalSpys = []; |
| 122 | + for (let i = 0; i < computedSubscriptionSignals.length; i++) { |
| 123 | + computedSubscriptionSignalSpys[i] = jest.spyOn(computedSubscriptionSignals[i], 'set'); |
| 124 | + computedSubscriptionArgs[i] = [ |
| 125 | + getUUID(), |
| 126 | + computedSubscriptionSignals[i], |
| 127 | + computedSubscriptionBindTargets[i], |
| 128 | + computedSubscriptionDependencies[i], |
| 129 | + computedSubscriptionComputeFunctions[i], |
| 130 | + ]; |
| 131 | + } |
| 132 | + |
| 133 | + computedSubscriptions = []; |
| 134 | + |
| 135 | + // --- Manager Setup --- |
| 136 | + |
| 137 | + const adapter = new TestMetadataAdapter(new Map()); |
| 138 | + manager = new MetadataManager(adapter); |
| 139 | + }); |
| 140 | + |
| 141 | + test('subscribing should change the signal value to the current cache value', () => { |
| 142 | + subscriptions[0] = manager.subscribe(...subscriptionArgs[0]); |
| 143 | + |
| 144 | + expect(subscriptionSignals[0].get()).toBe(undefined); |
| 145 | + expect(subscriptionSignalSpys[0]).toHaveBeenCalledTimes(1); |
| 146 | + }); |
| 147 | + |
| 148 | + test('unsubscribing should not change the signal value', () => { |
| 149 | + subscriptions[0] = manager.subscribe(...subscriptionArgs[0]); |
| 150 | + subscriptions[0].unsubscribe(); |
| 151 | + |
| 152 | + expect(subscriptionSignals[0].get()).toBe(undefined); |
| 153 | + expect(subscriptionSignalSpys[0]).toHaveBeenCalledTimes(1); |
| 154 | + }); |
| 155 | + |
| 156 | + test('should update on external update', () => { |
| 157 | + subscriptions[0] = manager.subscribe(...subscriptionArgs[0]); |
| 158 | + |
| 159 | + expect(subscriptionSignals[0].get()).toBe(undefined); |
| 160 | + expect(subscriptionSignalSpys[0]).toHaveBeenCalledTimes(1); |
| 161 | + |
| 162 | + manager.updateCacheOnExternalUpdate(testFilePath, { numberField: 5 }); |
| 163 | + |
| 164 | + expect(subscriptionSignals[0].get()).toBe(5); |
| 165 | + expect(subscriptionSignalSpys[0]).toHaveBeenCalledTimes(2); |
| 166 | + }); |
| 167 | + |
| 168 | + test('should not update on external update after unsubscribing', () => { |
| 169 | + subscriptions[0] = manager.subscribe(...subscriptionArgs[0]); |
| 170 | + subscriptions[0].unsubscribe(); |
| 171 | + |
| 172 | + manager.updateCacheOnExternalUpdate(testFilePath, { numberField: 5 }); |
| 173 | + |
| 174 | + expect(subscriptionSignals[0].get()).toBe(undefined); |
| 175 | + expect(subscriptionSignalSpys[0]).toHaveBeenCalledTimes(1); |
| 176 | + }); |
| 177 | +}); |
0 commit comments