diff --git a/test/unit/core/Executors.ts b/test/unit/core/Executors.ts deleted file mode 100644 index 02046f576..000000000 --- a/test/unit/core/Executors.ts +++ /dev/null @@ -1,79 +0,0 @@ -import test from "ava"; -import sinon, { SinonSandbox } from "sinon"; -import CoreModule from "../../../src/core/CoreModule"; -import { IdentityExecutor } from "../../../src/core/executors/IdentityExecutor"; -import { PropertiesExecutor } from "../../../src/core/executors/PropertiesExecutor"; -import { SubscriptionExecutor } from "../../../src/core/executors/SubscriptionExecutor"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { CoreChangeType } from "../../../src/core/models/CoreChangeType"; -import { ModelName } from "../../../src/core/models/SupportedModels"; -import { TestEnvironment } from "../../support/sdk/TestEnvironment"; -import { stubModelCache } from "./_helpers"; - -const sinonSandbox: SinonSandbox = sinon.sandbox.create(); - -test.beforeEach(async () => { - await TestEnvironment.stubDomEnvironment(); - stubModelCache(sinonSandbox); -}); - -test.afterEach(() => { - sinonSandbox.restore(); -}); - -test("Subscriptions executor flushes deltas at end of `processDeltaQueue`", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - - const processDeltaQueueSpy = sinonSandbox.spy(SubscriptionExecutor.prototype, "processDeltaQueue"); - const flushDeltasSpy = sinonSandbox.spy(SubscriptionExecutor.prototype as any, "_flushDeltas"); - - core.modelRepo?.broadcast({ - changeType: CoreChangeType.Add, - model: new OSModel(ModelName.EmailSubscriptions), - }); - - clock.tick(2001); - - t.is(processDeltaQueueSpy.callCount, 1); - t.is(flushDeltasSpy.callCount, 1); -}); - -test("Identity executor flushes deltas at end of `processDeltaQueue`", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - - const processDeltaQueueSpy = sinonSandbox.spy(IdentityExecutor.prototype, "processDeltaQueue"); - const flushDeltasSpy = sinonSandbox.spy(IdentityExecutor.prototype as any, "_flushDeltas"); - - core.modelRepo?.broadcast({ - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Identity), - }); - - clock.tick(2001); - - t.is(processDeltaQueueSpy.callCount, 1); - t.is(flushDeltasSpy.callCount, 1); -}); - -test("User Properties executor flushes deltas at end of `processDeltaQueue`", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - - const processDeltaQueueSpy = sinonSandbox.spy(PropertiesExecutor.prototype, "processDeltaQueue"); - const flushDeltasSpy = sinonSandbox.spy(PropertiesExecutor.prototype as any, "_flushDeltas"); - - core.modelRepo?.broadcast({ - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Properties), - }); - - clock.tick(2001); - - t.is(processDeltaQueueSpy.callCount, 1); - t.is(flushDeltasSpy.callCount, 1); -}); diff --git a/test/unit/core/ModelCache.ts b/test/unit/core/ModelCache.ts deleted file mode 100644 index b1d3b7241..000000000 --- a/test/unit/core/ModelCache.ts +++ /dev/null @@ -1,113 +0,0 @@ -import { TestEnvironment } from "../../support/sdk/TestEnvironment"; -import test from 'ava'; -import ModelCache from "../../../src/core/caching/ModelCache"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { SubscriptionType, SupportedSubscription } from "../../../src/core/models/SubscriptionModels"; -import { ModelName, SupportedModel } from "../../../src/core/models/SupportedModels"; -import Database from "../../../src/shared/services/Database"; -import sinon from "sinon"; - -const MODEL_ID = '0000000000'; -const MODEL_ID_2 = '1111111111'; - -test.beforeEach(async () => { - sinon.useFakeTimers(); - await TestEnvironment.initialize(); - TestEnvironment.mockInternalOneSignal(); -}); - -test("model cache: add model -> added to IndexedDB", async t => { - const modelCache = new ModelCache(); - const data = { myAlias: "myAliasId" }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const result = await Database.get(ModelName.Identity, MODEL_ID); - t.deepEqual(result, { modelId: MODEL_ID, modelName: ModelName.Identity, ...data }); -}); - -test("model cache: add multiple models -> added to IndexedDB", async t => { - const modelCache = new ModelCache(); - const data = { myAlias: "myAliasId" }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const model2 = new OSModel(ModelName.Identity, data, MODEL_ID_2); - await modelCache.add(ModelName.Identity, model2); - const result = await Database.getAll(ModelName.Identity); - t.deepEqual(result, [ - { modelId: MODEL_ID, modelName: ModelName.Identity, ...data }, - { modelId: MODEL_ID_2, modelName: ModelName.Identity, ...data } - ]); -}); - -test("model cache: update model -> updated in IndexedDB", async t => { - const modelCache = new ModelCache(); - const data = { myAlias: "myAliasId" }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const newData = { myAlias: "myNewAliasId" }; - await modelCache.update(ModelName.Identity, MODEL_ID, "myAlias", newData.myAlias); - const result = await Database.get(ModelName.Identity, MODEL_ID); - t.deepEqual(result, { modelId: MODEL_ID, modelName: ModelName.Identity, ...newData }); -}); - -test("model cache: remove model -> removed from IndexedDB", async t => { - const modelCache = new ModelCache(); - const data = { myAlias: "myAliasId" }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - await modelCache.remove(ModelName.Identity, MODEL_ID); - const result = await Database.get(ModelName.Identity, MODEL_ID); - t.is(result, null); -}); - -test("decode all models with model name: no models -> returns undefined", async t => { - const modelCache = new ModelCache(); - const result = await modelCache.getAndDecodeModelsWithModelName(ModelName.Identity); - t.deepEqual(result, undefined); -}); - -test("decode all models with model name: one model -> returns model", async t => { - const modelCache = new ModelCache(); - const data = { - onesignalId: "00000000-0000-0000-0000-000000000000", - myAlias: "myAliasId" - }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const result = await modelCache.getAndDecodeModelsWithModelName(ModelName.Identity); - t.deepEqual(result, [model]); -}); - -test("decode all models with model name: multiple models -> returns models", async t => { - const modelCache = new ModelCache(); - const data = { - onesignalId: "00000000-0000-0000-0000-000000000000", - myAlias: "myAliasId" - }; - const model = new OSModel(ModelName.Identity, data, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const model2 = new OSModel(ModelName.Identity, data, MODEL_ID_2); - await modelCache.add(ModelName.Identity, model2); - const result = await modelCache.getAndDecodeModelsWithModelName(ModelName.Identity); - t.deepEqual(result, [model, model2]); -}); - -test("decode all models with model name: multiple models with different model names -> returns models with correct model name", async t => { - const modelCache = new ModelCache(); - const identityData = { - onesignalId: "00000000-0000-0000-0000-000000000000", - myAlias: "myAliasId" - }; - const subscriptionData = { - type: SubscriptionType.Email - }; - const model = new OSModel(ModelName.Identity, identityData, MODEL_ID); - await modelCache.add(ModelName.Identity, model); - const model2 = new OSModel(ModelName.Identity, identityData, MODEL_ID_2); - await modelCache.add(ModelName.Identity, model2); - const model3 = new OSModel( - ModelName.EmailSubscriptions, subscriptionData, MODEL_ID_2); - await modelCache.add(ModelName.EmailSubscriptions, model3); - const result = await modelCache.getAndDecodeModelsWithModelName(ModelName.Identity); - t.deepEqual(result, [model, model2]); -}); diff --git a/test/unit/core/ModelRepo.ts b/test/unit/core/ModelRepo.ts deleted file mode 100644 index 2935d8e2e..000000000 --- a/test/unit/core/ModelRepo.ts +++ /dev/null @@ -1,142 +0,0 @@ -import test from "ava"; -import sinon, { SinonSandbox } from "sinon"; -import CoreModule from "../../../src/core/CoreModule"; -import { CoreModuleDirector } from "../../../src/core/CoreModuleDirector"; -import { ModelRepo } from "../../../src/core/modelRepo/ModelRepo"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { CoreChangeType } from "../../../src/core/models/CoreChangeType"; -import { CoreDelta } from "../../../src/core/models/CoreDeltas"; -import { ModelName, SupportedModel } from "../../../src/core/models/SupportedModels"; -import { isPropertyDelta } from "../../../src/core/utils/typePredicates"; -import { generateNewSubscription, passIfBroadcastNTimes, stubModelCache } from "./_helpers"; - -const sinonSandbox: SinonSandbox = sinon.sandbox.create(); - -let broadcastCount: number; -let core: CoreModule; -let coreDirector: CoreModuleDirector; - -test.beforeEach(async () => { - stubModelCache(sinonSandbox); - core = new CoreModule(); - coreDirector = new CoreModuleDirector(core); - await core.init(); - broadcastCount = 0; -}); - -test.afterEach(() => { - sinonSandbox.restore(); -}); - -/* M O D E L C H A N G E T Y P E S */ -test("Add subscription -> +1 subscription model in model store", async t => { - const processModelAddedSpy = sinonSandbox.spy(ModelRepo.prototype as any, "processModelAdded"); - const newSub = generateNewSubscription(); - const emailSubModels = await coreDirector.getEmailSubscriptionModels(); - - if (emailSubModels === undefined) { - t.fail("Email subscription models are undefined"); - return; - } - - t.is(Object.keys(emailSubModels).length, 0); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - t.is(Object.keys(emailSubModels).length, 1); - t.true(processModelAddedSpy.calledOnce); -}); - -test("Remove subscription -> -1 subscription model in model store", async t => { - const processModelRemovedSpy = sinonSandbox.spy(ModelRepo.prototype as any, "processModelRemoved"); - const newSub = generateNewSubscription(); - const emailSubModels = await coreDirector.getEmailSubscriptionModels(); - - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - t.is(Object.keys(emailSubModels).length, 1); - await coreDirector.remove(ModelName.EmailSubscriptions, newSub.modelId); - t.is(Object.keys(emailSubModels).length, 0); - t.true(processModelRemovedSpy.calledOnce); -}); - -test("Update model properties -> change is processed correctly", async t => { - const processModelUpdatedSpy = sinonSandbox.spy(ModelRepo.prototype as any, "processModelUpdated"); - const newSub = generateNewSubscription(); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - newSub.set("rooted", true); // trigger model update - t.true(processModelUpdatedSpy.calledOnce); - - const emailSubModels = await coreDirector.getEmailSubscriptionModels(); - const emailModel = emailSubModels[newSub.modelId]; - t.is(emailModel.data?.rooted, true); -}); - -/* D E L T A S */ -test("Add subscription -> delta is broadcasted once", async t => { - core.modelRepo?.subscribe(() => { - broadcastCount+=1; - passIfBroadcastNTimes(t, 1, broadcastCount); - }); - const newSub = generateNewSubscription(); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); -}); - -test("Remove subscription -> delta is broadcasted twice", async t => { - core.modelRepo?.subscribe(() => { - broadcastCount+=1; - passIfBroadcastNTimes(t, 2, broadcastCount); - }); - const newSub = generateNewSubscription(); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - await coreDirector.remove(ModelName.EmailSubscriptions, newSub.modelId); -}); - -test("Update model properties -> delta is broadcasted twice", async t => { - core.modelRepo?.subscribe(() => { - broadcastCount+=1; - passIfBroadcastNTimes(t, 2, broadcastCount); - }); - const newSub = generateNewSubscription(); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - newSub.set("rooted", true); // trigger model update -}); - -test("Add subscription -> delta is broadcasted with correct change type and payload", async t => { - const newSub = generateNewSubscription(); - core.modelRepo?.subscribe((delta: CoreDelta) => { - t.is(delta.changeType, CoreChangeType.Add); - t.deepEqual(delta.model, newSub as OSModel); - }); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); -}); - -test("Remove subscription -> delta is broadcasted with correct change type and payload", async t => { - const newSub = generateNewSubscription(); - core.modelRepo?.subscribe((delta: CoreDelta) => { - broadcastCount+=1; - if (broadcastCount === 1) { - t.is(delta.changeType, CoreChangeType.Add); - t.deepEqual(delta.model, newSub as OSModel); - t.is(delta.model.modelName, newSub.modelName); - t.is(delta.model.modelId, newSub.modelId); - } else if (broadcastCount === 2) { - t.is(delta.changeType, CoreChangeType.Remove); - } - }); - - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - await coreDirector.remove(ModelName.EmailSubscriptions, newSub.modelId); -}); - -test("Update model properties -> delta is broadcasted with correct change type and payload", async t => { - const newSub = generateNewSubscription(); - await coreDirector.add(ModelName.EmailSubscriptions, newSub as OSModel, false); - - core.modelRepo?.subscribe(delta => { - if (isPropertyDelta(delta)) { - t.is(delta.changeType, CoreChangeType.Update); - t.is(delta.property, "rooted"); - t.is(delta.newValue, true); - t.is(delta.oldValue, undefined); - } - }); - newSub.set("rooted", true); // trigger model update -}); diff --git a/test/unit/core/OSModel.ts b/test/unit/core/OSModel.ts deleted file mode 100644 index ed039f357..000000000 --- a/test/unit/core/OSModel.ts +++ /dev/null @@ -1,60 +0,0 @@ -import test from "ava"; -import { generateNewSubscription } from "./_helpers"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { SubscriptionModel, SubscriptionType } from "../../../src/core/models/SubscriptionModels"; -import { ModelName } from "../../../src/core/models/SupportedModels"; - -const MODEL_ID = "0000000000"; - -test("`Set` function updates data", async t => { - const newSub = generateNewSubscription(); - t.is(newSub.data?.rooted, undefined); - newSub.set("rooted", true); - t.is(newSub.data?.rooted, true); -}); - -test("`Set` function broadcasts update event", async t => { - const newSub = generateNewSubscription(); - newSub.subscribe(() => { - t.pass(); - }); - newSub.set("rooted", true); -}); - -test("`Hydrate` function updates data", async t => { - const newSub = generateNewSubscription(); - t.is(newSub.data?.type, SubscriptionType.Email); - newSub.hydrate({ type: SubscriptionType.ChromePush }); - t.is(newSub.data?.type, SubscriptionType.ChromePush); - t.deepEqual(newSub.data, { type: SubscriptionType.ChromePush }); -}); - -test("`Encode` function returns encoded model", async t => { - const newSub = generateNewSubscription(); - const encodedSub = newSub.encode(); - t.deepEqual(encodedSub, { - modelId: MODEL_ID, - modelName: ModelName.EmailSubscriptions, - type: SubscriptionType.Email, - id: "123", - token: "myToken", - }); -}); - -test("`Decode` function returns decoded model", async t => { - const encodedSub = { - modelId: MODEL_ID, - modelName: ModelName.EmailSubscriptions, - type: SubscriptionType.Email, - id: "123", - token: "myToken", - }; - const decodedSub = OSModel.decode(encodedSub) as OSModel; - - const model: SubscriptionModel = { - type: SubscriptionType.Email, - id: "123", - token: "myToken", - }; - t.deepEqual(decodedSub, new OSModel(ModelName.EmailSubscriptions, model, MODEL_ID)); -}); diff --git a/test/unit/core/OperationCache.ts b/test/unit/core/OperationCache.ts deleted file mode 100644 index e16a9c2a2..000000000 --- a/test/unit/core/OperationCache.ts +++ /dev/null @@ -1,62 +0,0 @@ -import test from "ava"; -import sinon, { SinonSandbox } from "sinon"; -import { CoreModuleDirector } from "../../../src/core/CoreModuleDirector"; -import OperationCache from "../../../src/core/caching/OperationCache"; -import { CoreChangeType } from "../../../src/core/models/CoreChangeType"; -import { ModelName } from "../../../src/core/models/SupportedModels"; -import { Operation } from "../../../src/core/operationRepo/Operation"; -import { TestEnvironment } from "../../support/sdk/TestEnvironment"; -import { getDummyIdentityOSModel, getMockDeltas } from "./_helpers"; - -const sinonSandbox: SinonSandbox = sinon.sandbox.create(); - -test.beforeEach(async () => { - sinon.useFakeTimers(); - await TestEnvironment.initialize(); - TestEnvironment.mockInternalOneSignal(); - sinonSandbox.stub(CoreModuleDirector.prototype, "getModelByTypeAndId").callsFake(() => { - return getDummyIdentityOSModel(); - }); -}); - -test("Add operation to cache -> operation queue +1", async t => { - const operation = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - OperationCache.enqueue(operation); - const operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 1); -}); - -test("Remove operation from cache -> operation queue -1", async t => { - const operation = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - OperationCache.enqueue(operation); - let operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 1); - OperationCache.delete(operation.operationId); - operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 0); -}); - -test("Add multiple operations to cache -> operation queue +2", async t => { - const operation = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - const operation2 = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - OperationCache.enqueue(operation); - let operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 1); - OperationCache.enqueue(operation2); - operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 2); -}); - -test("Flush operation cache -> operation queue 0", async t => { - const operation = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - const operation2 = new Operation(CoreChangeType.Add, ModelName.Identity, getMockDeltas()); - OperationCache.enqueue(operation); - let operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 1); - OperationCache.enqueue(operation2); - operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 2); - OperationCache.flushOperations(); - operations = await OperationCache.getOperationsWithModelName(ModelName.Identity); - t.is(operations.length, 0); -}); diff --git a/test/unit/core/OperationRepo.ts b/test/unit/core/OperationRepo.ts deleted file mode 100644 index 2e50c773b..000000000 --- a/test/unit/core/OperationRepo.ts +++ /dev/null @@ -1,177 +0,0 @@ -import test from "ava"; -import sinon, { SinonSandbox } from "sinon"; -import CoreModule from "../../../src/core/CoreModule"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { CoreChangeType } from "../../../src/core/models/CoreChangeType"; -import { CoreDelta } from "../../../src/core/models/CoreDeltas"; -import { IdentityModel } from "../../../src/core/models/IdentityModel"; -import { ModelName, SupportedModel } from "../../../src/core/models/SupportedModels"; -import { OperationRepo } from "../../../src/core/operationRepo/OperationRepo"; -import { TestEnvironment } from "../../support/sdk/TestEnvironment"; -import { passIfBroadcastNTimes, stubModelCache } from "./_helpers"; - -const sinonSandbox: SinonSandbox = sinon.sandbox.create(); -let broadcastCount: number; - -test.beforeEach(async () => { - await TestEnvironment.stubDomEnvironment(); - stubModelCache(sinonSandbox); - broadcastCount = 0; -}); - -test.afterEach(() => { - sinonSandbox.restore(); -}); - -test("OperationRepo executor store has executor for each model name", async t => { - sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - const { operationRepo } = core; - const executorStore = operationRepo?.executorStore; - Object.values(ModelName).forEach(modelName => { - const executor = executorStore ? executorStore[modelName] : null; - t.truthy(executor); - }); -}); - -test("Model repo delta broadcast is received and processed by operation repo", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - const { modelRepo, operationRepo } = core; - const executor = operationRepo?.executorStore[ModelName.EmailSubscriptions]; - - modelRepo?.subscribe(() => { - passIfBroadcastNTimes(t, 1, broadcastCount); - }); - - const processDeltaSpy = sinonSandbox.spy(OperationRepo.prototype as any, "processDelta"); - - modelRepo?.broadcast({ - changeType: CoreChangeType.Add, - model: new OSModel(ModelName.EmailSubscriptions), - }); - - clock.tick(1001); - - t.true(processDeltaSpy.calledOnce); - t.is(executor?.operationQueue.length, 1); - t.is(executor?.deltaQueue.length, 0); -}); - -test("Add Subscriptions: Multiple delta broadcasts -> two operations of change type: add", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - const { modelRepo, operationRepo } = core; - const executor = operationRepo?.executorStore[ModelName.EmailSubscriptions]; - - modelRepo?.subscribe(() => { - passIfBroadcastNTimes(t, 2, broadcastCount); - }); - - const processDeltaSpy = sinonSandbox.spy(OperationRepo.prototype as any, "processDelta"); - - modelRepo?.broadcast({ - changeType: CoreChangeType.Add, - model: new OSModel(ModelName.EmailSubscriptions), - }); - modelRepo?.broadcast({ - changeType: CoreChangeType.Add, - model: new OSModel(ModelName.EmailSubscriptions), - }); - - clock.tick(1001); - - t.true(processDeltaSpy.calledTwice); - t.is(executor?.operationQueue.length, 2); - t.is(executor?.deltaQueue.length, 0); -}); - -test("Update Identity: Multiple delta broadcasts -> one operation of change type: update", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - const { modelRepo, operationRepo } = core; - const executor = operationRepo?.executorStore[ModelName.Identity]; - - modelRepo?.subscribe(() => { - passIfBroadcastNTimes(t, 2, broadcastCount); - }); - - const processDeltaSpy = sinonSandbox.spy(OperationRepo.prototype as any, "processDelta"); - - const delta1: CoreDelta = { - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Identity), - property: "myAlias", - oldValue: "", - newValue: "myNewAlias", - }; - - const delta2: CoreDelta = { - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Identity), - property: "myAlias2", - oldValue: "", - newValue: "myNewAlias2", - }; - - modelRepo?.broadcast(delta1 as CoreDelta); - modelRepo?.broadcast(delta2 as CoreDelta); - - clock.tick(1001); - - t.true(processDeltaSpy.calledTwice); - t.is(executor?.operationQueue.length, 1); - t.is(executor?.deltaQueue.length, 0); - - const operation = executor?.operationQueue[0]; - t.is(operation?.changeType, CoreChangeType.Add); // adding identity since old value was "" - t.is(operation?.model?.modelName, ModelName.Identity); - t.deepEqual(operation?.payload, { myAlias: "myNewAlias", myAlias2: "myNewAlias2" }); -}); - -test("Update User Properties -> one operation of change type: update", async t => { - const clock = sinon.useFakeTimers(); - const core = new CoreModule(); - await core.init(); - const { modelRepo, operationRepo } = core; - const executor = operationRepo?.executorStore[ModelName.Properties]; - - modelRepo?.subscribe(() => { - passIfBroadcastNTimes(t, 2, broadcastCount); - }); - const processDeltaSpy = sinonSandbox.spy(OperationRepo.prototype as any, "processDelta"); - - const delta1: CoreDelta = { - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Properties), - property: "tags", - oldValue: undefined, - newValue: { tag2: "tag2" }, - }; - - const delta2: CoreDelta = { - changeType: CoreChangeType.Update, - model: new OSModel(ModelName.Properties), - property: "tags", - oldValue: undefined, - newValue: { tag1: "tag1" }, - }; - - modelRepo?.broadcast(delta1 as CoreDelta); - modelRepo?.broadcast(delta2 as CoreDelta); - - clock.tick(1001); - - t.true(processDeltaSpy.calledTwice); - t.is(executor?.operationQueue.length, 1); - t.is(executor?.deltaQueue.length, 0); - - const operation = executor?.operationQueue[0]; - t.is(operation?.changeType, CoreChangeType.Update); - t.is(operation?.model?.modelName, ModelName.Properties); - t.deepEqual(operation?.payload, { tags: { tag1: "tag1", tag2: "tag2" } }); -}); diff --git a/test/unit/core/_helpers.ts b/test/unit/core/_helpers.ts deleted file mode 100644 index 20ac6efb5..000000000 --- a/test/unit/core/_helpers.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { ExecutionContext } from "ava"; -import { SinonSandbox } from "sinon"; -import ModelCache from "../../../src/core/caching/ModelCache"; -import { OSModel } from "../../../src/core/modelRepo/OSModel"; -import { CoreChangeType } from "../../../src/core/models/CoreChangeType"; -import { CoreDelta } from "../../../src/core/models/CoreDeltas"; -import { SupportedSubscription, SubscriptionType } from "../../../src/core/models/SubscriptionModels"; -import { ModelName, SupportedModel } from "../../../src/core/models/SupportedModels"; - -const MODEL_ID = '0000000000'; - -export function generateNewSubscription() { - return new OSModel( - ModelName.EmailSubscriptions, - { - type: SubscriptionType.Email, - id: '123', // subscription id - token: "myToken", - }, - MODEL_ID, - ); -} - -export function getMockDeltas(): CoreDelta[] { - return [ - { - model: getDummyIdentityOSModel(), - changeType: CoreChangeType.Add, - } - ]; -} - -const dummyIdentityModel = new OSModel(ModelName.Identity, undefined, "123"); - -export function getDummyIdentityOSModel(): OSModel { - return dummyIdentityModel; -} - -export function stubModelCache(sinonSandbox: SinonSandbox) { - sinonSandbox.stub(ModelCache.prototype, "load").resolves(); - sinonSandbox.stub(ModelCache.prototype, "add").resolves(); - sinonSandbox.stub(ModelCache.prototype, "remove").resolves(); - sinonSandbox.stub(ModelCache.prototype, "update").resolves(); -} - -export const passIfBroadcastNTimes = (t: ExecutionContext, target: number, broadcastCount: number) => { - if (broadcastCount === target) { - t.pass(); - } -};