Skip to content

Commit b9d041a

Browse files
committed
add tests for getPushSubscriptionModel
1 parent a4f3129 commit b9d041a

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

__test__/support/helpers/core.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { CoreDelta } from "../../../src/core/models/CoreDeltas";
55
import { SupportedSubscription, SubscriptionType } from "../../../src/core/models/SubscriptionModels";
66
import { ModelName } from "../../../src/core/models/SupportedModels";
77
import { DUMMY_MODEL_ID, DUMMY_PUSH_TOKEN, DUMMY_SUBSCRIPTION_ID } from "../constants";
8-
8+
import CoreModule from "../../../src/core/CoreModule";
9+
import { CoreModuleDirector } from "../../../src/core/CoreModuleDirector";
910

1011
export function generateNewSubscription(modelId = '0000000000') {
1112
return new OSModel<SupportedSubscription>(
@@ -40,6 +41,13 @@ export function getDummyPushSubscriptionOSModel(): OSModel<SupportedSubscription
4041
}, DUMMY_MODEL_ID);
4142
}
4243

44+
// Requirement: Test must also call TestEnvironment.initialize();
45+
export async function getCoreModuleDirector(): Promise<CoreModuleDirector> {
46+
const coreModule = new CoreModule();
47+
await coreModule.init();
48+
return new CoreModuleDirector(coreModule);
49+
}
50+
4351
export const passIfBroadcastNTimes = (target: number, broadcastCount: number, pass: () => void) => {
4452
if (broadcastCount === target) {
4553
pass();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { CoreModuleDirector } from "../../../src/core/CoreModuleDirector";
2+
import { OSModel } from "../../../src/core/modelRepo/OSModel";
3+
import { SupportedSubscription } from "../../../src/core/models/SubscriptionModels";
4+
import { TestEnvironment } from "../../support/environment/TestEnvironment";
5+
import { getCoreModuleDirector, getDummyPushSubscriptionOSModel } from "../../support/helpers/core";
6+
7+
describe('CoreModuleDirector tests', () => {
8+
beforeEach(() => {
9+
TestEnvironment.initialize();
10+
});
11+
describe('getPushSubscriptionModel', () => {
12+
beforeEach(() => {
13+
jest.resetAllMocks();
14+
});
15+
16+
async function getPushSubscriptionModel(): Promise<OSModel<SupportedSubscription> | undefined> {
17+
return (await getCoreModuleDirector()).getPushSubscriptionModel();
18+
}
19+
20+
test('returns undefined when it find no push records', async () => {
21+
expect(await getPushSubscriptionModel()).toBe(undefined);
22+
});
23+
24+
test('returns current subscription when available', async () => {
25+
const pushModelCurrent = getDummyPushSubscriptionOSModel();
26+
test.stub(CoreModuleDirector.prototype, "getPushSubscriptionModelByCurrentToken", Promise.resolve(pushModelCurrent));
27+
expect(await getPushSubscriptionModel()).toBe(pushModelCurrent);
28+
});
29+
30+
test('returns last known subscription when current is unavailable', async () => {
31+
const pushModelLastKnown = getDummyPushSubscriptionOSModel();
32+
test.stub(CoreModuleDirector.prototype, "getPushSubscriptionModelByLastKnownToken", Promise.resolve(pushModelLastKnown));
33+
expect(await getPushSubscriptionModel()).toBe(pushModelLastKnown);
34+
});
35+
36+
test('returns current subscription over last known', async () => {
37+
const pushModelCurrent = getDummyPushSubscriptionOSModel();
38+
test.stub(CoreModuleDirector.prototype, "getPushSubscriptionModelByCurrentToken", Promise.resolve(pushModelCurrent));
39+
40+
const pushModelLastKnown = getDummyPushSubscriptionOSModel();
41+
test.stub(CoreModuleDirector.prototype, "getPushSubscriptionModelByLastKnownToken", Promise.resolve(pushModelLastKnown));
42+
43+
expect(await getPushSubscriptionModel()).toBe(pushModelCurrent);
44+
});
45+
});
46+
});

src/shared/managers/SubscriptionManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ export class SubscriptionManager {
163163
}
164164

165165
private async _updatePushSubscriptionModelWithRawSubscription(rawPushSubscription: RawPushSubscription) {
166-
// This undefined when it shouldn't be
167166
const pushModel = await OneSignal.coreDirector.getPushSubscriptionModel();
168167

169168
if (!pushModel) {

0 commit comments

Comments
 (0)