Skip to content

Commit 7fab0d4

Browse files
author
Christopher Goddard
committed
Test updateUserAgentWithAppId more thoroughly + break early if appId undefined
1 parent 23e6df8 commit 7fab0d4

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

lib/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import pkgJson from '../package.json'
44
const Promise = window.Promise || NativePromise
55

66
export function updateUserAgentWithAppId (headers, appId) {
7+
if (!appId) {
8+
return headers
9+
}
710
const originalUserAgent = headers && headers['User-Agent'] ? headers['User-Agent'] : ''
811
const zafSdkUserAgentString = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}`
912
const userAgent = originalUserAgent ? `${originalUserAgent} ${zafSdkUserAgentString}` : zafSdkUserAgentString

spec/utils_spec.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,51 @@ describe('Utils', () => {
159159
global.pkgJson = { version: '1.0.0' }
160160
})
161161

162-
it('should append the app id to the user agent', () => {
163-
const headers = { 'User-Agent': 'Mozilla/5.0' }
164-
const appId = '1'
165-
const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId)
166-
const expectedUserAgent = `Mozilla/5.0 zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}`
167-
expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent)
162+
describe('with a valid app id', () => {
163+
let appId
164+
165+
beforeEach(() => {
166+
appId = '1'
167+
})
168+
169+
it('should append the app id to the user agent', () => {
170+
const headers = { 'User-Agent': 'Mozilla/5.0' }
171+
const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId)
172+
const expectedUserAgent = `Mozilla/5.0 zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}`
173+
expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent)
174+
})
175+
176+
it('should handle an undefined headers object', () => {
177+
const updatedHeaders = Utils.updateUserAgentWithAppId(undefined, appId)
178+
const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}`
179+
expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent)
180+
})
181+
182+
it('should handle a headers object with no User-Agent', () => {
183+
const headers = {}
184+
const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId)
185+
const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}`
186+
expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent)
187+
})
188+
})
189+
190+
describe('with an invalid app id', () => {
191+
it('should not append the app id to the user agent', () => {
192+
const headers = { 'User-Agent': 'Mozilla/5.0' }
193+
const updatedHeaders = Utils.updateUserAgentWithAppId(headers)
194+
expect(updatedHeaders['User-Agent']).to.equal('Mozilla/5.0')
195+
})
196+
197+
it('should handle an undefined headers object', () => {
198+
const updatedHeaders = Utils.updateUserAgentWithAppId()
199+
expect(updatedHeaders).to.be.undefined()
200+
})
201+
202+
it('should handle a headers object with no User-Agent', () => {
203+
const headers = {}
204+
const updatedHeaders = Utils.updateUserAgentWithAppId(headers)
205+
expect(updatedHeaders['User-Agent']).to.equal(undefined)
206+
})
168207
})
169208
})
170209
})

0 commit comments

Comments
 (0)