forked from AmpersandJS/ampersand-view-conventions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampersand-view-conventions.js
114 lines (94 loc) · 4.58 KB
/
ampersand-view-conventions.js
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
var extend = require('extend-object');
var isFunction = require('is-function');
exports.view = function (test, ViewClass, requiredOptions) {
var tests = {
'basics': function (t) {
var view = new ViewClass(requiredOptions || {});
t.ok(view, 'should init with `new`');
t.ok(isFunction(view.render), 'has `render` method');
t.ok(isFunction(view.remove), 'has `remove` method');
t.end();
},
'root element handling': function (t) {
var container = document.createElement('div');
var viewRoot = document.createElement('div');
container.appendChild(viewRoot);
var view = new ViewClass(extend(requiredOptions || {}, {el: viewRoot}));
// note it's possible this isn't the same element as passed in, it's ok to autorender
// and replace the element you were passed.
t.ok(view.el.nodeName, 'should have a `this.el` that is an element if passed into constructor');
t.equal(view.el.parentNode, container, 'parent container should be the same');
// these should still be true, post-`render()`
t.doesNotThrow(function () {
view.render();
}, 'should not error when calling render');
t.ok(view.el.nodeName, 'should have a `this.el` that is an element if passed into constructor');
t.equal(view.el.parentNode, container, 'parent container should be the same');
// calling remove should remove itself from the DOM
view.remove();
t.ok(!view.el.parentNode, 'after calling remove should no longer have a parent node');
t.ok(container.children.length === 0, 'container should have no children');
t.end();
}
};
for (var item in tests) {
test('view-compliance: ' + item, tests[item]);
}
};
// for testing rules for form fields
exports.formField = function (test, ViewClass, requiredOptions, validValue) {
var tests = {
'basics': function (t) {
var counter;
var parent = {
update: function (field) {
counter++;
this.passedField = field;
}
};
var view = new ViewClass(extend(requiredOptions || {}, {parent: parent}));
// helper we can call
function ensureProperties(str) {
str = str || '';
t.ok(view.hasOwnProperty('value') || view.value, 'has `value` property' + str);
t.equal(typeof view.name, 'string', 'has `name` property that is a string' + str);
t.notEqual(view.name, '', '`name` property should not be empty string' + str);
t.ok(isFunction(view.setValue), 'has `setValue` method' + str);
t.ok(typeof view.valid, 'boolean', 'has `valid` property that is a boolean' + str);
t.equal(parent, view.parent, 'has same `parent` property' + str);
}
t.ok(view, 'should init with `new`');
ensureProperties();
counter = 0;
t.doesNotThrow(function () {
view.setValue(validValue);
}, 'should not error when setting valid value');
t.equal(counter, 1, 'should have called `update` on parent when value changed');
t.equal(parent.passedField, view, 'should have passed itself to the parent when changed');
// all this should still be true after setting a value
ensureProperties(' after setting a value');
// all this should be true after calling `beforeSubmit`, if present.
if (isFunction(view.beforeSubmit)) {
view.beforeSubmit();
ensureProperties(' after `beforeSubmit`');
}
t.end();
},
'handling values passed in instantiation': function (t) {
var parent = {update: function () {}};
var view = new ViewClass(extend(requiredOptions || {}, {
value: validValue,
parent: parent,
name: 'awesome name'
}));
t.equal(view.value, validValue, 'should have maintained its value');
t.strictEqual(view.valid, true, 'should be `valid` at init when passed valid value');
t.equal(view.parent, parent, 'should have kept its `parent`');
t.equal(view.name, 'awesome name', 'should have kepts its `name`');
t.end();
}
};
for (var item in tests) {
test('input-view-compliance: ' + item, tests[item]);
}
};