Skip to content

Commit 1e34dab

Browse files
authored
test: add missing cases for unhandled events (#184)
1 parent 73f9ea0 commit 1e34dab

File tree

4 files changed

+84
-41
lines changed

4 files changed

+84
-41
lines changed

config/jest.setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint no-undef: 'off' */
2+
3+
process.on('unhandledRejection', error => {
4+
fail(error)
5+
})

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
// https://github.com/SmartThingsCommunity/smartapp-sdk-nodejs/blob/79983de15646dd3be84d2b3e82d409f9bc632959/package.json#L72
66
coverageReporters: ['json', 'text'],
77
testEnvironment: 'node',
8-
testMatch: ['**/test/**/*.[jt]s?(x)']
8+
testMatch: ['**/test/**/*.[jt]s?(x)'],
9+
setupFiles: ['<rootDir>/config/jest.setup.js']
910
}

test/unit/smartapp-page-spec.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
const assert = require('assert').strict
21
const SmartApp = require('../../lib/smart-app')
32

43
describe('smartapp-page-spec', () => {
4+
let app
5+
beforeEach(() => {
6+
app = new SmartApp()
7+
})
8+
59
it('should set page ID', () => {
6-
const app = new SmartApp()
710
app.appId('xxx')
811
app.page('eaMainPage', (ctx, page) => {
912
page.section('whenDoorOpensAndCloses', section => {
@@ -56,7 +59,7 @@ describe('smartapp-page-spec', () => {
5659
disableRemoveApp: false
5760
}}
5861

59-
assert.deepStrictEqual(initResponse.configurationData, expectedInitResponse)
62+
expect(initResponse.configurationData).toStrictEqual(expectedInitResponse)
6063
})
6164

6265
// Page configuration callback
@@ -109,7 +112,8 @@ describe('smartapp-page-spec', () => {
109112
]
110113
}
111114
}
112-
assert.deepStrictEqual(pageResponse.configurationData, expectedPageResponse)
115+
116+
expect(pageResponse.configurationData).toStrictEqual(expectedPageResponse)
113117
})
114118

115119
// Default page handler configuration callback
@@ -182,12 +186,12 @@ describe('smartapp-page-spec', () => {
182186
]
183187
}
184188
}
185-
assert.deepStrictEqual(pageResponse.configurationData, expectedPageResponse)
189+
190+
expect(pageResponse.configurationData).toStrictEqual(expectedPageResponse)
186191
})
187192
})
188193

