From 20fb648882824bdaa25baf9c462c3870c1ce685f Mon Sep 17 00:00:00 2001 From: Alex Gyoshev Date: Sat, 8 May 2021 08:43:59 +0300 Subject: [PATCH 1/2] chore: improve types, remove unused method --- src/Leanplum.ts | 4 ++-- src/LeanplumInternal.ts | 13 ++++--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Leanplum.ts b/src/Leanplum.ts index e53a0a1f..3969e4af 100644 --- a/src/Leanplum.ts +++ b/src/Leanplum.ts @@ -37,8 +37,8 @@ export default class Leanplum { Leanplum._lp.setApiPath(apiPath) } - static setEmail(email: string): void { - Leanplum._lp.setEmail(email) + static setEmail(): void { + // deprecated } /** diff --git a/src/LeanplumInternal.ts b/src/LeanplumInternal.ts index cde98b77..9a5942e2 100644 --- a/src/LeanplumInternal.ts +++ b/src/LeanplumInternal.ts @@ -65,7 +65,6 @@ export default class LeanplumInternal { this.getFileUrl.bind(this), ) - private _email: string private _deviceName: string private _deviceModel: string private _systemName: string @@ -74,7 +73,7 @@ export default class LeanplumInternal { constructor(private wnd: Window) { this._browserDetector = new BrowserDetector(wnd) - this._events.on('navigationChange', (url) => { + this._events.on('navigationChange', (url: string) => { let prevented = false this._events.emit('openUrl', { preventDefault: () => prevented = true, @@ -95,10 +94,6 @@ export default class LeanplumInternal { this._lpRequest.apiPath = apiPath } - setEmail(email: string): void { - this._email = email - } - /** * Sets the network timeout. * @param {number} seconds The timeout in seconds. @@ -234,7 +229,7 @@ export default class LeanplumInternal { this.createRequest(Constants.METHODS.GET_VARS, args, { queued: false, sendNow: true, - response: (response) => { + response: (response: Array) => { const getVarsResponse = this._lpRequest.getLastResponse(response) const isSuccess = this._lpRequest.isResponseSuccess(getVarsResponse) if (isSuccess) { @@ -310,7 +305,7 @@ export default class LeanplumInternal { this.createRequest(Constants.METHODS.START, args, { queued: true, sendNow: true, - response: (response) => { + response: (response: Array) => { this._internalState.hasStarted = true const startResponse = this._lpRequest.getLastResponse(response) const isSuccess = this._lpRequest.isResponseSuccess(startResponse) @@ -565,7 +560,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas */ registerForWebPush(serviceWorkerUrl?: string): Promise { if (this._pushManager.isWebPushSupported()) { - const subscribe = (isSubscribed): Promise => { + const subscribe = (isSubscribed: boolean): Promise => { if (isSubscribed) { return Promise.resolve(true) } From 7c60aa22c8d64ce407ecfe2e95cae781b71c6f33 Mon Sep 17 00:00:00 2001 From: Alex Gyoshev Date: Sat, 8 May 2021 08:54:43 +0300 Subject: [PATCH 2/2] fix: registerForWebPush() without arguments throws error --- src/LeanplumInternal.ts | 2 +- test/specs/LeanplumInternal.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/LeanplumInternal.ts b/src/LeanplumInternal.ts index 9a5942e2..2ab54ab8 100644 --- a/src/LeanplumInternal.ts +++ b/src/LeanplumInternal.ts @@ -567,7 +567,7 @@ Use "npm update leanplum-sdk" or go to https://docs.leanplum.com/reference#javas return this._pushManager.subscribeUser() } - const options = this._webPushOptions + const options = this._webPushOptions || {} const workerUrl = serviceWorkerUrl || options.serviceWorkerUrl const scope = options && options.scope ? { scope: options.scope } : null diff --git a/test/specs/LeanplumInternal.test.ts b/test/specs/LeanplumInternal.test.ts index e1dbd4de..3bd30603 100644 --- a/test/specs/LeanplumInternal.test.ts +++ b/test/specs/LeanplumInternal.test.ts @@ -481,6 +481,16 @@ describe(LeanplumInternal, () => { expect(pushManagerMock.subscribeUser).toHaveBeenCalledTimes(0) }) + it('uses default worker url when passed no options', async () => { + pushManagerMock.isWebPushSupported.mockReturnValueOnce(true) + pushManagerMock.register.mockImplementationOnce(async(_url, _scope, callback) => callback(true)) + + const result = await lp.registerForWebPush() + + expect(result).toBe(true) + expect(pushManagerMock.register).toHaveBeenCalledTimes(1) + }) + it('returns `true` and subscribes user when not subscribed', async() => { pushManagerMock.isWebPushSupported.mockReturnValueOnce(true) pushManagerMock.register.mockImplementationOnce(async(url, scope, callback) => callback(false))