group_configuration_spec.js 11.8 KB
Newer Older
1
define([
2
    'backbone', 'coffee/src/main', 'js/models/group_configuration',
3
    'js/models/group', 'js/collections/group', 'squire'
4
], function(
5
    Backbone, main, GroupConfigurationModel, GroupModel, GroupCollection, Squire
6 7 8 9 10 11
) {
    'use strict';
    beforeEach(function() {
      this.addMatchers({
        toBeInstanceOf: function(expected) {
          return this.actual instanceof expected;
12 13 14
        },
        toBeEmpty: function() {
            return this.actual.length === 0;
15 16 17 18
        }
      });
    });

19
    describe('GroupConfigurationModel', function() {
20 21
        beforeEach(function() {
            main();
22
            this.model = new GroupConfigurationModel();
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        });

        describe('Basic', function() {
            it('should have an empty name by default', function() {
                expect(this.model.get('name')).toEqual('');
            });

            it('should have an empty description by default', function() {
                expect(this.model.get('description')).toEqual('');
            });

            it('should not show groups by default', function() {
                expect(this.model.get('showGroups')).toBeFalsy();
            });

38
            it('should have a collection with 2 groups by default', function() {
39 40
                var groups = this.model.get('groups');

41
                expect(groups).toBeInstanceOf(GroupCollection);
42 43
                expect(groups.at(0).get('name')).toBe('Group A');
                expect(groups.at(1).get('name')).toBe('Group B');
44 45
            });

46 47 48 49
            it('should have an empty usage by default', function() {
                expect(this.model.get('usage')).toBeEmpty();
            });

50
            it('should be able to reset itself', function() {
51 52 53 54
                var originalName = 'Original Name',
                    model = new GroupConfigurationModel({name: originalName});
                model.set({name: 'New Name'});
                model.reset();
55

56
                expect(model.get('name')).toEqual(originalName);
57 58 59 60 61 62 63 64
            });

            it('should be dirty after it\'s been changed', function() {
                this.model.set('name', 'foobar');

                expect(this.model.isDirty()).toBeTruthy();
            });

65 66 67 68 69 70 71 72
            describe('should not be dirty', function () {
                it('by default', function() {
                    expect(this.model.isDirty()).toBeFalsy();
                });

                it('after calling setOriginalAttributes', function() {
                    this.model.set('name', 'foobar');
                    this.model.setOriginalAttributes();
73

74 75
                    expect(this.model.isDirty()).toBeFalsy();
                });
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
            });
        });

        describe('Input/Output', function() {
            var deepAttributes = function(obj) {
                if (obj instanceof Backbone.Model) {
                    return deepAttributes(obj.attributes);
                } else if (obj instanceof Backbone.Collection) {
                    return obj.map(deepAttributes);
                } else if (_.isObject(obj)) {
                    var attributes = {};

                    for (var prop in obj) {
                        if (obj.hasOwnProperty(prop)) {
                            attributes[prop] = deepAttributes(obj[prop]);
                        }
                    }
                    return attributes;
                } else {
                    return obj;
                }
            };

            it('should match server model to client model', function() {
                var serverModelSpec = {
101 102 103
                        'id': 10,
                        'name': 'My Group Configuration',
                        'description': 'Some description',
104 105
                        'version': 2,
                        'scheme': 'random',
106 107 108
                        'groups': [
                            {
                                'version': 1,
109 110
                                'name': 'Group 1',
                                'usage': []
111 112
                            }, {
                                'version': 1,
113 114
                                'name': 'Group 2',
                                'usage': []
115 116
                            }
                        ]
117 118
                    },
                    clientModelSpec = {
119 120 121
                        'id': 10,
                        'name': 'My Group Configuration',
                        'description': 'Some description',
122
                        'scheme': 'random',
123 124
                        'showGroups': false,
                        'editing': false,
125
                        'version': 2,
126 127 128
                        'groups': [
                            {
                                'version': 1,
129
                                'order': 0,
130 131
                                'name': 'Group 1',
                                'usage': []
132 133
                            }, {
                                'version': 1,
134
                                'order': 1,
135 136
                                'name': 'Group 2',
                                'usage': []
137
                            }
138 139
                        ],
                        'usage': []
140
                    },
141 142 143
                    model = new GroupConfigurationModel(
                        serverModelSpec, { parse: true }
                    );
144 145 146 147 148 149 150 151

                expect(deepAttributes(model)).toEqual(clientModelSpec);
                expect(model.toJSON()).toEqual(serverModelSpec);
            });
        });

        describe('Validation', function() {
            it('requires a name', function() {
152
                var model = new GroupConfigurationModel({ name: '' });
153 154 155 156 157

                expect(model.isValid()).toBeFalsy();
            });

            it('can pass validation', function() {
158 159
                // Note that two groups - Group A and Group B - are
                // created by default.
160
                var model = new GroupConfigurationModel({ name: 'foo' });
161 162 163

                expect(model.isValid()).toBeTruthy();
            });
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

            it('requires at least one group', function() {
                var group1 = new GroupModel({ name: 'Group A' }),
                    model = new GroupConfigurationModel({ name: 'foo', groups: [] });

                expect(model.isValid()).toBeFalsy();

                model.get('groups').add(group1);
                expect(model.isValid()).toBeTruthy();
            });

            it('requires a valid group', function() {
                var model = new GroupConfigurationModel({ name: 'foo', groups: [{ name: '' }] });

                expect(model.isValid()).toBeFalsy();
            });

            it('requires all groups to be valid', function() {
                var model = new GroupConfigurationModel({ name: 'foo', groups: [{ name: 'Group A' }, { name: '' }] });

                expect(model.isValid()).toBeFalsy();
            });

            it('requires all groups to have unique names', function() {
                var model = new GroupConfigurationModel({
                    name: 'foo', groups: [{ name: 'Group A' }, { name: 'Group A' }]
                });

                expect(model.isValid()).toBeFalsy();
            });
194 195 196
        });
    });

197
    describe('GroupModel', function() {
198
        beforeEach(function() {
199 200
            this.collection = new GroupCollection([{}]);
            this.model = this.collection.at(0);
201 202 203
        });

        describe('Basic', function() {
204
            it('should have an empty name by default', function() {
205 206 207 208 209 210 211 212 213 214
                expect(this.model.get('name')).toEqual('');
            });

            it('should be empty by default', function() {
                expect(this.model.isEmpty()).toBeTruthy();
            });
        });

        describe('Validation', function() {
            it('requires a name', function() {
215
                var model = new GroupModel({ name: '' });
216 217 218 219 220

                expect(model.isValid()).toBeFalsy();
            });

            it('can pass validation', function() {
221
                var model = new GroupConfigurationModel({ name: 'foo' });
222 223 224 225 226 227

                expect(model.isValid()).toBeTruthy();
            });
        });
    });

228
    describe('GroupCollection', function() {
229
        beforeEach(function() {
230
            this.collection = new GroupCollection();
231 232 233 234 235 236 237
        });

        it('is empty by default', function() {
            expect(this.collection.isEmpty()).toBeTruthy();
        });

        it('is empty if all groups are empty', function() {
238
            this.collection.add([{ name: '' }, { name: '' }, { name: '' }]);
239 240 241 242 243

            expect(this.collection.isEmpty()).toBeTruthy();
        });

        it('is not empty if a group is not empty', function() {
244 245 246
            this.collection.add([
                { name: '' }, { name: 'full' }, { name: '' }
            ]);
247 248 249

            expect(this.collection.isEmpty()).toBeFalsy();
        });
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

        describe('getGroupId', function () {
            var collection, injector, mockGettext, initializeGroupModel;

            mockGettext = function (returnedValue) {
                var injector = new Squire();

                injector.mock('gettext', function () {
                    return function () { return returnedValue; };
                });

                return injector;
            };

            initializeGroupModel = function (dict, that) {
                runs(function() {
                    injector = mockGettext(dict);
                    injector.require(['js/collections/group'],
                    function(GroupCollection) {
                        collection = new GroupCollection();
                    });
                });

                waitsFor(function() {
                    return collection;
                }, 'GroupModel was not instantiated', 500);

                that.after(function () {
                    collection = null;
                    injector.clean();
                    injector.remove();
                });
            };

            it('returns correct ids', function () {
                var collection = new GroupCollection();

                expect(collection.getGroupId(0)).toBe('A');
                expect(collection.getGroupId(1)).toBe('B');
                expect(collection.getGroupId(25)).toBe('Z');
                expect(collection.getGroupId(702)).toBe('AAA');
                expect(collection.getGroupId(704)).toBe('AAC');
                expect(collection.getGroupId(475253)).toBe('ZZZZ');
                expect(collection.getGroupId(475254)).toBe('AAAAA');
                expect(collection.getGroupId(475279)).toBe('AAAAZ');
            });

            it('just 1 character in the dictionary', function () {
                initializeGroupModel('1', this);
                runs(function() {
                    expect(collection.getGroupId(0)).toBe('1');
                    expect(collection.getGroupId(1)).toBe('11');
                    expect(collection.getGroupId(5)).toBe('111111');
                });
            });

            it('allow to use unicode characters in the dict', function () {
                initializeGroupModel('ö诶úeœ', this);
                runs(function() {
                    expect(collection.getGroupId(0)).toBe('ö');
                    expect(collection.getGroupId(1)).toBe('诶');
                    expect(collection.getGroupId(5)).toBe('öö');
                    expect(collection.getGroupId(29)).toBe('œœ');
                    expect(collection.getGroupId(30)).toBe('ööö');
                    expect(collection.getGroupId(43)).toBe('öúe');
                });
            });

            it('return initial value if dictionary is empty', function () {
                initializeGroupModel('', this);
                runs(function() {
                    expect(collection.getGroupId(0)).toBe('0');
                    expect(collection.getGroupId(5)).toBe('5');
                    expect(collection.getGroupId(30)).toBe('30');
                });
            });
        });
327 328
    });
});