Skip to content

Commit 6d45c45

Browse files
committed
more renames
fix tests
1 parent dd65cf0 commit 6d45c45

File tree

19 files changed

+180
-202
lines changed

19 files changed

+180
-202
lines changed

__test__/support/environment/TestEnvironmentHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function initOSGlobals(config: TestEnvironmentConfig = {}) {
3232
global.OneSignal._coreDirector = new CoreModuleDirector(core);
3333

3434
// Clear the User singleton before creating new instance
35-
User.singletonInstance = undefined;
35+
User._singletonInstance = undefined;
3636

3737
const userNamespace = new UserNamespace(!!config.initUserAndPushSubscription); // TO DO: pass in subscription, and permission
3838
global.OneSignal.User = userNamespace;

__test__/unit/core/coreModuleDirector.test.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ describe('CoreModuleDirector tests', () => {
2323
const pushModelCurrent = generateNewSubscription();
2424
vi.spyOn(
2525
CoreModuleDirector.prototype,
26-
// @ts-expect-error - private method
27-
'getPushSubscriptionModelByCurrentToken',
28-
// @ts-expect-error - private method
26+
'_getPushSubscriptionModelByCurrentToken',
2927
).mockResolvedValue(pushModelCurrent);
3028
expect(await getPushSubscriptionModel()).toBe(pushModelCurrent);
3129
});
@@ -34,9 +32,7 @@ describe('CoreModuleDirector tests', () => {
3432
const pushModelLastKnown = generateNewSubscription();
3533
vi.spyOn(
3634
CoreModuleDirector.prototype,
37-
// @ts-expect-error - private method
38-
'getPushSubscriptionModelByLastKnownToken',
39-
// @ts-expect-error - private method
35+
'_getPushSubscriptionModelByLastKnownToken',
4036
).mockResolvedValue(pushModelLastKnown);
4137
expect(await getPushSubscriptionModel()).toEqual(pushModelLastKnown);
4238
});
@@ -45,17 +41,13 @@ describe('CoreModuleDirector tests', () => {
4541
const pushModelCurrent = generateNewSubscription();
4642
vi.spyOn(
4743
CoreModuleDirector.prototype,
48-
// @ts-expect-error - private method
49-
'getPushSubscriptionModelByCurrentToken',
50-
// @ts-expect-error - private method
44+
'_getPushSubscriptionModelByCurrentToken',
5145
).mockResolvedValue(pushModelCurrent);
5246

5347
const pushModelLastKnown = generateNewSubscription();
5448
vi.spyOn(
5549
CoreModuleDirector.prototype,
56-
// @ts-expect-error - private method
57-
'getPushSubscriptionModelByLastKnownToken',
58-
// @ts-expect-error - private method
50+
'_getPushSubscriptionModelByLastKnownToken',
5951
).mockResolvedValue(pushModelLastKnown);
6052

6153
expect(await getPushSubscriptionModel()).toBe(pushModelCurrent);

__test__/unit/user/user.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('User tests', () => {
88
test('getTags called with unset tags should return empty tags', () => {
99
TestEnvironment.initialize();
1010

11-
const user = User.createOrGetInstance();
11+
const user = User._createOrGetInstance();
1212
const tags = user.getTags();
1313

1414
expect(tags).toStrictEqual({});
@@ -17,7 +17,7 @@ describe('User tests', () => {
1717
test('getTags called with empty tags in properties model should return empty tags', () => {
1818
TestEnvironment.initialize();
1919

20-
const user = User.createOrGetInstance();
20+
const user = User._createOrGetInstance();
2121
const tags = user.getTags();
2222

2323
expect(tags).toStrictEqual({});
@@ -30,7 +30,7 @@ describe('User tests', () => {
3030
const propModel = OneSignal._coreDirector._getPropertiesModel();
3131
propModel.tags = tagsSample;
3232

33-
const user = User.createOrGetInstance();
33+
const user = User._createOrGetInstance();
3434
const tags = user.getTags();
3535

3636
expect(tags).toBe(tagsSample);
@@ -44,7 +44,7 @@ describe('User tests', () => {
4444
const propModel = OneSignal._coreDirector._getPropertiesModel();
4545
propModel.language = languageSample;
4646

47-
const user = User.createOrGetInstance();
47+
const user = User._createOrGetInstance();
4848
const language = user.getLanguage();
4949

5050
expect(language).toBe(languageSample);
@@ -55,7 +55,7 @@ describe('User tests', () => {
5555

5656
const languageSample = 'fr';
5757

58-
const user = User.createOrGetInstance();
58+
const user = User._createOrGetInstance();
5959
user.setLanguage(languageSample);
6060

6161
const propModel = OneSignal._coreDirector._getPropertiesModel();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
{
8686
"path": "./build/releases/OneSignalSDK.page.es6.js",
87-
"limit": "49.79 kB",
87+
"limit": "49.631 kB",
8888
"gzip": true
8989
},
9090
{

src/core/CoreModuleDirector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class CoreModuleDirector {
113113
);
114114
}
115115

116-
private async _getPushSubscriptionModelByCurrentToken(): Promise<
116+
async _getPushSubscriptionModelByCurrentToken(): Promise<
117117
SubscriptionModel | undefined
118118
> {
119119
logMethodCall('CoreModuleDirector.getPushSubscriptionModelByCurrentToken');
@@ -129,7 +129,7 @@ export class CoreModuleDirector {
129129

130130
// Browser may return a different PushToken value, use the last-known value as a fallback.
131131
// - This happens if you disable notification permissions then re-enable them.
132-
private async _getPushSubscriptionModelByLastKnownToken(): Promise<
132+
async _getPushSubscriptionModelByLastKnownToken(): Promise<
133133
SubscriptionModel | undefined
134134
> {
135135
logMethodCall(

src/onesignal/User.ts

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import type { SubscriptionTypeValue } from 'src/shared/subscriptions/types';
2121
import { logMethodCall } from 'src/shared/utils/utils';
2222

2323
export default class User {
24-
static singletonInstance?: User;
24+
static _singletonInstance?: User;
2525

2626
/**
2727
* Creates a user singleton
2828
* @returns - User singleton
2929
*/
30-
static createOrGetInstance(): User {
31-
if (!User.singletonInstance) {
32-
User.singletonInstance = new User();
30+
static _createOrGetInstance(): User {
31+
if (!User._singletonInstance) {
32+
User._singletonInstance = new User();
3333
const identityModel = OneSignal._coreDirector._getIdentityModel();
3434
const propertiesModel = OneSignal._coreDirector._getPropertiesModel();
3535

@@ -52,46 +52,10 @@ export default class User {
5252
}
5353
}
5454

55-
return User.singletonInstance;
55+
return User._singletonInstance;
5656
}
5757

58-
get onesignalId(): string | undefined {
59-
const onesignalId = OneSignal._coreDirector._getIdentityModel().onesignalId;
60-
return IDManager._isLocalId(onesignalId) ? undefined : onesignalId;
61-
}
62-
63-
private validateStringLabel(label: string, labelName: string): void {
64-
if (typeof label !== 'string') throw WrongTypeArgumentError(labelName);
65-
66-
if (!label) throw EmptyArgumentError(labelName);
67-
}
68-
69-
private validateArray(array: string[], arrayName: string): void {
70-
if (!Array.isArray(array)) throw WrongTypeArgumentError(arrayName);
71-
72-
if (array.length === 0) throw EmptyArgumentError(arrayName);
73-
74-
for (const label of array) {
75-
this.validateLabel(label, 'label');
76-
}
77-
}
78-
79-
private validateObject(object: unknown, objectName: string): void {
80-
if (!isObject(object)) throw WrongTypeArgumentError(objectName);
81-
82-
if (!object || Object.keys(object).length === 0)
83-
throw EmptyArgumentError(objectName);
84-
}
85-
86-
private validateLabel(label: string, labelName: string): void {
87-
this.validateStringLabel(label, labelName);
88-
89-
if (label === 'external_id' || label === 'onesignal_id') {
90-
throw ReservedArgumentError(label);
91-
}
92-
}
93-
94-
private updateIdentityModel(aliases: {
58+
private _updateIdentityModel(aliases: {
9559
[key: string]: string | undefined;
9660
}): void {
9761
const identityModel = OneSignal._coreDirector._getIdentityModel();
@@ -101,12 +65,17 @@ export default class User {
10165
}
10266

10367
/* PUBLIC API METHODS */
68+
get onesignalId(): string | undefined {
69+
const onesignalId = OneSignal._coreDirector._getIdentityModel().onesignalId;
70+
return IDManager._isLocalId(onesignalId) ? undefined : onesignalId;
71+
}
72+
10473
public addAlias(label: string, id: string): void {
10574
logMethodCall('addAlias', { label, id });
10675
if (isConsentRequiredButNotGiven()) return;
10776

108-
this.validateStringLabel(label, 'label');
109-
this.validateStringLabel(id, 'id');
77+
validateStringLabel(label, 'label');
78+
validateStringLabel(id, 'id');
11079

11180
this.addAliases({ [label]: id });
11281
}
@@ -115,14 +84,14 @@ export default class User {
11584
logMethodCall('addAliases', { aliases });
11685
if (isConsentRequiredButNotGiven()) return;
11786

118-
this.validateObject(aliases, 'aliases');
87+
validateObject(aliases, 'aliases');
11988

12089
for (const label of Object.keys(aliases)) {
121-
this.validateStringLabel(aliases[label], `key: ${label}`);
122-
this.validateLabel(label, `key: ${label}`);
90+
validateStringLabel(aliases[label], `key: ${label}`);
91+
validateLabel(label, `key: ${label}`);
12392
}
12493

125-
this.updateIdentityModel(aliases);
94+
this._updateIdentityModel(aliases);
12695
}
12796

12897
public removeAlias(label: string): void {
@@ -136,19 +105,19 @@ export default class User {
136105
logMethodCall('removeAliases', { aliases });
137106
if (isConsentRequiredButNotGiven()) return;
138107

139-
this.validateArray(aliases, 'aliases');
108+
validateArray(aliases, 'aliases');
140109

141110
const newAliases = Object.fromEntries(
142111
aliases.map((key) => [key, undefined]),
143112
);
144-
this.updateIdentityModel(newAliases);
113+
this._updateIdentityModel(newAliases);
145114
}
146115

147116
public addEmail(email: string): void {
148117
logMethodCall('addEmail', { email });
149118
if (isConsentRequiredButNotGiven()) return;
150119

151-
this.validateStringLabel(email, 'email');
120+
validateStringLabel(email, 'email');
152121

153122
if (!isValidEmail(email)) throw MalformedArgumentError('email');
154123

@@ -162,7 +131,7 @@ export default class User {
162131
logMethodCall('addSms', { sms });
163132
if (isConsentRequiredButNotGiven()) return;
164133

165-
this.validateStringLabel(sms, 'sms');
134+
validateStringLabel(sms, 'sms');
166135

167136
addSubscriptionToModels({
168137
type: SubscriptionType.SMS,
@@ -174,7 +143,7 @@ export default class User {
174143
logMethodCall('removeEmail', { email });
175144
if (isConsentRequiredButNotGiven()) return;
176145

177-
this.validateStringLabel(email, 'email');
146+
validateStringLabel(email, 'email');
178147

179148
const emailSubscriptions =
180149
OneSignal._coreDirector._getEmailSubscriptionModels();
@@ -190,7 +159,7 @@ export default class User {
190159
logMethodCall('removeSms', { smsNumber });
191160
if (isConsentRequiredButNotGiven()) return;
192161

193-
this.validateStringLabel(smsNumber, 'smsNumber');
162+
validateStringLabel(smsNumber, 'smsNumber');
194163

195164
const smsSubscriptions =
196165
OneSignal._coreDirector._getSmsSubscriptionModels();
@@ -205,8 +174,8 @@ export default class User {
205174
logMethodCall('addTag', { key, value });
206175
if (isConsentRequiredButNotGiven()) return;
207176

208-
this.validateStringLabel(key, 'key');
209-
this.validateStringLabel(value, 'value');
177+
validateStringLabel(key, 'key');
178+
validateStringLabel(value, 'value');
210179

211180
this.addTags({ [key]: value });
212181
}
@@ -223,7 +192,7 @@ export default class User {
223192
logMethodCall('removeTag', { tagKey });
224193
if (isConsentRequiredButNotGiven()) return;
225194

226-
this.validateStringLabel(tagKey, 'tagKey');
195+
validateStringLabel(tagKey, 'tagKey');
227196

228197
this.removeTags([tagKey]);
229198
}
@@ -232,7 +201,7 @@ export default class User {
232201
logMethodCall('removeTags', { tagKeys });
233202
if (isConsentRequiredButNotGiven()) return;
234203

235-
this.validateArray(tagKeys, 'tagKeys');
204+
validateArray(tagKeys, 'tagKeys');
236205

237206
const propertiesModel = OneSignal._coreDirector._getPropertiesModel();
238207
const newTags = { ...propertiesModel.tags };
@@ -253,7 +222,7 @@ export default class User {
253222
logMethodCall('setLanguage', { language });
254223
if (isConsentRequiredButNotGiven()) return;
255224

256-
this.validateStringLabel(language, 'language');
225+
validateStringLabel(language, 'language');
257226

258227
const propertiesModel = OneSignal._coreDirector._getPropertiesModel();
259228
propertiesModel.language = language;
@@ -347,3 +316,34 @@ function isObjectSerializable(value: unknown): boolean {
347316
return false;
348317
}
349318
}
319+
320+
function validateStringLabel(label: string, labelName: string): void {
321+
if (typeof label !== 'string') throw WrongTypeArgumentError(labelName);
322+
323+
if (!label) throw EmptyArgumentError(labelName);
324+
}
325+
326+
function validateArray(array: string[], arrayName: string): void {
327+
if (!Array.isArray(array)) throw WrongTypeArgumentError(arrayName);
328+
329+
if (array.length === 0) throw EmptyArgumentError(arrayName);
330+
331+
for (const label of array) {
332+
validateLabel(label, 'label');
333+
}
334+
}
335+
336+
function validateObject(object: unknown, objectName: string): void {
337+
if (!isObject(object)) throw WrongTypeArgumentError(objectName);
338+
339+
if (!object || Object.keys(object).length === 0)
340+
throw EmptyArgumentError(objectName);
341+
}
342+
343+
function validateLabel(label: string, labelName: string): void {
344+
validateStringLabel(label, labelName);
345+
346+
if (label === 'external_id' || label === 'onesignal_id') {
347+
throw ReservedArgumentError(label);
348+
}
349+
}

src/onesignal/UserDirector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IDManager } from 'src/shared/managers/IDManager';
77
import MainHelper from '../shared/helpers/MainHelper';
88

99
export default class UserDirector {
10-
static async createUserOnServer(): Promise<void> {
10+
static async _createUserOnServer(): Promise<void> {
1111
const identityModel = OneSignal._coreDirector._getIdentityModel();
1212
const appId = MainHelper.getAppId();
1313

@@ -55,7 +55,7 @@ export default class UserDirector {
5555

5656
// Resets models similar to Android SDK
5757
// https://github.com/OneSignal/OneSignal-Android-SDK/blob/ed2e87618ea3af81b75f97b0a4cbb8f658c7fc80/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt#L448
58-
static resetUserModels() {
58+
static _resetUserModels() {
5959
// replace models
6060
const newIdentityModel = new IdentityModel();
6161
const newPropertiesModel = new PropertiesModel();

src/onesignal/UserNamespace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ vi.useFakeTimers();
1616

1717
const setup = () => {
1818
TestEnvironment.initialize({});
19-
delete User.singletonInstance;
19+
delete User._singletonInstance;
2020
};
2121

2222
setup();

src/onesignal/UserNamespace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class UserNamespace extends EventListenerBase {
1919
) {
2020
super();
2121
if (initialize) {
22-
this._currentUser = User.createOrGetInstance();
22+
this._currentUser = User._createOrGetInstance();
2323
this.PushSubscription = new PushSubscriptionNamespace(
2424
true,
2525
subscription,

0 commit comments

Comments
 (0)