rivets-backbone-is-required.spec.js
2.69 KB
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
define(function(require) {
'use strict';
var $ = require('jquery');
window.jQuery = $;
var RivetsBackboneIsRequired = require('rivets-backbone-is-required');
var _ = require('underscore');
var Backbone = require('backbone');
var rivets = require('rivets');
require('backbone-validation');
require('rivets-backbone-adapter');
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
//rivets.config.rootInterface = ':';
describe('RivetsBackboneIsRequired', function() {
it('returns undefined', function() {
expect(RivetsBackboneIsRequired).toBeUndefined();
});
var Person, Address;
beforeEach(function() {
Person = Backbone.Model.extend({
defaults: {
firstName: '',
middleName: '',
lastName: '',
},
validation: {
firstName: [
{required: true},
],
lastName: {
required: function() { return true; },
},
},
});
Address = Backbone.Model.extend({
defaults: {
address1: '',
},
});
});
var person, address, model;
beforeEach(function() {
person = new Person({
firstName: 'Adam',
lastName: 'Heath',
});
address = new Address();
model = new Backbone.Model({person: person, address: address});
});
function buildTest(objName, fieldName, wantedValue, isRequired) {
it(objName + ':' + fieldName + '=' + isRequired, function() {
var span = document.createElement('span');
span.setAttribute('rv-text', ':' + objName + ':' + fieldName);
span.setAttribute('rv-class-required', ':' + objName + ' | is-required \'' + fieldName + '\'');
//console.log('before bind', span.outerHTML);
var view = rivets.bind(span, model);
//console.log('after bind', span.outerHTML);
if (isRequired) {
expect(span.getAttribute('class')).toMatch(/\brequired\b/);
} else {
expect(span.getAttribute('class')).toBe(null);
}
});
}
buildTest('person', 'firstName', 'Adam', true);
buildTest('person', 'middleName', '', false);
buildTest('person', 'lastName', 'Heath', true);
buildTest('address', 'address1', '', false);
buildTest('missing', 'field', '', false);
});
});