Skip to content

Commit

Permalink
Add getTags unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherd-l committed Nov 18, 2023
1 parent 7db3f30 commit 0c4997c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions __test__/support/helpers/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SupportedSubscription>(
Expand Down Expand Up @@ -42,6 +43,12 @@ export function getDummyIdentityOSModel(
return new OSModel<SupportedIdentity>(ModelName.Identity, {}, modelId);
}

export function getDummyPropertyOSModel(
modelId = DUMMY_MODEL_ID,
): OSModel<UserPropertiesModel> {
return new OSModel<UserPropertiesModel>(ModelName.Properties, {}, modelId);
}

export function getDummyPushSubscriptionOSModel(): OSModel<SupportedSubscription> {
return new OSModel<SupportedSubscription>(
ModelName.PushSubscriptions,
Expand Down
57 changes: 57 additions & 0 deletions __test__/unit/user/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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);
});
});

0 comments on commit 0c4997c

Please sign in to comment.