189194
it('should configure event logger', () => {
190-
const app = new SmartApp()
191195
app.appId('xxx')
192196
app.enableEventLogging(4)
193197
app.page('eaMainPage', (ctx, page) => {
@@ -218,9 +222,7 @@ describe('smartapp-page-spec', () => {
218222
})
219223
})
220224

221-
it('default page handler', () => {
222-
const app = new SmartApp()
223-
225+
test('default page handler', () => {
224226
app.handleMockCallback({
225227
lifecycle: 'CONFIGURATION',
226228
executionId: 'abcf6e72-60f4-1f27-341b-449ad9e2192e',
@@ -263,13 +265,12 @@ describe('smartapp-page-spec', () => {
263265
]
264266
}
265267
}
266-
assert.deepStrictEqual(pageResponse.configurationData, expectedPageResponse)
268+
269+
expect(pageResponse.configurationData).toStrictEqual(expectedPageResponse)
267270
})
268271
})
269272

270-
it('default page handler without pageId', () => {
271-
const app = new SmartApp()
272-
273+
test('default page handler without pageId', () => {
273274
app.handleMockCallback({
274275
lifecycle: 'CONFIGURATION',
275276
executionId: 'abcf6e72-60f4-1f27-341b-449ad9e2192e',
@@ -312,7 +313,37 @@ describe('smartapp-page-spec', () => {
312313
]
313314
}
314315
}
315-
assert.deepStrictEqual(pageResponse.configurationData, expectedPageResponse)
316+
317+
expect(pageResponse.configurationData).toStrictEqual(expectedPageResponse)
316318
})
317319
})
320+
321+
it('should respond with error when config phase is not supported', async () => {
322+
const unsupportedConfigPhase = {
323+
lifecycle: 'CONFIGURATION',
324+
executionId: '00000000-0000-0000-0000-000000000000',
325+
locale: 'en',
326+
version: '0.1.0',
327+
client: {
328+
os: 'ios',
329+
version: '0.0.0',
330+
language: 'fr'
331+
},
332+
configurationData: {
333+
installedAppId: '00000000-0000-0000-0000-000000000000',
334+
phase: 'UNSUPPORTED',
335+
pageId: '',
336+
previousPageId: '',
337+
config: {}
338+
},
339+
settings: {}
340+
}
341+
342+
const expectedPageResponse = {
343+
message: 'Server error: \'Error: Unsupported config phase: UNSUPPORTED\'',
344+
statusCode: 500
345+
}
346+
347+
await expect(app.handleMockCallback(unsupportedConfigPhase)).resolves.toStrictEqual(expectedPageResponse)
348+
})
318349
})

test/unit/smartapp-spec.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require('assert').strict
1+
const Log = require('../../lib/util/log')
22
const SmartApp = require('../../lib/smart-app')
33

44
describe('smartapp-spec', () => {
@@ -19,11 +19,11 @@ describe('smartapp-spec', () => {
1919
}
2020
})
2121

22-
it('should handle INSTALL event', () => {
22+
it('should handle INSTALL event', async () => {
2323
app.installed((_, installData) => {
2424
receivedData = installData
2525
})
26-
app.handleMockCallback({
26+
await app.handleMockCallback({
2727
lifecycle: 'INSTALL',
2828
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
2929
locale: 'en',
@@ -36,34 +36,15 @@ describe('smartapp-spec', () => {
3636
installData: expectedData,
3737
settings: {}
3838
})
39-
assert.strictEqual(receivedData, expectedData)
40-
})
4139

42-
it('should handle UNINSTALL event', () => {
43-
app.uninstalled((_, uninstallData) => {
44-
receivedData = uninstallData
45-
})
46-
app.handleMockCallback({
47-
lifecycle: 'UNINSTALL',
48-
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
49-
locale: 'en',
50-
version: '0.1.0',
51-
client: {
52-
os: 'ios',
53-
version: '0.0.0',
54-
language: 'en-US'
55-
},
56-
uninstallData: expectedData,
57-
settings: {}
58-
})
59-
assert.strictEqual(receivedData, expectedData)
40+
expect(receivedData).toStrictEqual(expectedData)
6041
})
6142

62-
it('should handle UPDATE event', () => {
43+
it('should handle UPDATE event', async () => {
6344
app.updated((_, updateData) => {
6445
receivedData = updateData
6546
})
66-
app.handleMockCallback({
47+
await app.handleMockCallback({
6748
lifecycle: 'UPDATE',
6849
executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc',
6950
locale: 'en',
@@ -76,6 +57,31 @@ describe('smartapp-spec', () => {
7657
updateData: expectedData,
7758
settings: {}
7859
})
79-
assert.strictEqual(receivedData, expectedData)
60+
61+
expect(receivedData).toStrictEqual(expectedData)
62+
})
63+
64+
it('should warn when event type is unhandled', async () => {
65+
const logSpy = jest.spyOn(Log.prototype, 'warn')
66+
67+
const unhandledEvent = {
68+
lifecycle: 'EVENT',
69+
eventData: {
70+
installedApp: {
71+
installedAppId: '00000000-0000-0000-0000-000000000000'
72+
},
73+
events: [
74+
{
75+
eventType: 'UNHANDLED_EVENT'
76+
}
77+
]
78+
}
79+
}
80+
81+
await expect(app.handleMockCallback(unhandledEvent)).resolves.not.toThrow()
82+
expect(logSpy).toBeCalledTimes(1)
83+
expect(logSpy).toBeCalledWith('Unhandled event of type UNHANDLED_EVENT')
84+
85+
logSpy.mockClear()
8086
})
8187
})

0 commit comments

Comments
 (0)