/
main.js
151 lines (140 loc) · 4.34 KB
/
main.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
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
var test = require('tape');
var ViewSwitcher = require('../ampersand-view-switcher');
var View = require('ampersand-view');
var makeTestView = function(options) {
options = options || {};
return View.extend({
template: '<div data-hook="container"></div>',
autoRender: true,
render: function () {
this.renderWithTemplate();
this.switcher = new ViewSwitcher(this.queryByHook('container'), options);
}
});
};
var ItemView = View.extend({
template: '<a>hey</a>',
autoRender: true
});
var SelfInsertingView = View.extend({
insertSelf: true,
render: function () {}
});
test('basics', function (t) {
var Base = makeTestView();
var base = new Base();
var c1 = new ItemView();
var c2 = new ItemView();
t.ok(base.el);
base.switcher.set(c1);
t.equal(base.el.firstChild, c1.el);
base.switcher.set(c2);
t.equal(base.el.firstChild, c2.el);
t.end();
});
test('calls `empty` when appropriate', function (t) {
var count = 0;
var NewView = makeTestView({
empty: function () { count++; }
});
var c1 = new ItemView();
var c2 = new ItemView();
var base = new NewView();
t.equal(count, 1, 'should be called at first');
base.switcher.set(c1);
base.switcher.clear();
t.equal(count, 2, 'should be called when clear is called');
base.switcher.set(c2);
c2.remove();
t.equal(count, 3, 'should be called when view is removed');
t.end();
});
test('self inserting views can be set without throwing exceptions', function (t) {
var Base = makeTestView();
var base = new Base();
var v1 = new SelfInsertingView();
base.render();
t.doesNotThrow(function() {
base.switcher.set(v1);
});
t.end();
});
test('`options.show`', function (t) {
var TestView = makeTestView({
show: function (newView) {
t.equal(newView, c1, 'first param should be new view');
}
});
var base = new TestView();
var c1 = new ItemView();
base.switcher.set(c1);
t.equal(base.el.firstChild, c1.el, 'the current view was set');
t.end();
});
test('`options.prepend`', function (t) {
var TestView = makeTestView({
prepend: true
});
var base = new TestView();
var c1 = new ItemView();
base.el.appendChild(document.createElement('div'));
base.switcher.set(c1);
t.equal(c1.el.parentNode.firstChild, c1.el, 'view was prepended to container');
t.end();
});
test('`options.hide`', function (t) {
var TestView = makeTestView({
hide: function (oldView) {
t.equal(oldView, c1, 'first param should be previous view');
}
});
var base = new TestView();
var c1 = new ItemView();
var c2 = new ItemView();
base.switcher.set(c1);
base.switcher.set(c2);
t.equal(c1.el.parentNode, null, 'the previous view was removed');
t.equal(base.el.firstChild, c2.el, 'the current view was set');
t.end();
});
test('`option.hide` with `waitForRemove`', function (t) {
var TestView = makeTestView({
waitForRemove: true,
hide: function (oldView, cb) {
t.equal(oldView, c1, 'first param should be previous view');
t.ok(typeof cb === 'function', 'second param is callback');
cb();
}
});
var base = new TestView();
var c1 = new ItemView();
var c2 = new ItemView();
base.switcher.set(c1);
base.switcher.set(c2);
t.equal(c1.el.parentNode, null, 'the previous view was removed');
t.equal(base.el.firstChild, c2.el, 'the current view was set');
t.end();
});
test('`option.show` and `option.hide` used together', function (t) {
var showCount = 0;
var hideCount = 0;
var TestView = makeTestView({
show: function () {
showCount++;
},
hide: function () {
hideCount++;
}
});
var base = new TestView();
var c1 = new ItemView();
var c2 = new ItemView();
base.switcher.set(c1);
t.equal(base.el.firstChild, c1.el, 'first view was set');
base.switcher.set(c2);
t.equal(c1.el.parentNode, null, 'first view was removed');
t.equal(base.el.firstChild, c2.el, 'second view was set');
t.equal(showCount, 2, 'show should be called for every view we set');
t.equal(hideCount, 1, 'hide should be called only once, after first view has been set');
t.end();
});