2013-01-05 04:06:03 +04:00
|
|
|
fs = require 'fs'
|
|
|
|
|
2012-12-13 06:05:28 +04:00
|
|
|
describe "Config", ->
|
2012-12-18 05:52:20 +04:00
|
|
|
describe ".get(keyPath) and .set(keyPath, value)", ->
|
|
|
|
it "allows a key path's value to be read and written", ->
|
|
|
|
expect(config.set("foo.bar.baz", 42)).toBe 42
|
|
|
|
expect(config.get("foo.bar.baz")).toBe 42
|
|
|
|
expect(config.get("bogus.key.path")).toBeUndefined()
|
|
|
|
|
|
|
|
it "updates observers and saves when a key path is set", ->
|
|
|
|
observeHandler = jasmine.createSpy "observeHandler"
|
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.set("foo.bar.baz", 42)
|
|
|
|
|
|
|
|
expect(config.save).toHaveBeenCalled()
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith 42
|
|
|
|
|
2013-01-05 04:06:03 +04:00
|
|
|
describe ".save()", ->
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(fs, 'write')
|
|
|
|
jasmine.unspy config, 'save'
|
|
|
|
|
|
|
|
it "writes any non-default properties to the config.json in the user's .atom directory", ->
|
|
|
|
config.set("a.b.c", 1)
|
|
|
|
config.set("a.b.d", 2)
|
|
|
|
config.set("x.y.z", 3)
|
|
|
|
config.setDefaults("a.b", e: 4, f: 5)
|
|
|
|
|
2013-01-05 05:07:37 +04:00
|
|
|
fs.write.reset()
|
2013-01-05 04:06:03 +04:00
|
|
|
config.save()
|
|
|
|
|
|
|
|
writtenConfig = JSON.parse(fs.write.argsForCall[0][1])
|
|
|
|
expect(writtenConfig).toEqual config.settings
|
|
|
|
|
2012-12-18 07:56:28 +04:00
|
|
|
describe ".setDefaults(keyPath, defaults)", ->
|
|
|
|
it "assigns any previously-unassigned keys to the object at the key path", ->
|
|
|
|
config.set("foo.bar.baz", a: 1)
|
|
|
|
config.setDefaults("foo.bar.baz", a: 2, b: 3, c: 4)
|
2013-01-05 04:06:03 +04:00
|
|
|
expect(config.get("foo.bar.baz.a")).toBe 1
|
|
|
|
expect(config.get("foo.bar.baz.b")).toBe 3
|
|
|
|
expect(config.get("foo.bar.baz.c")).toBe 4
|
2012-12-18 07:56:28 +04:00
|
|
|
|
|
|
|
config.setDefaults("foo.quux", x: 0, y: 1)
|
2013-01-05 04:06:03 +04:00
|
|
|
expect(config.get("foo.quux.x")).toBe 0
|
|
|
|
expect(config.get("foo.quux.y")).toBe 1
|
2012-12-18 07:56:28 +04:00
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
describe ".update()", ->
|
2012-12-18 07:56:28 +04:00
|
|
|
it "updates observers if a value is mutated without the use of .set", ->
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar.baz", ["a"])
|
|
|
|
observeHandler = jasmine.createSpy "observeHandler"
|
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.get("foo.bar.baz").push("b")
|
|
|
|
config.update()
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith config.get("foo.bar.baz")
|
2012-12-18 06:00:47 +04:00
|
|
|
observeHandler.reset()
|
|
|
|
|
|
|
|
config.update()
|
|
|
|
expect(observeHandler).not.toHaveBeenCalled()
|
2012-12-15 03:21:18 +04:00
|
|
|
|
|
|
|
describe ".observe(keyPath)", ->
|
2012-12-13 06:05:28 +04:00
|
|
|
observeHandler = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
observeHandler = jasmine.createSpy("observeHandler")
|
2012-12-18 06:00:47 +04:00
|
|
|
config.set("foo.bar.baz", "value 1")
|
2012-12-13 06:05:28 +04:00
|
|
|
config.observe "foo.bar.baz", observeHandler
|
|
|
|
|
|
|
|
it "fires the given callback with the current value at the keypath", ->
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 1")
|
|
|
|
|
|
|
|
it "fires the callback every time the observed value changes", ->
|
|
|
|
observeHandler.reset() # clear the initial call
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set('foo.bar.baz', "value 2")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 2")
|
|
|
|
observeHandler.reset()
|
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set('foo.bar.baz', "value 1")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("value 1")
|
|
|
|
|
|
|
|
it "fires the callback when the full key path goes into and out of existence", ->
|
|
|
|
observeHandler.reset() # clear the initial call
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar", undefined)
|
2012-12-13 06:05:28 +04:00
|
|
|
|
|
|
|
expect(observeHandler).toHaveBeenCalledWith(undefined)
|
|
|
|
observeHandler.reset()
|
|
|
|
|
2012-12-18 05:52:20 +04:00
|
|
|
config.set("foo.bar.baz", "i'm back")
|
2012-12-13 06:05:28 +04:00
|
|
|
expect(observeHandler).toHaveBeenCalledWith("i'm back")
|