Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions __test__/support/environment/TestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type {
ServerAppConfig,
} from 'src/shared/config/types';
import type { RecursivePartial } from 'src/shared/context/types';
import { clearAll } from 'src/shared/database/client';
import MainHelper from 'src/shared/helpers/MainHelper';
import { DUMMY_ONESIGNAL_ID, DUMMY_PUSH_TOKEN } from '../../constants';
import { generateNewSubscription } from '../helpers/core';
import {
initOSGlobals,
resetDatabase,
stubDomEnvironment,
stubNotification,
} from './TestEnvironmentHelpers';
Expand All @@ -32,8 +32,7 @@ export interface TestEnvironmentConfig {
export class TestEnvironment {
static async initialize(config: TestEnvironmentConfig = {}) {
// reset db & localStorage
resetDatabase();

await clearAll();
const oneSignal = await initOSGlobals(config);

if (config.useMockIdentityModel) {
Expand Down
13 changes: 3 additions & 10 deletions __test__/support/environment/TestEnvironmentHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type DOMWindow, JSDOM, ResourceLoader } from 'jsdom';
import CoreModule from 'src/core/CoreModule';
import { SubscriptionModel } from 'src/core/models/SubscriptionModel';
import { ModelChangeTags } from 'src/core/types/models';
import { setPushId, setPushToken } from 'src/shared/database/subscription';
import {
NotificationType,
SubscriptionType,
Expand All @@ -14,7 +15,6 @@ import UserNamespace from '../../../src/onesignal/UserNamespace';
import Context from '../../../src/page/models/Context';
import { getSlidedownElement } from '../../../src/page/slidedown/SlidedownElement';
import Emitter from '../../../src/shared/libraries/Emitter';
import Database from '../../../src/shared/services/Database';
import { CUSTOM_LINK_CSS_CLASSES } from '../../../src/shared/slidedown/constants';
import {
DEFAULT_USER_AGENT,
Expand All @@ -23,18 +23,11 @@ import {
DUMMY_SUBSCRIPTION_ID_3,
} from '../../constants';
import MockNotification from '../mocks/MockNotification';
import Random from '../utils/Random';
import TestContext from './TestContext';
import { type TestEnvironmentConfig } from './TestEnvironment';

declare const global: any;

export function resetDatabase() {
// Erase and reset IndexedDb database name to something random
Database.resetInstance();
Database.databaseInstanceName = Random.getRandomString(10);
}

export async function initOSGlobals(config: TestEnvironmentConfig = {}) {
global.OneSignal = OneSignal;
global.OneSignal.EVENTS = ONESIGNAL_EVENTS;
Expand Down Expand Up @@ -153,8 +146,8 @@ export const setupSubModelStore = async ({
token,
onesignalId,
});
await Database.setPushId(pushModel.id);
await Database.setPushToken(pushModel.token);
await setPushId(pushModel.id);
await setPushToken(pushModel.token);
OneSignal.coreDirector.subscriptionModelStore.replaceAll(
[pushModel],
ModelChangeTags.NO_PROPOGATE,
Expand Down
4 changes: 0 additions & 4 deletions __test__/unit/models/deliveryPlatformKind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ describe('DeliveryPlatformKind', () => {
expect(DeliveryPlatformKind.ChromeLike).toBe(5);
expect(DeliveryPlatformKind.SafariLegacy).toBe(7);
expect(DeliveryPlatformKind.Firefox).toBe(8);
expect(DeliveryPlatformKind.Email).toBe(11);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

unused constants

expect(DeliveryPlatformKind.Edge).toBe(12);
expect(DeliveryPlatformKind.SMS).toBe(14);
expect(DeliveryPlatformKind.SafariVapid).toBe(17);
});
});
2 changes: 1 addition & 1 deletion __test__/unit/notifications/permission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function expectPermissionChangeEvent(

describe('Notifications namespace permission properties', () => {
beforeEach(async () => {
TestEnvironment.initialize();
await TestEnvironment.initialize();
});

afterEach(() => {
Expand Down
19 changes: 8 additions & 11 deletions __test__/unit/pushSubscription/nativePermissionChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
import { createPushSub } from '__test__/support/environment/TestEnvironmentHelpers';
import { MockServiceWorker } from '__test__/support/mocks/MockServiceWorker';
import { db, getOptionsValue } from 'src/shared/database/client';
import { setAppState as setDBAppState } from 'src/shared/database/config';
import * as PermissionUtils from 'src/shared/helpers/permissions';
import Emitter from 'src/shared/libraries/Emitter';
import { checkAndTriggerSubscriptionChanged } from 'src/shared/listeners';
import { AppState } from 'src/shared/models/AppState';
import Database from 'src/shared/services/Database';
import MainHelper from '../../../src/shared/helpers/MainHelper';
import { NotificationPermission } from '../../../src/shared/models/NotificationPermission';

Expand All @@ -31,12 +32,12 @@ describe('Notification Types are set correctly on subscription change', () => {
});

afterEach(async () => {
await Database.remove('subscriptions');
await Database.remove('Options');
await db.clear('subscriptions');
await db.clear('Options');
});

const setDbPermission = async (permission: NotificationPermission) => {
await Database.put('Options', {
await db.put('Options', {
key: 'notificationPermission',
value: permission,
});
Expand Down Expand Up @@ -77,8 +78,7 @@ describe('Notification Types are set correctly on subscription change', () => {
await MainHelper.checkAndTriggerNotificationPermissionChanged();

// should update the db
const dbPermission = await Database.get(
'Options',
const dbPermission = await getOptionsValue<NotificationPermission>(
'notificationPermission',
);
expect(dbPermission).toBe(NotificationPermission.Granted);
Expand All @@ -89,11 +89,8 @@ describe('Notification Types are set correctly on subscription change', () => {

describe('checkAndTriggerSubscriptionChanged', async () => {
const setAppState = async (appState: Partial<AppState>) => {
const currentAppState = await Database.get<AppState>(
'Options',
'appState',
);
await Database.setAppState({
const currentAppState = (await getOptionsValue<AppState>('appState'))!;
await setDBAppState({
...currentAppState,
...appState,
});
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Web push notifications from OneSignal.",
"type": "module",
"dependencies": {
"idb": "^8.0.3",
"jsonp": "github:OneSignal/jsonp#onesignal",
"uuid": "^11.1.0"
},
Expand Down Expand Up @@ -75,17 +76,17 @@
"size-limit": [
{
"path": "./build/releases/OneSignalSDK.page.js",
"limit": "630 B",
"limit": "490 B",
"gzip": true
},
{
"path": "./build/releases/OneSignalSDK.page.es6.js",
"limit": "58.6 kB",
"limit": "55.08 kB",
"gzip": true
},
{
"path": "./build/releases/OneSignalSDK.sw.js",
"limit": "22.4 kB",
"limit": "15.42 kB",
"gzip": true
},
{
Expand Down
8 changes: 4 additions & 4 deletions src/core/CoreModuleDirector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import FuturePushSubscriptionRecord from 'src/page/userModel/FuturePushSubscriptionRecord';
import { getPushToken, setPushId } from 'src/shared/database/subscription';
import { IDManager } from 'src/shared/managers/IDManager';
import {
SubscriptionChannel,
SubscriptionType,
} from 'src/shared/subscriptions/constants';
import type { SubscriptionChannelValue } from 'src/shared/subscriptions/types';
import { logMethodCall } from 'src/shared/utils/utils';
import SubscriptionHelper from '../../src/shared/helpers/SubscriptionHelper';
import MainHelper from '../shared/helpers/MainHelper';
import { RawPushSubscription } from '../shared/models/RawPushSubscription';
import Database from '../shared/services/Database';
import { logMethodCall } from '../shared/utils/utils';
import CoreModule from './CoreModule';
import { IdentityModel } from './models/IdentityModel';
import { PropertiesModel } from './models/PropertiesModel';
Expand Down Expand Up @@ -61,7 +61,7 @@ export class CoreModuleDirector {
new FuturePushSubscriptionRecord(rawPushSubscription).serialize(),
);
model.id = IDManager.createLocalId();
Database.setPushId(model.id);
setPushId(model.id);

// we enqueue a login operation w/ a create subscription operation the first time we generate/save a push subscription model
this.core.subscriptionModelStore.add(model, ModelChangeTags.HYDRATE);
Expand Down Expand Up @@ -136,7 +136,7 @@ export class CoreModuleDirector {
logMethodCall(
'CoreModuleDirector.getPushSubscriptionModelByLastKnownToken',
);
const lastKnownPushToken = await Database.getPushToken();
const lastKnownPushToken = await getPushToken();
if (lastKnownPushToken) {
return this.getSubscriptionOfTypeWithToken(
SubscriptionChannel.Push,
Expand Down
9 changes: 5 additions & 4 deletions src/core/executors/LoginUserOperationExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
setCreateUserError,
setCreateUserResponse,
} from '__test__/support/helpers/requests';
import Database from 'src/shared/services/Database';
import { clearAll } from 'src/shared/database/client';
import { getPushId, setPushId } from 'src/shared/database/subscription';
import {
NotificationType,
SubscriptionType,
Expand Down Expand Up @@ -55,7 +56,7 @@ describe('LoginUserOperationExecutor', () => {
});

beforeEach(async () => {
await Database.clear();
await clearAll();
identityModelStore = new IdentityModelStore();
propertiesModelStore = new PropertiesModelStore();
subscriptionModelStore = new SubscriptionModelStore();
Expand Down Expand Up @@ -165,7 +166,7 @@ describe('LoginUserOperationExecutor', () => {
DUMMY_ONESIGNAL_ID,
);
propertiesModelStore.model.setProperty('onesignalId', DUMMY_ONESIGNAL_ID);
await Database.setPushId(DUMMY_SUBSCRIPTION_ID);
await setPushId(DUMMY_SUBSCRIPTION_ID);

const subscriptionModel = new SubscriptionModel();
subscriptionModel.setProperty('id', DUMMY_SUBSCRIPTION_ID);
Expand All @@ -191,7 +192,7 @@ describe('LoginUserOperationExecutor', () => {
expect(propertiesModelStore.model.getProperty('onesignalId')).toEqual(
DUMMY_ONESIGNAL_ID_2,
);
expect(await Database.getPushId()).toEqual(DUMMY_SUBSCRIPTION_ID_2);
expect(await getPushId()).toEqual(DUMMY_SUBSCRIPTION_ID_2);
expect(subscriptionModel.getProperty('id')).toEqual(
DUMMY_SUBSCRIPTION_ID_2,
);
Expand Down
15 changes: 10 additions & 5 deletions src/core/executors/LoginUserOperationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {
ExecutionResult,
type IOperationExecutor,
} from 'src/core/types/operation';
import { getConsentGiven } from 'src/shared/database/config';
import {
getPushId,
setPushId,
setPushToken,
} from 'src/shared/database/subscription';
import { getTimeZoneId } from 'src/shared/helpers/general';
import { getConsentRequired } from 'src/shared/helpers/localStorage';
import {
Expand All @@ -11,7 +17,6 @@ import {
} from 'src/shared/helpers/NetworkUtils';
import Log from 'src/shared/libraries/Log';
import { checkAndTriggerUserChanged } from 'src/shared/listeners';
import Database from 'src/shared/services/Database';
import { IdentityConstants, OPERATION_NAME } from '../constants';
import { type IPropertiesModelKeys } from '../models/PropertiesModel';
import { type IdentityModelStore } from '../modelStores/IdentityModelStore';
Expand Down Expand Up @@ -80,7 +85,7 @@ export class LoginUserOperationExecutor implements IOperationExecutor {
operations: Operation[],
): Promise<ExecutionResponse> {
const consentRequired = getConsentRequired();
const consentGiven = await Database.getConsentGiven();
const consentGiven = await getConsentGiven();

if (consentRequired && !consentGiven) {
throw new Error('Consent required but not given');
Expand Down Expand Up @@ -231,10 +236,10 @@ export class LoginUserOperationExecutor implements IOperationExecutor {
if (!backendSub || !('id' in backendSub)) continue;
idTranslations[localId] = backendSub.id;

const pushSubscriptionId = await Database.getPushId();
const pushSubscriptionId = await getPushId();
if (pushSubscriptionId === localId) {
await Database.setPushId(backendSub.id);
await Database.setPushToken(backendSub.token);
await setPushId(backendSub.id);
await setPushToken(backendSub.token);
}

const model =
Expand Down
7 changes: 4 additions & 3 deletions src/core/executors/RefreshUserOperationExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
setGetUserError,
setGetUserResponse,
} from '__test__/support/helpers/requests';
import Database from 'src/shared/services/Database';
import { clearAll } from 'src/shared/database/client';
import { setPushId } from 'src/shared/database/subscription';
import {
NotificationType,
SubscriptionType,
Expand Down Expand Up @@ -40,7 +41,7 @@ vi.mock('src/shared/libraries/Log');

describe('RefreshUserOperationExecutor', () => {
beforeEach(async () => {
await Database.clear(); // in case subscription model (from previous tests) are loaded from db
await clearAll(); // in case subscription model (from previous tests) are loaded from db
identityModelStore = new IdentityModelStore();
propertiesModelStore = new PropertiesModelStore();
subscriptionModelStore = new SubscriptionModelStore();
Expand Down Expand Up @@ -181,7 +182,7 @@ describe('RefreshUserOperationExecutor', () => {
pushSubModel.notification_types = NotificationType.Subscribed;

subscriptionModelStore.add(pushSubModel);
await Database.setPushId(DUMMY_SUBSCRIPTION_ID_2);
await setPushId(DUMMY_SUBSCRIPTION_ID_2);

const executor = getExecutor();
const refreshOp = new RefreshUserOperation(APP_ID, DUMMY_ONESIGNAL_ID);
Expand Down
4 changes: 2 additions & 2 deletions src/core/executors/RefreshUserOperationExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getPushId } from 'src/shared/database/subscription';
import {
getResponseStatusType,
ResponseStatusType,
} from 'src/shared/helpers/NetworkUtils';
import SubscriptionHelper from 'src/shared/helpers/SubscriptionHelper';
import Log from 'src/shared/libraries/Log';
import Database from 'src/shared/services/Database';
import { NotificationType } from 'src/shared/subscriptions/constants';
import { IdentityConstants, OPERATION_NAME } from '../constants';
import { IdentityModel } from '../models/IdentityModel';
Expand Down Expand Up @@ -120,7 +120,7 @@ export class RefreshUserOperationExecutor implements IOperationExecutor {
}
}

const pushSubscriptionId = await Database.getPushId();
const pushSubscriptionId = await getPushId();

if (pushSubscriptionId) {
const cachedPushModel =
Expand Down
Loading
Loading