aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit_tests/settings_test.coffee
blob: 9dc25cefbcba01b33eff489a756555ab1a82f6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require "./test_helper.js"
extend global, require "./test_chrome_stubs.js"

extend(global, require "../../lib/utils.js")
Utils.getCurrentVersion = -> '1.44'
Utils.isBackgroundPage = -> true
Utils.isExtensionPage = -> true
global.localStorage = {}
extend(global,require "../../lib/settings.js")
extend(global,require "../../pages/options.js")

context "settings",

  setup ->
    stub global, 'localStorage', {}
    Settings.cache = global.localStorage # Point the settings cache to the new localStorage object.
    Settings.postUpdateHooks = {} # Avoid running update hooks which include calls to outside of settings.

  should "save settings in localStorage as JSONified strings", ->
    Settings.set 'dummy', ""
    assert.equal localStorage.dummy, '""'

  should "obtain defaults if no key is stored", ->
    assert.isFalse Settings.has 'scrollStepSize'
    assert.equal Settings.get('scrollStepSize'), 60

  should "store values", ->
    Settings.set 'scrollStepSize', 20
    assert.equal Settings.get('scrollStepSize'), 20

  should "revert to defaults if no key is stored", ->
    Settings.set 'scrollStepSize', 20
    Settings.clear 'scrollStepSize'
    assert.equal Settings.get('scrollStepSize'), 60

context "synced settings",

  setup ->
    stub global, 'localStorage', {}
    Settings.cache = global.localStorage # Point the settings cache to the new localStorage object.
    Settings.postUpdateHooks = {} # Avoid running update hooks which include calls to outside of settings.

  should "propagate non-default value via synced storage listener", ->
    Settings.set 'scrollStepSize', 20
    assert.equal Settings.get('scrollStepSize'), 20
    Settings.propagateChangesFromChromeStorage { scrollStepSize: { newValue: "40" } }
    assert.equal Settings.get('scrollStepSize'), 40

  should "propagate default value via synced storage listener", ->
    Settings.set 'scrollStepSize', 20
    assert.equal Settings.get('scrollStepSize'), 20
    Settings.propagateChangesFromChromeStorage { scrollStepSize: { newValue: "60" } }
    assert.equal Settings.get('scrollStepSize'), 60

  should "propagate non-default values from synced storage", ->
    chrome.storage.sync.set { scrollStepSize: JSON.stringify(20) }
    assert.equal Settings.get('scrollStepSize'), 20

  should "propagate default values from synced storage", ->
    Settings.set 'scrollStepSize', 20
    chrome.storage.sync.set { scrollStepSize: JSON.stringify(60) }
    assert.equal Settings.get('scrollStepSize'), 60

  should "clear a setting from synced storage", ->
    Settings.set 'scrollStepSize', 20
    chrome.storage.sync.remove 'scrollStepSize'
    assert.equal Settings.get('scrollStepSize'), 60

  should "trigger a postUpdateHook", ->
    message = "Hello World"
    receivedMessage = ""
    Settings.postUpdateHooks['scrollStepSize'] = (value) -> receivedMessage = value
    chrome.storage.sync.set { scrollStepSize: JSON.stringify(message) }
    assert.equal message, receivedMessage

  should "sync a key which is not a known setting (without crashing)", ->
    chrome.storage.sync.set { notASetting: JSON.stringify("notAUsefullValue") }

context "default valuess",

  should "have a default value for every option", ->
    for own key of Options
      assert.isTrue key of Settings.defaults