Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2014 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | var should = require("should"); |
| 17 | var when = require("when"); |
| 18 | |
| 19 | var settings = require("../../red/settings"); |
| 20 | |
| 21 | |
| 22 | describe("red/settings", function() { |
| 23 | |
| 24 | afterEach(function() { |
| 25 | settings.reset(); |
| 26 | }); |
| 27 | |
| 28 | it('wraps the user settings as read-only properties', function() { |
| 29 | var userSettings = { |
| 30 | a: 123, |
| 31 | b: "test", |
| 32 | c: [1,2,3] |
| 33 | } |
| 34 | settings.init(userSettings); |
| 35 | |
| 36 | settings.available().should.be.false; |
| 37 | |
| 38 | settings.a.should.equal(123); |
| 39 | settings.b.should.equal("test"); |
| 40 | settings.c.should.be.an.Array.with.lengthOf(3); |
| 41 | |
| 42 | settings.get("a").should.equal(123); |
| 43 | settings.get("b").should.equal("test"); |
| 44 | settings.get("c").should.be.an.Array.with.lengthOf(3); |
| 45 | |
| 46 | /*jshint immed: false */ |
| 47 | (function() { |
| 48 | settings.a = 456; |
| 49 | }).should.throw(); |
| 50 | |
| 51 | settings.c.push(5); |
| 52 | settings.c.should.be.an.Array.with.lengthOf(4); |
| 53 | |
| 54 | /*jshint immed: false */ |
| 55 | (function() { |
| 56 | settings.set("a",456); |
| 57 | }).should.throw(); |
| 58 | |
| 59 | /*jshint immed: false */ |
| 60 | (function() { |
| 61 | settings.set("a",456); |
| 62 | }).should.throw(); |
| 63 | |
| 64 | /*jshint immed: false */ |
| 65 | (function() { |
| 66 | settings.get("unknown"); |
| 67 | }).should.throw(); |
| 68 | |
| 69 | /*jshint immed: false */ |
| 70 | (function() { |
| 71 | settings.set("unknown",456); |
| 72 | }).should.throw(); |
| 73 | |
| 74 | }); |
| 75 | |
| 76 | it('loads global settings from storage', function(done) { |
| 77 | var userSettings = { |
| 78 | a: 123, |
| 79 | b: "test", |
| 80 | c: [1,2,3] |
| 81 | } |
| 82 | var savedSettings = null; |
| 83 | var storage = { |
| 84 | getSettings: function() { |
| 85 | return when.resolve({globalA:789}); |
| 86 | }, |
| 87 | saveSettings: function(settings) { |
| 88 | savedSettings = settings; |
| 89 | return when.resolve(); |
| 90 | } |
| 91 | } |
| 92 | settings.init(userSettings); |
| 93 | |
| 94 | settings.available().should.be.false; |
| 95 | |
| 96 | /*jshint immed: false */ |
| 97 | (function() { |
| 98 | settings.get("unknown"); |
| 99 | }).should.throw(); |
| 100 | |
| 101 | settings.load(storage).then(function() { |
| 102 | settings.available().should.be.true; |
| 103 | settings.get("globalA").should.equal(789); |
| 104 | settings.set("globalA","abc").then(function() { |
| 105 | savedSettings.globalA.should.equal("abc"); |
| 106 | done(); |
| 107 | }); |
| 108 | }).otherwise(function(err) { |
| 109 | done(err); |
| 110 | }); |
| 111 | |
| 112 | |
| 113 | }); |
| 114 | }); |