Skip to content

Commit 58cf911

Browse files
committed
add more test
1 parent f196efd commit 58cf911

6 files changed

Lines changed: 145 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { db } from 'src/shared/database/client';
2+
3+
export const setIsPushEnabled = async (isPushEnabled: boolean) => {
4+
await db.put('Options', { key: 'isPushEnabled', value: isPushEnabled });
5+
};

src/core/executors/LoginUserOperationExecutor.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,13 @@ describe('LoginUserOperationExecutor', () => {
164164
identityModelStore.model.setProperty(
165165
IdentityConstants.ONESIGNAL_ID,
166166
DUMMY_ONESIGNAL_ID,
167+
ModelChangeTags.HYDRATE,
168+
);
169+
propertiesModelStore.model.setProperty(
170+
'onesignalId',
171+
DUMMY_ONESIGNAL_ID,
172+
ModelChangeTags.HYDRATE,
167173
);
168-
propertiesModelStore.model.setProperty('onesignalId', DUMMY_ONESIGNAL_ID);
169174
await setPushToken(DUMMY_PUSH_TOKEN);
170175

171176
const subscriptionModel = new SubscriptionModel();
@@ -252,8 +257,13 @@ describe('LoginUserOperationExecutor', () => {
252257
identityModelStore.model.setProperty(
253258
IdentityConstants.ONESIGNAL_ID,
254259
DUMMY_ONESIGNAL_ID,
260+
ModelChangeTags.HYDRATE,
261+
);
262+
propertiesModelStore.model.setProperty(
263+
'onesignalId',
264+
DUMMY_ONESIGNAL_ID,
265+
ModelChangeTags.HYDRATE,
255266
);
256-
propertiesModelStore.model.setProperty('onesignalId', DUMMY_ONESIGNAL_ID);
257267

258268
const executor = getExecutor();
259269

src/core/executors/RefreshUserOperationExecutor.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SomeOperation } from '__test__/support/helpers/executors';
1212
import {
1313
setGetUserError,
1414
setGetUserResponse,
15+
setUpdateSubscriptionResponse,
1516
} from '__test__/support/helpers/requests';
1617
import { clearAll } from 'src/shared/database/client';
1718
import { setPushToken } from 'src/shared/database/subscription';
@@ -28,6 +29,7 @@ import { PropertiesModelStore } from '../modelStores/PropertiesModelStore';
2829
import type { SubscriptionModelStore } from '../modelStores/SubscriptionModelStore';
2930
import { NewRecordsState } from '../operationRepo/NewRecordsState';
3031
import { RefreshUserOperation } from '../operations/RefreshUserOperation';
32+
import { ModelChangeTags } from '../types/models';
3133
import { ExecutionResult } from '../types/operation';
3234
import { RefreshUserOperationExecutor } from './RefreshUserOperationExecutor';
3335

@@ -60,6 +62,11 @@ describe('RefreshUserOperationExecutor', () => {
6062
buildUserService,
6163
'getRebuildOperationsIfCurrentUser',
6264
);
65+
66+
setUpdateSubscriptionResponse({
67+
subscriptionId: '*',
68+
response: {},
69+
});
6370
});
6471

6572
const getExecutor = () => {
@@ -96,6 +103,7 @@ describe('RefreshUserOperationExecutor', () => {
96103
identityModelStore.model.setProperty(
97104
IdentityConstants.ONESIGNAL_ID,
98105
DUMMY_ONESIGNAL_ID,
106+
ModelChangeTags.HYDRATE,
99107
);
100108
});
101109

@@ -186,7 +194,7 @@ describe('RefreshUserOperationExecutor', () => {
186194
pushSubModel.token = DUMMY_PUSH_TOKEN;
187195
pushSubModel.notification_types = NotificationType.Subscribed;
188196

189-
subscriptionModelStore.add(pushSubModel);
197+
subscriptionModelStore.add(pushSubModel, ModelChangeTags.HYDRATE);
190198
await setPushToken(DUMMY_PUSH_TOKEN);
191199

192200
const executor = getExecutor();

src/shared/helpers/init.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import TestContext from '__test__/support/environment/TestContext';
22
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
33
import Context from 'src/page/models/Context';
44
import { type AppConfig } from 'src/shared/config/types';
5-
import type { Mock } from 'vitest';
5+
import type { MockInstance } from 'vitest';
66
import { db } from '../database/client';
77
import * as InitHelper from './init';
88

9-
let isSubscriptionExpiringSpy: Mock;
9+
let isSubscriptionExpiringSpy: MockInstance;
1010

1111
beforeEach(async () => {
1212
await TestEnvironment.initialize();

src/shared/listeners.test.ts

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,127 @@
1+
import { DUMMY_PUSH_TOKEN } from '__test__/constants';
12
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
2-
import OneSignal from 'src/onesignal/OneSignal';
3-
import type { Mock } from 'vitest';
3+
import { createPushSub } from '__test__/support/environment/TestEnvironmentHelpers';
4+
import { setIsPushEnabled } from '__test__/support/helpers/database';
5+
import {
6+
getSubscriptionFn,
7+
MockServiceWorker,
8+
} from '__test__/support/mocks/MockServiceWorker';
9+
import * as eventListeners from 'src/shared/listeners';
10+
import type { MockInstance } from 'vitest';
11+
import { getAppState } from './database/config';
12+
import { setPushToken } from './database/subscription';
413

5-
let emitterSpy: Mock;
14+
Object.defineProperty(global.navigator, 'serviceWorker', {
15+
value: new MockServiceWorker(),
16+
writable: true,
17+
});
18+
19+
let emitterSpy: MockInstance;
620

721
beforeEach(async () => {
822
await TestEnvironment.initialize();
923
emitterSpy = vi.spyOn(OneSignal.emitter, 'on');
1024
});
1125

12-
afterEach(() => {
13-
vi.resetAllMocks();
26+
describe('checkAndTriggerSubscriptionChanged', () => {
27+
test('should trigger subscription changed', async () => {
28+
const changeListener = vi.fn();
29+
OneSignal.User.PushSubscription.addEventListener('change', changeListener);
30+
31+
// no change
32+
getSubscriptionFn.mockResolvedValue({
33+
endpoint: undefined,
34+
});
35+
await setIsPushEnabled(false);
36+
await eventListeners.checkAndTriggerSubscriptionChanged();
37+
38+
expect(changeListener).not.toHaveBeenCalled();
39+
40+
// push enabled change
41+
await setIsPushEnabled(true); // mimic old opt in
42+
changeListener.mockClear();
43+
await eventListeners.checkAndTriggerSubscriptionChanged();
44+
45+
expect(changeListener).toHaveBeenCalledWith({
46+
current: {
47+
id: undefined,
48+
optedIn: false,
49+
token: undefined,
50+
},
51+
previous: {
52+
id: undefined,
53+
optedIn: true,
54+
token: undefined,
55+
},
56+
});
57+
expect(await getAppState()).toMatchObject({
58+
lastKnownOptedIn: false,
59+
lastKnownPushEnabled: false,
60+
lastKnownPushToken: undefined,
61+
lastKnownPushId: undefined,
62+
});
63+
64+
// token change
65+
getSubscriptionFn.mockResolvedValue({
66+
endpoint: DUMMY_PUSH_TOKEN,
67+
});
68+
await setIsPushEnabled(false);
69+
70+
changeListener.mockClear();
71+
await eventListeners.checkAndTriggerSubscriptionChanged();
72+
73+
expect(changeListener).toHaveBeenCalledWith({
74+
current: {
75+
id: undefined,
76+
optedIn: false,
77+
token: DUMMY_PUSH_TOKEN,
78+
},
79+
previous: {
80+
id: undefined,
81+
optedIn: false,
82+
token: undefined,
83+
},
84+
});
85+
expect(await getAppState()).toMatchObject({
86+
lastKnownOptedIn: false,
87+
lastKnownPushEnabled: false,
88+
lastKnownPushToken: DUMMY_PUSH_TOKEN,
89+
lastKnownPushId: undefined,
90+
});
91+
92+
// id change
93+
const token = 'some-token';
94+
const newID = 'some-id';
95+
const pushModel = createPushSub({
96+
id: newID,
97+
token,
98+
});
99+
await setPushToken(token);
100+
OneSignal.coreDirector.subscriptionModelStore.add(pushModel);
101+
getSubscriptionFn.mockResolvedValue({
102+
endpoint: token,
103+
});
104+
105+
await eventListeners.checkAndTriggerSubscriptionChanged();
106+
expect(changeListener).toHaveBeenCalledWith({
107+
current: {
108+
id: newID,
109+
optedIn: false,
110+
token,
111+
},
112+
previous: {
113+
id: undefined,
114+
optedIn: false,
115+
token,
116+
},
117+
});
118+
expect(await getAppState()).toMatchObject({
119+
lastKnownOptedIn: false,
120+
lastKnownPushEnabled: false,
121+
lastKnownPushToken: token,
122+
lastKnownPushId: newID,
123+
});
124+
});
14125
});
15126

16127
test('Adding click listener fires internal EventHelper', async () => {

src/shared/listeners.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export async function checkAndTriggerSubscriptionChanged() {
3838

3939
const pushModel = await OneSignal.coreDirector.getPushSubscriptionModel();
4040
const pushSubscriptionId = pushModel?.id;
41+
4142
const didStateChange =
4243
isPushEnabled !== lastKnownPushEnabled ||
4344
currentPushToken !== lastKnownPushToken ||

0 commit comments

Comments
 (0)