-
Notifications
You must be signed in to change notification settings - Fork 2
/
route-beutifyLocation.js
113 lines (98 loc) · 3.19 KB
/
route-beutifyLocation.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
/*globals PushStateTree, it, expect, beforeEach, beforeAll */
describe('PushStateTree beutifyLocation should', function() {
'use strict';
var events = {
popstate: [],
hashchange: [],
DOMContentLoaded: [],
readystatechange: [],
load: []
};
beforeAll(function(){
var addEventListener = window.addEventListener;
window.addEventListener = function(name, callback){
events[name].push(callback);
addEventListener.apply(window, arguments);
};
});
beforeEach(function(){
// Reset the URI before begin the tests
history.pushState(null, null, '/');
for (var eventName in events)
if (events.hasOwnProperty(eventName)) {
var eventList = events[eventName];
while (eventList.length) {
var callback = eventList.pop();
window.removeEventListener(eventName, callback);
}
}
});
it('not enable beautifyLocation feature by default', function(){
var pst = new PushStateTree();
expect(pst.beautifyLocation).toBeFalsy();
});
it('allow to change the beautifyLocation flag after start running', function(){
var pst = new PushStateTree();
pst.beautifyLocation = false;
expect(pst.beautifyLocation).toBeFalsy();
pst.beautifyLocation = true;
expect(pst.beautifyLocation).toBeTruthy();
});
it('prioritise the hash to provide the URI', function(){
var pst = new PushStateTree({
beautifyLocation: false
});
location.hash = '#test';
expect(pst.uri).toEqual('test');
});
it('remove the first slash from the URI in the regular URL', function(){
var pst = new PushStateTree({
beautifyLocation: false
});
history.pushState(null, null, '/test');
expect(location.pathname).toEqual('/test');
expect(pst.uri).toEqual('test');
});
it('remove the first slash from the URI in the location.hash', function(){
var pst = new PushStateTree({
beautifyLocation: false
});
location.hash = '/test';
expect(location.hash).toEqual('#/test');
expect(pst.uri).toEqual('test');
});
it('redirect from the hash to path when beautifyLocation is enabled', function(){
var pst = new PushStateTree({
beautifyLocation: true
});
// Reset URL
var randomURI = Math.random() + '';
history.pushState(null, null, '/' + randomURI);
expect(pst.uri).toEqual(randomURI);
expect(location.pathname).toEqual('/' + randomURI);
location.hash = '/abc';
expect(pst.uri).toEqual('abc');
expect(location.hash).toEqual('');
expect(location.pathname).toEqual('/abc');
});
it('not apply beautifyLocation when the basePath is not fulfilled', function(){
history.pushState(null, null, '/invalidBasePath/');
var pst = new PushStateTree({
beautifyLocation: true,
basePath: '/test/'
});
location.hash = '/abc';
expect(pst.uri).toEqual('abc');
expect(location.hash).toEqual('#/abc');
});
it('apply beautifyLocation when the basePath is fulfilled', function(){
history.pushState(null, null, '/test/');
var pst = new PushStateTree({
beautifyLocation: true,
basePath: '/test/'
});
location.hash = '/abc';
expect(pst.uri).toEqual('abc');
expect(location.hash).toEqual('');
});
});