Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add getTags to public API #1140

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ All user functions are synchronous.
| `addTags` | Adds multiple tags for the current user. | `tags: { [key: string]: string }` |
| `removeTag` | Removes a tag for the current user. | `key: string` |
| `removeTags` | Removes multiple tags for the current user. | `keys: string[]` |
| `getTags` | Gets the current user's tags. | |

### Notifications Namespace

Expand Down
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);
});
});
6 changes: 6 additions & 0 deletions api.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@
}
],
"returnType": "void"
},
{
"name": "getTags",
"isAsync": false,
"args": [],
"returnType": "{ [key: string]: string }"
}
],
"namespaces": ["PushSubscription"]
Expand Down
6 changes: 6 additions & 0 deletions src/onesignal/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,10 @@ export default class User {
propertiesModel?.set('tags', tagsCopy);
}
}

public getTags(): { [key: string]: string } {
logMethodCall('getTags');

return OneSignal.coreDirector.getPropertiesModel()?.data?.tags;
}
}
4 changes: 4 additions & 0 deletions src/onesignal/UserNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ export default class UserNamespace {
public removeTags(keys: string[]): void {
this._currentUser?.removeTags(keys);
}

public getTags(): { [key: string]: string } {
return this._currentUser?.getTags() ? this._currentUser.getTags() : {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can shorten this by using || instead of the ternary operator here.

}
}
Loading