|
1 |
| -const SmartApp = require('../../../lib/smart-app') |
| 1 | +const { SmartApp } = require('../../../lib/smart-app') |
| 2 | +const { INSTALL, UNINSTALL, UPDATE } = require('../../data/lifecycles') |
2 | 3 |
|
3 | 4 | describe('installation-lifecycle-spec', () => {
|
4 | 5 | let app
|
5 |
| - let expectedData |
6 | 6 | let receivedEvent
|
7 | 7 | let receivedData
|
8 | 8 |
|
9 | 9 | beforeEach(() => {
|
10 |
| - app = new SmartApp({logUnhandledRejections: false}) |
11 |
| - expectedData = { |
12 |
| - authToken: 'string', |
13 |
| - refreshToken: 'string', |
14 |
| - installedApp: { |
15 |
| - installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564', |
16 |
| - locationId: 'e675a3d9-2499-406c-86dc-8a492a886494', |
17 |
| - config: {} |
18 |
| - } |
19 |
| - } |
| 10 | + app = new SmartApp({ logUnhandledRejections: false }) |
| 11 | + receivedEvent = undefined |
| 12 | + receivedData = undefined |
20 | 13 | })
|
21 | 14 |
|
22 |
| - it('should handle INSTALL lifecycle', async () => { |
| 15 | + it('handles INSTALL lifecycle with installed handler', async () => { |
23 | 16 | app.installed((_, installData) => {
|
24 | 17 | receivedData = installData
|
25 | 18 | })
|
26 |
| - await app.handleMockCallback({ |
27 |
| - lifecycle: 'INSTALL', |
28 |
| - executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc', |
29 |
| - locale: 'en', |
30 |
| - version: '0.1.0', |
31 |
| - client: { |
32 |
| - os: 'ios', |
33 |
| - version: '0.0.0', |
34 |
| - language: 'en-US' |
35 |
| - }, |
36 |
| - installData: expectedData, |
37 |
| - settings: {} |
| 19 | + |
| 20 | + await app.handleMockCallback(INSTALL) |
| 21 | + |
| 22 | + expect(receivedData).toStrictEqual(INSTALL.installData) |
| 23 | + }) |
| 24 | + |
| 25 | + it('handles INSTALL lifecycle with updated handler when no installed handler defined', async () => { |
| 26 | + app.updated((_, installData) => { |
| 27 | + receivedData = installData |
38 | 28 | })
|
39 | 29 |
|
40 |
| - expect(receivedData).toStrictEqual(expectedData) |
| 30 | + await app.handleMockCallback(INSTALL) |
| 31 | + |
| 32 | + expect(receivedData).toStrictEqual(INSTALL.installData) |
41 | 33 | })
|
42 | 34 |
|
43 |
| - it('should handle UNINSTALL lifecycle', async () => { |
44 |
| - const expectedEvent = { |
45 |
| - 'installedApp': { |
46 |
| - 'installedAppId': 'd46a1c60-b6bd-4f82-b124-028e0f14a4f4', |
47 |
| - 'locationId': 'e1e66eab-1eab-4f09-9bb6-91da6585576d', |
48 |
| - 'config': {}, |
49 |
| - 'permissions': [] |
50 |
| - } |
51 |
| - } |
| 35 | + it('handles INSTALL lifecycle rejection with async updated handler when no installed handler defined', async () => { |
| 36 | + app.updated(async () => { |
| 37 | + await Promise.reject(new Error('something failed')) |
| 38 | + }) |
| 39 | + |
| 40 | + const response = app.handleMockCallback(INSTALL) |
| 41 | + |
| 42 | + await expect(response).resolves.toStrictEqual( |
| 43 | + expect.objectContaining({ statusCode: 500, message: expect.stringContaining('something failed') }) |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it('handles INSTALL lifecycle with installed handler when both installed and updated handlers defined', async () => { |
| 48 | + app.installed((_, installData) => { |
| 49 | + receivedData = installData |
| 50 | + }) |
| 51 | + |
| 52 | + const updateHandler = jest.fn(() => { |
| 53 | + receivedData = null |
| 54 | + }) |
| 55 | + |
| 56 | + app.updated(updateHandler) |
| 57 | + |
| 58 | + await app.handleMockCallback(INSTALL) |
| 59 | + |
| 60 | + expect(updateHandler).not.toBeCalled() |
| 61 | + expect(receivedData).toStrictEqual(INSTALL.installData) |
| 62 | + }) |
| 63 | + |
| 64 | + it('handles UNINSTALL lifecycle', async () => { |
52 | 65 | app.uninstalled((_, event) => {
|
53 | 66 | receivedEvent = event
|
54 | 67 | })
|
55 |
| - await app.handleMockCallback({ |
56 |
| - 'lifecycle': 'UNINSTALL', |
57 |
| - 'executionId': 'e139dc1f-ee24-3c1a-309e-48669006817f', |
58 |
| - 'locale': 'en-US', |
59 |
| - 'version': '0.1.0', |
60 |
| - 'uninstallData': { |
61 |
| - 'installedApp': { |
62 |
| - 'installedAppId': 'd46a1c60-b6bd-4f82-b124-028e0f14a4f4', |
63 |
| - 'locationId': 'e1e66eab-1eab-4f09-9bb6-91da6585576d', |
64 |
| - 'config': {}, |
65 |
| - 'permissions': [] |
66 |
| - } |
67 |
| - }, |
68 |
| - 'settings': {} |
69 |
| - }) |
70 | 68 |
|
71 |
| - expect(receivedEvent).toStrictEqual(expectedEvent) |
| 69 | + await app.handleMockCallback(UNINSTALL) |
| 70 | + |
| 71 | + expect(receivedEvent).toStrictEqual(UNINSTALL.uninstallData) |
72 | 72 | })
|
73 | 73 |
|
74 |
| - it('should handle UPDATE lifecycle', async () => { |
| 74 | + it('handles UPDATE lifecycle', async () => { |
75 | 75 | app.updated((_, updateData) => {
|
76 | 76 | receivedData = updateData
|
77 | 77 | })
|
78 |
| - await app.handleMockCallback({ |
79 |
| - lifecycle: 'UPDATE', |
80 |
| - executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc', |
81 |
| - locale: 'en', |
82 |
| - version: '0.1.0', |
83 |
| - client: { |
84 |
| - os: 'ios', |
85 |
| - version: '0.0.0', |
86 |
| - language: 'en-US' |
87 |
| - }, |
88 |
| - updateData: expectedData, |
89 |
| - settings: {} |
90 |
| - }) |
91 | 78 |
|
92 |
| - expect(receivedData).toStrictEqual(expectedData) |
| 79 | + await app.handleMockCallback(UPDATE) |
| 80 | + |
| 81 | + expect(receivedData).toStrictEqual(UPDATE.updateData) |
93 | 82 | })
|
94 | 83 | })
|
0 commit comments