This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
/
test.js
161 lines (126 loc) · 4.05 KB
/
test.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
152
153
154
155
156
157
158
159
160
161
// mock browser globals
var parseURL = require('url').parse;
var historyQueue = []
let windowCallbacks = {}
global.window = {
location: {
pathname: '/',
search: '',
hash: ''
},
addEventListener: (name, cb) => {
windowCallbacks[name] = cb
}
}
function* goBack() {
//console.log(historyQueue.length)
var current = historyQueue.pop()
var previous = historyQueue.pop()
window.location.pathname = previous.pathname
window.location.search = previous.search
window.location.hash = previous.hash
windowCallbacks.popstate(previous)
}
global.document = {
title: 'test'
}
global.history = {
pushState: (state, title, url) => {
window.location = parseURL(url)
historyQueue.push(window.location);
},
replaceState: (state, title, url) => {
window.location = parseURL(url)
},
}
// run tests
var test = require('tape-async');
var React = require('react')
var ReactDom = require('react-dom/server')
var testComponent = (props) => <div>path: {props.location.pathname}. params: {JSON.stringify(props.router.params)}. search: {props.location.search}</div>
var testComponent2 = (props) => <div>2</div>
var testComponent3 = (props) => <div>3</div>
var testComponent4 = (props) => <div>4</div>
var pageNotFound = (props) => <div>not found</div>
var { createStore, dispatch, promiseAction, subscribe } = require('fluxury')
// subscribe((state, action) => console.log('action', action))
var router= require('./src/index')
var { location, replaceRoutes, loadContent, loadRoutes, promiseContent } = require('./src/index')
loadRoutes([{
path: '/',
load: Promise.resolve(testComponent)
}, {
path: '/admin',
load: () => Promise.resolve(testComponent) // Promise resolves to component
}, {
path: '/admin2',
load: () => Promise.resolve({ default: testComponent2 }) // Promise resolves to ES6 default.
}, {
path: '/admin3',
component: testComponent3
}, {
path: '/admin4',
component: testComponent4
}, {
path: '/page/:num',
component: testComponent4
}, {
path: '*',
component: testComponent
}])
test( 'Exports are correct type', function(t) {
t.plan(2)
t.equal(typeof location, 'object')
t.equal(typeof router, 'object')
})
test( 'Location includes valid exports', function(t) {
t.plan(3)
t.equal(location.name, 'location')
t.equal(typeof location.open, 'function')
t.equal(typeof location.redirect, 'function')
})
test( 'Router includes valid exports', function(t) {
t.plan(3)
t.equal(typeof loadContent, 'function')
t.equal(typeof router.loadContent, 'function')
t.equal(7, router.getState().routes.length)
})
test( 'location.open(path) works correctly', function* (t) {
t.plan(16)
var result = yield location.open('/admin')
t.equal( window.location.pathname, '/admin')
var result = yield location.open('/admin?foo=1#456')
t.equal( window.location.pathname, '/admin')
t.equal( window.location.search, '?foo=1')
t.equal( window.location.hash, '#456')
var result = yield location.open('/')
var rs = router.getState;
var result = yield location.open('/admin')
t.equal( rs().route.path, '/admin' );
t.equal( rs().content, testComponent );
var result = yield location.open('/admin2?foo=bar')
t.equal( rs().route.path, '/admin2' );
t.equal( rs().content, testComponent2 );
var result = yield location.open('/admin3')
t.equal( rs().route.path, '/admin3' );
t.equal( rs().content, testComponent3 );
var result = yield location.open('/admin4')
t.equal( rs().route.path, '/admin4' );
t.equal( rs().content, testComponent4 );
var result = yield location.open('/page/1')
t.equal( rs().route.path, '/page/:num' );
t.deepEqual( rs().params, { num: '1' })
var result = yield location.open('/page/2')
t.equal( rs().route.path, '/page/:num' );
t.deepEqual( rs().params, { num: '2' })
});
test( 'goBack works correctly', function*(t) {
t.plan(3)
var result = yield location.open('/admin?foo=1#bar')
yield location.open('/admin2')
yield goBack();
var state = location.getState();
t.equal( state.pathname, '/admin')
t.equal( state.search, '?foo=1')
t.equal( state.hash, '#bar')
});