|
| 1 | +/* eslint no-undef: "off" */ |
| 2 | + |
| 3 | +const assert = require('assert').strict |
| 4 | +const SmartApp = require('../lib/smart-app') |
| 5 | + |
| 6 | +class ContextStore { |
| 7 | + constructor() { |
| 8 | + this.contexts = {} |
| 9 | + } |
| 10 | + |
| 11 | + get(installedAppId) { |
| 12 | + return new Promise(resolve => { |
| 13 | + resolve(this.contexts[installedAppId]) |
| 14 | + }) |
| 15 | + } |
| 16 | + |
| 17 | + put(params) { |
| 18 | + this.contexts[params.installedAppId] = params |
| 19 | + return new Promise(resolve => { |
| 20 | + resolve() |
| 21 | + }) |
| 22 | + } |
| 23 | + |
| 24 | + update(installedAppId, params) { |
| 25 | + this.contexts[params.installedAppId].authToken = params.authToken |
| 26 | + this.contexts[params.installedAppId].refreshToken = params.refreshToken |
| 27 | + return new Promise(resolve => { |
| 28 | + resolve() |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + delete(installedAppId) { |
| 33 | + this.contexts[installedAppId] = null |
| 34 | + return new Promise(resolve => { |
| 35 | + resolve() |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe('smartapp-context-spec', () => { |
| 41 | + let app |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + app = new SmartApp() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should handle context store', async () => { |
| 48 | + const installData = { |
| 49 | + authToken: 'xxx', |
| 50 | + refreshToken: 'yyy', |
| 51 | + installedApp: { |
| 52 | + installedAppId: 'd692699d-e7a6-400d-a0b7-d5be96e7a564', |
| 53 | + locationId: 'e675a3d9-2499-406c-86dc-8a492a886494', |
| 54 | + config: {} |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + const contextStore = new ContextStore() |
| 59 | + app.contextStore(contextStore) |
| 60 | + |
| 61 | + await app.handleMockCallback({ |
| 62 | + lifecycle: 'INSTALL', |
| 63 | + executionId: 'e6903fe6-f88f-da69-4c12-e2802606ccbc', |
| 64 | + locale: 'en', |
| 65 | + version: '0.1.0', |
| 66 | + client: { |
| 67 | + os: 'ios', |
| 68 | + version: '0.0.0', |
| 69 | + language: 'en-US' |
| 70 | + }, |
| 71 | + installData, |
| 72 | + settings: {} |
| 73 | + }) |
| 74 | + |
| 75 | + const ctx = await app.withContext('d692699d-e7a6-400d-a0b7-d5be96e7a564') |
| 76 | + |
| 77 | + assert.equal(installData.installedApp.installedAppId, ctx.installedAppId) |
| 78 | + assert.equal(installData.installedApp.locationId, ctx.locationId) |
| 79 | + assert.equal(installData.authToken, ctx.api.client.authToken) |
| 80 | + assert.equal(installData.refreshToken, ctx.api.client.refreshToken) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should handle context object', async () => { |
| 84 | + const params = { |
| 85 | + authToken: 'xxx', |
| 86 | + refreshToken: 'yyy', |
| 87 | + installedAppId: 'aaa', |
| 88 | + locationId: 'bbb', |
| 89 | + locale: 'en', |
| 90 | + config: {device: 'ccc'} |
| 91 | + } |
| 92 | + |
| 93 | + const ctx = await app.withContext(params) |
| 94 | + |
| 95 | + assert.equal(params.installedAppId, ctx.installedAppId) |
| 96 | + assert.equal(params.locationId, ctx.locationId) |
| 97 | + assert.equal(params.authToken, ctx.api.client.authToken) |
| 98 | + assert.equal(params.refreshToken, ctx.api.client.refreshToken) |
| 99 | + assert.equal(params.locale, ctx.event.locale) |
| 100 | + }) |
| 101 | +}) |
0 commit comments