aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit_tests/settings_test.coffee
blob: 346c98dafc18ea1149edcf76534ef5fac401ddf8 (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'
global.localStorage = {}
extend(global,require "../../background_scripts/sync.js")
extend(global,require "../../background_scripts/settings.js")
Sync.init()

context "settings",

  setup ->
    stub global, 'localStorage', {}

  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 "not store values equal to the default", ->
    Settings.set 'scrollStepSize', 20
    assert.isTrue Settings.has 'scrollStepSize'
    Settings.set 'scrollStepSize', 60
    assert.isFalse Settings.has 'scrollStepSize'

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

  should "propagate non-default value via synced storage listener", ->
    Settings.set 'scrollStepSize', 20
    assert.equal Settings.get('scrollStepSize'), 20
    Sync.handleStorageUpdate { 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
    Sync.handleStorageUpdate { scrollStepSize: { newValue: "60" } }
    assert.isFalse Settings.has 'scrollStepSize'

  should "propagate non-default values from synced storage", ->
    chrome.storage.sync.set { scrollStepSize: JSON.stringify(20) }
    Sync.fetchAsync()
    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) }
    Sync.fetchAsync()
    assert.isFalse Settings.has 'scrollStepSize'

  should "clear a setting from synced storage", ->
    Settings.set 'scrollStepSize', 20
    chrome.storage.sync.remove 'scrollStepSize'
    assert.isFalse Settings.has 'scrollStepSize'

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

  should "set search engines, retrieve them correctly and check that they have been parsed correctly", ->
    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s baz description"
    Settings.set 'searchEngines', searchEngines
    result = SearchEngineCompleter.getSearchEngines()
    assert.equal Object.keys(result).length, 2
    assert.equal "bar?q=%s", result["foo"].url
    assert.isFalse result["foo"].description
    assert.equal "qux?q=%s", result["baz"].url
    assert.equal "baz description", result["baz"].description

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