|
1 | 1 | import '../../support/polyfills/polyfills'; |
2 | 2 | import test from 'ava'; |
3 | 3 | import { TestEnvironment, HttpHttpsEnvironment } from '../../support/sdk/TestEnvironment'; |
4 | | -import CookieSyncer from '../../../src/modules/CookieSyncer'; |
5 | 4 | import OneSignal from '../../../src/OneSignal'; |
6 | | -import MainHelper from '../../../src/helpers/MainHelper'; |
7 | 5 | import sinon from 'sinon'; |
8 | 6 | import SubscriptionHelper from '../../../src/helpers/SubscriptionHelper'; |
9 | 7 | import { SubscriptionManager } from '../../../src/managers/SubscriptionManager'; |
10 | | -import { AppConfig } from '../../../src/models/AppConfig'; |
11 | | - |
12 | | -import Context from '../../../src/models/Context'; |
13 | 8 | import { SessionManager } from '../../../src/managers/SessionManager'; |
14 | | -import Random from '../../support/tester/Random'; |
15 | 9 |
|
16 | | -test.beforeEach(async t => { |
| 10 | +const sinonSandbox = sinon.sandbox.create(); |
| 11 | + |
| 12 | +test.beforeEach(async () => { |
17 | 13 | await TestEnvironment.initialize({ |
18 | 14 | httpOrHttps: HttpHttpsEnvironment.Https |
19 | 15 | }); |
20 | | - |
21 | | - const appConfig = TestEnvironment.getFakeAppConfig(); |
22 | | - appConfig.appId = Random.getRandomUuid(); |
23 | | - OneSignal.context = new Context(appConfig); |
| 16 | + TestEnvironment.mockInternalOneSignal(); |
24 | 17 | }); |
25 | 18 |
|
| 19 | +test.afterEach(() => { |
| 20 | + sinonSandbox.restore(); |
| 21 | +}) |
| 22 | + |
26 | 23 | test('should not resubscribe user on subsequent page views if the user is already subscribed', async t => { |
27 | | - const isPushEnabledStub = sinon.stub(OneSignal, 'privateIsPushNotificationsEnabled').resolves(true); |
28 | | - const getSessionCountStub = sinon.stub(SessionManager.prototype, 'getPageViewCount').returns(2); |
29 | | - const subscribeSpy = sinon.spy(SubscriptionManager.prototype, 'subscribe'); |
| 24 | + sinonSandbox.stub(OneSignal, 'privateIsPushNotificationsEnabled').resolves(true); |
| 25 | + sinonSandbox.stub(SessionManager.prototype, 'getPageViewCount').returns(2); |
| 26 | + const subscribeSpy = sinonSandbox.spy(SubscriptionManager.prototype, 'subscribe'); |
30 | 27 |
|
31 | 28 | await SubscriptionHelper.registerForPush(); |
32 | | - |
33 | 29 | t.true(subscribeSpy.notCalled); |
34 | | - |
35 | | - subscribeSpy.restore(); |
36 | | - isPushEnabledStub.restore(); |
37 | | - getSessionCountStub.restore(); |
38 | 30 | }); |
39 | 31 |
|
40 | 32 | test('should subscribe user on subsequent page views if the user is not subscribed', async t => { |
41 | | - await TestEnvironment.initialize({ |
42 | | - httpOrHttps: HttpHttpsEnvironment.Https |
43 | | - }); |
44 | | - |
45 | | - const appConfig = TestEnvironment.getFakeAppConfig(); |
46 | | - appConfig.appId = Random.getRandomUuid(); |
47 | | - OneSignal.context = new Context(appConfig); |
48 | | - |
49 | | - const isPushEnabledStub = sinon.stub(OneSignal, 'isPushNotificationsEnabled').resolves(false); |
50 | | - const getSessionCountStub = sinon.stub(SessionManager.prototype, 'getPageViewCount').returns(2); |
51 | | - const subscribeStub = sinon.stub(SubscriptionManager.prototype, 'subscribe').resolves(null); |
52 | | - |
| 33 | + sinonSandbox.stub(OneSignal, 'isPushNotificationsEnabled').resolves(false); |
| 34 | + sinonSandbox.stub(SessionManager.prototype, 'getPageViewCount').returns(2); |
| 35 | + sinonSandbox.stub(SubscriptionManager.prototype, 'registerSubscription').resolves(); |
| 36 | + |
| 37 | + const subscribeStub = sinonSandbox.stub(SubscriptionManager.prototype, 'subscribe').resolves(null); |
53 | 38 | await SubscriptionHelper.registerForPush(); |
54 | | - |
55 | 39 | t.true(subscribeStub.called); |
56 | | - |
57 | | - subscribeStub.restore(); |
58 | | - isPushEnabledStub.restore(); |
59 | | - getSessionCountStub.restore(); |
60 | 40 | }); |
61 | 41 |
|
62 | 42 | test('should resubscribe an already subscribed user on first page view', async t => { |
63 | | - await TestEnvironment.initialize({ |
64 | | - httpOrHttps: HttpHttpsEnvironment.Https |
65 | | - }); |
66 | | - |
67 | | - const appConfig = TestEnvironment.getFakeAppConfig(); |
68 | | - appConfig.appId = Random.getRandomUuid(); |
69 | | - OneSignal.context = new Context(appConfig); |
70 | | - |
71 | | - const isPushEnabledStub = sinon.stub(OneSignal, 'isPushNotificationsEnabled').resolves(true); |
72 | | - const getSessionCountStub = sinon.stub(SessionManager.prototype, 'getPageViewCount').returns(1); |
73 | | - const subscribeStub = sinon.stub(SubscriptionManager.prototype, 'subscribe').resolves(null); |
| 43 | + sinonSandbox.stub(OneSignal, 'isPushNotificationsEnabled').resolves(true); |
| 44 | + sinonSandbox.stub(SessionManager.prototype, 'getPageViewCount').returns(1); |
| 45 | + sinonSandbox.stub(SubscriptionManager.prototype, 'registerSubscription').resolves(); |
74 | 46 |
|
| 47 | + const subscribeStub = sinonSandbox.stub(SubscriptionManager.prototype, 'subscribe').resolves(null); |
75 | 48 | await SubscriptionHelper.registerForPush(); |
76 | 49 |
|
77 | 50 | t.true(subscribeStub.called); |
78 | | - |
79 | | - subscribeStub.restore(); |
80 | | - isPushEnabledStub.restore(); |
81 | | - getSessionCountStub.restore(); |
82 | 51 | }); |
0 commit comments