Browse Source

Fixes #4124 - OS detection crashes if OS name cannot be parsed

Mantas 2 years ago
parent
commit
134b419fd7

+ 16 - 9
app/assets/javascripts/app/lib/app_post/browser.coffee

@@ -83,18 +83,25 @@ class App.Browser
     fingerprint
 
   @magicKey: ->
-    browser = @detection()
-    magicKey = 'ctrl'
-    if browser && browser.os && browser.os.name.toString().match(/mac/i)
-      magicKey = 'cmd'
-    magicKey
+    if @isMac()
+      'cmd'
+    else
+      'ctrl'
 
   @hotkeys: ->
+    if @isMac()
+      'alt+ctrl'
+    else
+      'ctrl+shift'
+
+  @isMac: ->
     browser = @detection()
-    hotkeys = 'ctrl+shift'
-    if browser && browser.os && browser.os.name.toString().match(/mac/i)
-      hotkeys = 'alt+ctrl'
-    hotkeys
+
+    osName = browser?.os?.name?.toString()
+
+    return if !osName
+
+    osName.match(/mac/i)
 
 class Modal extends App.ControllerModal
   buttonClose: false

+ 67 - 0
public/assets/tests/qunit/browser.js

@@ -0,0 +1,67 @@
+QUnit.test('App.Browser .magicKey', assert => {
+  let stub = sinon.stub(App.Browser, 'isMac')
+
+  stub.returns(true)
+  assert.equal(App.Browser.magicKey(), 'cmd')
+
+  stub.returns(false)
+  assert.equal(App.Browser.magicKey(), 'ctrl')
+
+  stub.restore()
+})
+
+QUnit.test('App.Browser .hotkeys', assert => {
+  let stub = sinon.stub(App.Browser, 'isMac')
+
+  stub.returns(true)
+  assert.equal(App.Browser.hotkeys(), 'alt+ctrl')
+
+  stub.returns(false)
+  assert.equal(App.Browser.hotkeys(), 'ctrl+shift')
+
+  stub.restore()
+})
+
+QUnit.test('App.Browser .isMac', assert => {
+  let stub = sinon.stub(App.Browser, 'detection')
+  stub.returns({
+    browser: {
+      major: "48",
+      name: "Chrome",
+      version: "48.0.2564.109",
+    },
+    os: {
+      name: "Mac OS",
+      version: "10.11.3",
+    }
+  })
+
+  assert.ok(App.Browser.isMac())
+
+  stub.returns({
+    browser: {
+      major: "48",
+      name: "Chrome",
+      version: "48.0.2564.109",
+    },
+    os: {
+      name: "Debian McDebian",
+      version: "14.10",
+    }
+  })
+
+  assert.notOk(App.Browser.isMac())
+
+  stub.returns({
+    browser: {
+      major: "48",
+      name: "Chrome",
+      version: "48.0.2564.109",
+    },
+    os: {}
+  })
+
+  assert.notOk(App.Browser.isMac())
+
+  stub.restore()
+})