summaryrefslogtreecommitdiff
path: root/horizon/static/horizon/js/angular/services/hz.api.config.spec.js
blob: 7a439766890f2e5c1ee431070524e5e53327df8f (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*global angular,describe,it,expect,inject,module,beforeEach,afterEach*/
(function () {
  'use strict';

  describe('settingsService', function () {
    var settingsService,
      $httpBackend,
      responseMockOpts = {succeed: true},
      testData = {
        isTrue: true,
        isFalse: false,
        versions: {one: 1, two: 2},
        deep: {nest: { foo: 'bar' } },
        isNull: null
      };

    beforeEach(module('hz.api'));
    beforeEach(inject(function (_$httpBackend_, _settingsService_) {
      responseMockOpts.succeed = true;
      settingsService = _settingsService_;
      $httpBackend = _$httpBackend_;
      $httpBackend.whenGET('/api/settings/').respond(
        function () {
          return responseMockOpts.succeed ?
            [200, testData, {}] : [500, 'Fail', {}];
        });
      $httpBackend.expectGET('/api/settings/');
    }));

    afterEach(function () {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

    describe('getSettings', function () {

      it('should return all settings', function () {
        settingsService.getSettings().then(
          function (actual) {
            expect(actual).toEqual(testData);
          }
        );
        $httpBackend.flush();
      });

      it('should fail when error response', function () {
        responseMockOpts.succeed = false;
        settingsService.getSettings().then(
          function (actual) {
            fail('Should not have succeeded: ' + angular.toJson(actual));
          },
          function (actual) {
            expect(actual).toBeDefined();
          }
        );
        $httpBackend.flush();
      });

    });

    describe('getSetting', function () {

      it('nested deep object is found', function () {
        settingsService.getSetting('deep.nest.foo')
          .then(function (actual) {
            expect(actual).toEqual('bar');
          });
        $httpBackend.flush();
      });

      it("is undefined when doesn't exist", function () {
        settingsService.getSetting('will.not.exist')
          .then(function (actual) {
            expect(actual).toBeUndefined();
          });
        $httpBackend.flush();
      });

      it("default is returned when doesn't exist", function () {
        var actual;
        settingsService.getSetting('will.not.exist', 'hello')
          .then(function (actual) {
            expect(actual).toEqual('hello');
          });
        $httpBackend.flush();
      });

      it('should return true', function () {
        settingsService.getSetting('isTrue')
          .then(function (actual) {
            expect(actual).toEqual(true);
          });
        $httpBackend.flush();
      });

      it('should fail when error response', function () {
        responseMockOpts.succeed = false;
        settingsService.getSetting('isTrue').then(
          function (actual) {
            fail('Should not have succeeded: ' + angular.toJson(actual));
          },
          function (actual) {
            expect(actual).toBeDefined();
          }
        );
        $httpBackend.flush();
      });

    });

    describe('ifEnabled', function () {

      var expectedResult = {};

      var enabled = function () {
        expectedResult.enabled = true;
      };

      var notEnabled = function () {
        expectedResult.enabled = false;
      };

      beforeEach(inject(function () {
        expectedResult = {enabled: null};
      }));

      function meetsExpectations(expected) {
        $httpBackend.flush();
        expect(expectedResult.enabled).toBe(expected);
      }

      it('should fail when error response', function () {
        responseMockOpts.succeed = false;
        settingsService.ifEnabled('isTrue').then(
          function (actual) {
            fail('Should not have succeeded: ' + angular.toJson(actual));
          },
          function (actual) {
            expect(actual).toBeDefined();
          }
        );
        $httpBackend.flush();
      });

      it('boolean is enabled when true', function () {
        settingsService.ifEnabled('isTrue').then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('boolean is enabled when true expected', function () {
        settingsService.ifEnabled('isTrue', true).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('boolean is not enabled when false expected', function () {
        settingsService.ifEnabled('isTrue', false).then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('boolean is not enabled when false', function () {
        settingsService.ifEnabled('isFalse').then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('boolean is enabled when false expected', function () {
        settingsService.ifEnabled('isFalse', false).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('nested object is enabled when expected', function () {
        settingsService.ifEnabled('versions.one', 1).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('nested object is not enabled', function () {
        settingsService.ifEnabled('versions.two', 1).then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('nested object is not enabled when not found', function () {
        settingsService.ifEnabled('no-exist.two', 1).then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('nested deep object is enabled when expected', function () {
        settingsService.ifEnabled('deep.nest.foo', 'bar').then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('nested deep object is not enabled when not expected', function () {
        settingsService.ifEnabled('deep.nest.foo', 'wrong').then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('null is not enabled', function () {
        settingsService.ifEnabled('isNull').then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('null is enabled when expected', function () {
        settingsService.ifEnabled('isNull', null).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('true is enabled when not found and true default', function () {
        settingsService.ifEnabled('nonExistent', true, true).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('true is not enabled when not found and false default', function () {
        settingsService.ifEnabled('nonExistent', true, false).then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('true is not enabled when not found and no default', function () {
        settingsService.ifEnabled('nonExistent', true).then(enabled, notEnabled);
        meetsExpectations(false);
      });

      it('false is enabled when not found and expected w/ default', function () {
        settingsService.ifEnabled('nonExistent', false, false).then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('bar is enabled when expected not found and bar default', function () {
        settingsService.ifEnabled('nonExistent', 'bar', 'bar').then(enabled, notEnabled);
        meetsExpectations(true);
      });

      it('bar is not enabled when expected not found and not default', function () {
        settingsService.ifEnabled('nonExistent', 'bar', 'foo').then(enabled, notEnabled);
        meetsExpectations(false);
      });
    });

  });

})();