From fcd87ab698ace24c645f7ee116199dec67f95e2f Mon Sep 17 00:00:00 2001 From: Shepherd Date: Fri, 17 Nov 2023 18:49:05 -0500 Subject: [PATCH] Add getTags unit tests --- __test__/support/helpers/core.ts | 7 +++ __test__/unit/user/user.test.ts | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 __test__/unit/user/user.test.ts diff --git a/__test__/support/helpers/core.ts b/__test__/support/helpers/core.ts index 6f1cc255b..ef05310e0 100644 --- a/__test__/support/helpers/core.ts +++ b/__test__/support/helpers/core.ts @@ -14,6 +14,7 @@ import { } from '../constants'; import CoreModule from '../../../src/core/CoreModule'; import { CoreModuleDirector } from '../../../src/core/CoreModuleDirector'; +import { UserPropertiesModel } from 'src/core/models/UserPropertiesModel'; export function generateNewSubscription(modelId = '0000000000') { return new OSModel( @@ -42,6 +43,12 @@ export function getDummyIdentityOSModel( return new OSModel(ModelName.Identity, {}, modelId); } +export function getDummyPropertyOSModel( + modelId = DUMMY_MODEL_ID, +): OSModel { + return new OSModel(ModelName.Properties, {}, modelId); +} + export function getDummyPushSubscriptionOSModel(): OSModel { return new OSModel( ModelName.PushSubscriptions, diff --git a/__test__/unit/user/user.test.ts b/__test__/unit/user/user.test.ts new file mode 100644 index 000000000..2a5781df2 --- /dev/null +++ b/__test__/unit/user/user.test.ts @@ -0,0 +1,77 @@ +import { TestEnvironment } from '../../support/environment/TestEnvironment'; +import User from '../../../src/onesignal/User'; +import { ModelName } from '../../../src/core/models/SupportedModels'; +import { getDummyPropertyOSModel } from '../../support/helpers/core'; + +// suppress all internal logging +jest.mock('../../../src/shared/libraries/Log'); + +describe('User tests', () => { + test('getTags called without a properties model should return undefined tags', async () => { + await TestEnvironment.initialize(); + + const user = User.createOrGetInstance(); + const tags = user.getTags(); + + expect(tags).toBe(undefined); + }); + + test('getTags called with undefined tags in properties model should return undefined tags', async () => { + await TestEnvironment.initialize(); + + OneSignal.coreDirector.add(ModelName.Properties, getDummyPropertyOSModel()); + + const user = User.createOrGetInstance(); + const tags = user.getTags(); + + expect(tags).toBe(undefined); + }); + + test('getTags called with empty tags in properties model should return empty tags', async () => { + await TestEnvironment.initialize(); + + const propertyModel = getDummyPropertyOSModel(); + propertyModel.set('tags', {}); + OneSignal.coreDirector.add(ModelName.Properties, propertyModel); + + const user = User.createOrGetInstance(); + const tags = user.getTags(); + + expect(tags).toStrictEqual({}); + }); + + test('getTags called with tags in properties model should return tags', async () => { + await TestEnvironment.initialize(); + + const tagsSample = { key1: 'value1' }; + + const propertyModel = getDummyPropertyOSModel(); + propertyModel.set('tags', tagsSample); + OneSignal.coreDirector.add(ModelName.Properties, propertyModel); + + const user = User.createOrGetInstance(); + const tags = user.getTags(); + + expect(tags).toBe(tagsSample); + }); + + test("getTags called after addTags should return the user's tags with the added tags", async () => { + await TestEnvironment.initialize(); + + const tagsSample = { key1: 'value1' }; + const tagsToAdd = { key2: 'value2', key3: 'value3' }; + + const propertyModel = getDummyPropertyOSModel(); + propertyModel.set('tags', tagsSample); + OneSignal.coreDirector.add(ModelName.Properties, propertyModel); + + const user = User.createOrGetInstance(); + const tagsSnapshot1 = user.getTags(); + + user.addTags(tagsToAdd); + const tagsSnapshot2 = user.getTags(); + + expect(tagsSnapshot1).toBe(tagsSample); + expect(tagsSnapshot2).toStrictEqual({ ...tagsSample, ...tagsToAdd }); + }); +});