-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
180 lines (153 loc) · 4.48 KB
/
tests.js
File metadata and controls
180 lines (153 loc) · 4.48 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
var Hash = require('./');
var test = require('tapes');
test('Testing initialization', function(t) {
// Before Each
t.afterEach(function(t) {
Hash.destroy();
t.end();
});
t.test('Hash.subscribe is defined', function(t) {
t.plan(1);
t.notEqual(Hash, null);
});
t.test('Hash.subscribe is a singleton', function(t) {
t.plan(1);
var hashInstance = Hash;
t.deepEqual(hashInstance, Hash);
});
t.test('Hash can be initialized with a default hash String', function(t) {
t.plan(1);
var initHash = 'foo=bar';
Hash.init(initHash);
t.equal(window.location.hash, '#'+initHash);
});
t.end();
});
test('Testing setters', function(t) {
t.afterEach(function(t) {
Hash.destroy();
t.end();
});
t.test('Hash can be set with a default hash String containing the Hash character', function(t) {
t.plan(1);
var initHash = '#foo=bar';
Hash.init(initHash);
t.equal(window.location.hash, initHash);
});
t.test('Hash can be set with a default hash Object', function(t) {
t.plan(1);
var initHash = {'baz': ['qux','fubar']};
var mockHashStr = '#baz=qux,fubar';
Hash.init(initHash);
t.equal(window.location.hash, mockHashStr);
});
t.test('Can update one parameter only', function(t) {
t.plan(2);
var initHash = {'baz': ['qux'], 'foo': ['bar']};
Hash.init(initHash);
t.equal(Hash.getHash(), 'baz=qux&foo=bar');
Hash.updateHashKeyValue('foo', ['bar1', 'bar2']);
t.equal(Hash.getHash(), 'baz=qux&foo=bar1,bar2');
});
t.test('Can delete one parameter only', function(t) {
t.plan(2);
var initHash = {'qux': ['baz'], 'bar': ['foo']};
Hash.init(initHash);
t.equal(Hash.getHash(), 'qux=baz&bar=foo');
Hash.deleteParam('qux');
t.equal(Hash.getHash(), 'bar=foo');
});
t.test('Can delete several parameters', function(t) {
t.plan(2);
var initHash = {'baz': ['qux'], 'foo': ['bar'], 'place': ['holder']};
Hash.init(initHash);
t.equal(Hash.getHash(), 'baz=qux&foo=bar&place=holder');
Hash.deleteParam(['baz', 'place']);
t.equal(Hash.getHash(), 'foo=bar');
});
t.end();
});
test('Testing getters', function(t) {
// Before Each
t.afterEach(function(t) {
Hash.destroy();
t.end();
});
t.test('getHash returns the hash as a string', function(t) {
t.plan(1);
var initHash = 'foo=bar';
Hash.init(initHash);
var currentHash = Hash.getHash();
t.equal(currentHash, initHash);
});
t.test('getParams returns an array of all hash parameters', function(t) {
t.plan(1);
var initHash = 'baz=qux&foo=bar1,bar2';
var mockHash = {'foo': {values: ['bar1', 'bar2']}, 'baz': {values: ['qux']}};
Hash.init(initHash);
var currentHashParams = Hash.getParams();
t.deepEqual(currentHashParams, mockHash);
});
t.test('getParam returns the values of one hash parameter', function(t) {
t.plan(1);
var initHash = 'qux=baz&bar=foo1,foo2';
var mockHash = ['foo1', 'foo2'];
Hash.init(initHash);
var currentHashParam = Hash.getParam('bar');
t.deepEqual(currentHashParam, mockHash);
});
t.test('getParam returns false if parameter does not exist', function(t) {
t.plan(1);
Hash.init('');
t.deepEqual(Hash.getParam(''), false);
});
t.end();
});
test('Testing Hash Subscription', function(t) {
// Before Each
t.afterEach(function(t) {
Hash.destroy();
t.end();
});
t.test('Should be able to subscribe to Hash and receive change notifications', function(t) {
t.plan(1);
var initHash = 'foo=bar';
Hash.init(initHash);
var updatedHash = 'foo=bar1';
Hash.subscribe(['foo'], function(c) {
if (c.foo.changed) {
t.deepEqual(c.foo.values, ['bar1']);
} else {
t.fail();
}
});
Hash.setHash(updatedHash);
});
t.test('Should be able to mute subscription', function(t) {
t.plan(1);
Hash.init('baz=qux');
Hash.subscribe(['baz'], function(c) {
if (c.baz.changed) {
t.deepEqual(c.baz.values, ['qux2']);
} else {
t.fail();
}
});
Hash.mute();
Hash.setHash('baz=qux1');
Hash.unmute();
Hash.setHash('baz=qux2');
});
t.test('Should receive notification changed=false when hash param does not change', function(t) {
t.plan(2);
var initHash = 'bar=1&hoo=1';
Hash.init(initHash);
Hash.subscribe(['bar', 'hoo'], function(c) {
t.equal(c.bar.changed, true);
t.equal(c.hoo.changed, false);
});
window.location.hash = 'bar=2&hoo=1';
});
t.end();
});