forked from tmeasday/meteor-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_client_tests.js
111 lines (92 loc) · 2.66 KB
/
router_client_tests.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
Tinytest.add("Router page", function(test) {
Meteor.Router.resetFilters();
Meteor.Router.add({
'/foo': 'foo',
'/bar/:id': function(id) {
Session.set('id', id);
return 'bar';
}
});
Meteor.Router.add(/page\/(\d+)/, function(number) {
Session.set('pageNo', number);
return 'page';
});
test.equal(Meteor.Router.page(), null);
Meteor.Router.to('/foo');
test.equal(Meteor.Router.page(), 'foo');
Meteor.Router.to('/bar/1');
test.equal(Meteor.Router.page(), 'bar');
test.equal(Session.get('id'), '1');
Meteor.Router.to('/page/7');
test.equal(Meteor.Router.page(), 'page');
test.equal(Session.get('pageNo'), '7');
});
Tinytest.add("Router reactivity", function(test) {
var context_called = 0;
Meteor.Router.resetFilters();
Meteor.Router.add({
'/foo': 'foo',
'/bar': 'bar',
'/bar/2': function() {
// do something unrelated
Session.set('router-test-page', 2);
return 'bar';
}
})
Meteor.autorun(function() {
Meteor.Router.page();
context_called += 1;
});
test.equal(context_called, 1);
Meteor.Router.to('/foo')
Meteor.flush()
test.equal(context_called, 2);
Meteor.Router.to('/bar')
Meteor.flush()
test.equal(context_called, 3);
// returns 'bar' to shouldn't trigger reactivity
Meteor.Router.to('/bar/2')
Meteor.flush()
test.equal(context_called, 3);
});
Tinytest.add("Router filtering", function(test) {
Meteor.Router.resetFilters();
Meteor.Router.add({
'/foo': 'foo',
'/bar': 'bar',
'/baz': 'baz'
})
Meteor.Router.filters({
'something_else': function() { return 'something_else'; },
'third': function() { return 'a third thing' }
})
Meteor.Router.filter('something_else', {only: 'foo'});
Meteor.Router.filter('third', {except: ['something_else', 'baz']});
Meteor.Router.to('/foo');
test.equal(Meteor.Router.page(), 'something_else');
Meteor.Router.to('/bar');
test.equal(Meteor.Router.page(), 'a third thing');
Meteor.Router.to('/baz');
test.equal(Meteor.Router.page(), 'baz');
});
Tinytest.add("FilteredRouter filter reactivity", function(test) {
Meteor.Router.resetFilters();
Meteor.Router.add('/foo', 'foo');
Meteor.Router.filters({
'something_else': function(page) {
if (Session.get('something_else')) {
return 'something_else';
} else {
return page;
}
}
});
Meteor.Router.filter('something_else');
Session.set('something_else', null);
Meteor.flush();
Meteor.Router.to('/foo');
test.equal(Meteor.Router.page(), 'foo');
Session.set('something_else', true);
Meteor.flush();
test.equal(Meteor.Router.page(), 'something_else');
});