Skip to content

Commit 043cac9

Browse files
authored
Merge pull request #165 from Backendless/vupirov/push-templates-data
- fix PushWithTemplates API
2 parents 672ffb6 + 41ab434 commit 043cac9

File tree

5 files changed

+86
-13
lines changed

5 files changed

+86
-13
lines changed

backendless.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ declare module Backendless {
526526

527527
function getPushTemplates(deviceType: string): Promise<Object>;
528528

529-
function pushWithTemplate(templateName: string): Promise<Object>;
529+
function pushWithTemplate(templateName: string, templateValues?: object): Promise<Object>;
530530
}
531531

532532
/**
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import Utils from '../../utils'
2+
import Async from '../../request/async'
23

3-
export function pushWithTemplate(templateName) {
4-
const responder = Utils.extractResponder(arguments)
5-
const isAsync = !!responder
4+
export function pushWithTemplate(templateName, templateValues, asyncHandler) {
5+
if (templateValues instanceof Async) {
6+
asyncHandler = templateValues
7+
templateValues = undefined
8+
}
69

710
if (!templateName || !Utils.isString(templateName)) {
811
throw new Error('Push Template Name must be non empty string!')
912
}
1013

14+
const data = {}
15+
16+
if (templateValues) {
17+
data.templateValues = templateValues
18+
}
19+
1120
return this.app.request.post({
12-
url : this.app.urls.messagingPushWithTemplate(templateName),
13-
isAsync : isAsync,
14-
asyncHandler: responder
21+
url : this.app.urls.messagingPushWithTemplate(templateName),
22+
isAsync: !!asyncHandler,
23+
asyncHandler,
24+
data
1525
})
1626
}

test/e2e/helpers/sandbox.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Request from 'backendless-request'
12
import { createClient } from 'backendless-console-sdk'
23
import { TablesAPI } from './tables'
34
import { wait } from './promise'
@@ -95,10 +96,50 @@ const waitUntilAppIsConfigured = async app => {
9596
app.ready = true
9697
}
9798

99+
let mockRequests = []
100+
const nativeRequestSend = Request.send
101+
102+
Request.send = function fakeRequestSend(path, method, headers, body, encoding) {
103+
const mockRequest = mockRequests.shift()
104+
105+
if (!mockRequest) {
106+
return nativeRequestSend.call(Request, path, method, headers, body, encoding)
107+
}
108+
109+
Object.assign(mockRequest, { path, method, headers, body, encoding })
110+
111+
try {
112+
mockRequest.body = JSON.parse(body)
113+
} catch (e) {
114+
//
115+
}
116+
117+
const responseFn = mockRequest.responseFn || Promise.resolve()
118+
119+
return responseFn
120+
.then(response => {
121+
return Object.assign({ status: 200, body: undefined, headers: {} }, response)
122+
})
123+
}
124+
125+
function prepareMockRequest(responseFn) {
126+
const mockRequest = {
127+
responseFn
128+
}
129+
130+
mockRequests.push(mockRequest)
131+
132+
return mockRequest
133+
}
134+
98135
const createSandboxFor = each => () => {
99136
const beforeHook = each ? beforeEach : before
100137
const afterHook = each ? afterEach : after
101138

139+
beforeEach(() => {
140+
mockRequests = []
141+
})
142+
102143
beforeHook(function() {
103144
this.timeout(120000)
104145
this.consoleApi = createClient(CONSOLE_SERVER)
@@ -130,6 +171,7 @@ const createSandboxFor = each => () => {
130171

131172
export default {
132173
Backendless,
174+
prepareMockRequest,
133175
forTest : createSandboxFor(true),
134176
forSuite: createSandboxFor(false)
135177
}

test/e2e/specs/messaging.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ describe('Messaging', function() {
2929
expect(error.message).to.equal('Email Template with name \'nonexistent-template\' doesn\'t exist.')
3030
})
3131

32+
describe('Push Templates', function() {
33+
34+
const templateName = 'MY_PUSH_TEMPLATE'
35+
36+
it('send push with template', async () => {
37+
const req1 = sandbox.prepareMockRequest()
38+
const req2 = sandbox.prepareMockRequest()
39+
40+
await Backendless.Messaging.pushWithTemplate(templateName)
41+
await Backendless.Messaging.pushWithTemplate(templateName, { customValue: 'customValue' })
42+
43+
expect(req1.path).to.equal(`${Backendless.appPath}/messaging/push/${templateName}`)
44+
expect(req1.method).to.equal('POST')
45+
expect(Object.keys(req1.body)).to.eql([])
46+
47+
expect(req2.path).to.equal(`${Backendless.appPath}/messaging/push/${templateName}`)
48+
expect(req2.method).to.equal('POST')
49+
expect(Object.keys(req2.body)).to.eql(['templateValues'])
50+
expect(req2.body.templateValues.customValue).to.eql('customValue')
51+
})
52+
})
3253
})

test/tsd.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function testMain() {
1111
const browser: { browser: string, version: string } = Backendless.browser;
1212

1313
Backendless.initApp('APPLICATION_ID', 'JS_SECRET_KEY');
14-
Backendless.initApp({ appId: 'APPLICATION_ID', apiKey: 'JS_SECRET_KEY'});
14+
Backendless.initApp({appId: 'APPLICATION_ID', apiKey: 'JS_SECRET_KEY'});
1515
}
1616

1717
function testLocalCache() {
@@ -75,13 +75,13 @@ function testDataQueryBuilderClass() {
7575
str = dataQuery.getWhereClause();
7676

7777
dataQuery = dataQuery.setProperties('abc');
78-
dataQuery = dataQuery.setProperties(['abc','abc','abc']);
78+
dataQuery = dataQuery.setProperties(['abc', 'abc', 'abc']);
7979
dataQuery = dataQuery.addProperty(str);
8080
dataQuery = dataQuery.addProperties(str, str, str, str);
81-
dataQuery = dataQuery.addProperties(['abc','abc','abc'], ['abc','abc','abc'], ['abc','abc','abc']);
82-
dataQuery = dataQuery.addProperties(['abc','abc','abc'], str, str);
81+
dataQuery = dataQuery.addProperties(['abc', 'abc', 'abc'], ['abc', 'abc', 'abc'], ['abc', 'abc', 'abc']);
82+
dataQuery = dataQuery.addProperties(['abc', 'abc', 'abc'], str, str);
8383
dataQuery = dataQuery.addProperties(str);
84-
dataQuery = dataQuery.addProperties(['abc','abc','abc']);
84+
dataQuery = dataQuery.addProperties(['abc', 'abc', 'abc']);
8585

8686
strs = dataQuery.getProperties();
8787

@@ -790,7 +790,7 @@ function testMessaging() {
790790
promiseObject = Backendless.Messaging.getPushTemplates('ios');
791791

792792
promiseObject = Backendless.Messaging.pushWithTemplate('templateName');
793-
793+
promiseObject = Backendless.Messaging.pushWithTemplate('templateName', {foo: 'bar'});
794794
}
795795

796796
function testFilesService() {

0 commit comments

Comments
 (0)