Skip to content

Commit 441da11

Browse files
authored
fix: registerForPush with default options (#134)
* chore: improve types, remove unused method * fix: registerForWebPush() without arguments throws error
1 parent b41da8c commit 441da11

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/Leanplum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default class Leanplum {
3737
Leanplum._lp.setApiPath(apiPath)
3838
}
3939

40-
static setEmail(email: string): void {
41-
Leanplum._lp.setEmail(email)
40+
static setEmail(): void {
41+
// deprecated
4242
}
4343

4444
/**

src/LeanplumInternal.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export default class LeanplumInternal {
6565
this.getFileUrl.bind(this),
6666
)
6767

68-
private _email: string
6968
private _deviceName: string
7069
private _deviceModel: string
7170
private _systemName: string
@@ -74,7 +73,7 @@ export default class LeanplumInternal {
7473

7574
constructor(private wnd: Window) {
7675
this._browserDetector = new BrowserDetector(wnd)
77-
this._events.on('navigationChange', (url) => {
76+
this._events.on('navigationChange', (url: string) => {
7877
let prevented = false
7978
this._events.emit('openUrl', {
8079
preventDefault: () => prevented = true,
@@ -95,10 +94,6 @@ export default class LeanplumInternal {
9594
this._lpRequest.apiPath = apiPath
9695
}
9796

98-
setEmail(email: string): void {
99-
this._email = email
100-
}
101-
10297
/**
10398
* Sets the network timeout.
10499
* @param {number} seconds The timeout in seconds.
@@ -234,7 +229,7 @@ export default class LeanplumInternal {
234229
this.createRequest(Constants.METHODS.GET_VARS, args, {
235230
queued: false,
236231
sendNow: true,
237-
response: (response) => {
232+
response: (response: Array<Object>) => {
238233
const getVarsResponse = this._lpRequest.getLastResponse(response)
239234
const isSuccess = this._lpRequest.isResponseSuccess(getVarsResponse)
240235
if (isSuccess) {
@@ -310,7 +305,7 @@ export default class LeanplumInternal {
310305
this.createRequest(Constants.METHODS.START, args, {
311306
queued: true,
312307
sendNow: true,
313-
response: (response) => {
308+
response: (response: Array<Object>) => {
314309
this._internalState.hasStarted = true
315310
const startResponse = this._lpRequest.getLastResponse(response)
316311
const isSuccess = this._lpRequest.isResponseSuccess(startResponse)
@@ -565,14 +560,14 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas
565560
*/
566561
registerForWebPush(serviceWorkerUrl?: string): Promise<boolean> {
567562
if (this._pushManager.isWebPushSupported()) {
568-
const subscribe = (isSubscribed): Promise<boolean> => {
563+
const subscribe = (isSubscribed: boolean): Promise<boolean> => {
569564
if (isSubscribed) {
570565
return Promise.resolve(true)
571566
}
572567
return this._pushManager.subscribeUser()
573568
}
574569

575-
const options = this._webPushOptions
570+
const options = this._webPushOptions || {}
576571
const workerUrl = serviceWorkerUrl || options.serviceWorkerUrl
577572
const scope = options && options.scope ? { scope: options.scope } : null
578573

test/specs/LeanplumInternal.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,16 @@ describe(LeanplumInternal, () => {
481481
expect(pushManagerMock.subscribeUser).toHaveBeenCalledTimes(0)
482482
})
483483

484+
it('uses default worker url when passed no options', async () => {
485+
pushManagerMock.isWebPushSupported.mockReturnValueOnce(true)
486+
pushManagerMock.register.mockImplementationOnce(async(_url, _scope, callback) => callback(true))
487+
488+
const result = await lp.registerForWebPush()
489+
490+
expect(result).toBe(true)
491+
expect(pushManagerMock.register).toHaveBeenCalledTimes(1)
492+
})
493+
484494
it('returns `true` and subscribes user when not subscribed', async() => {
485495
pushManagerMock.isWebPushSupported.mockReturnValueOnce(true)
486496
pushManagerMock.register.mockImplementationOnce(async(url, scope, callback) => callback(false))

0 commit comments

Comments
 (0)