1
1
const xtend = require ( 'xtend' )
2
+ const assert = require ( 'assert' )
2
3
const nanorouter = require ( 'nanorouter' )
3
4
const belCreateElement = require ( 'bel' ) . createElement
4
5
const rbelRegister = require ( 'rbel' )
@@ -8,6 +9,10 @@ const urlListener = require('url-listener')
8
9
9
10
class Tram {
10
11
constructor ( options ) {
12
+ if ( options ) {
13
+ assert . equal ( typeof options , 'object' , 'Tram-One: options should be an object' )
14
+ }
15
+
11
16
options = options || { }
12
17
const defaultRoute = options . defaultRoute || '/404'
13
18
@@ -18,13 +23,18 @@ class Tram {
18
23
}
19
24
20
25
addReducer ( key , reducer , state ) {
26
+ assert . equal ( typeof reducer , 'function' , 'Tram-One: reducer should be a function' )
27
+
21
28
this . reducers [ key ] = reducer
22
29
this . state [ key ] = state
23
30
24
31
return this
25
32
}
26
33
27
34
addRoute ( path , page ) {
35
+ assert . equal ( typeof path , 'string' , 'Tram-One: path should be a string' )
36
+ assert . equal ( typeof page , 'function' , 'Tram-One: page should be a function' )
37
+
28
38
this . router . on ( path , ( pathParams ) => ( state ) => {
29
39
const completeState = xtend (
30
40
state , { dispatch : this . store . dispatch } ,
@@ -37,6 +47,8 @@ class Tram {
37
47
}
38
48
39
49
dispatch ( action ) {
50
+ assert . equal ( typeof action , 'object' , 'Tram-One: action should be an object' )
51
+
40
52
this . store . dispatch ( action )
41
53
}
42
54
@@ -59,6 +71,9 @@ class Tram {
59
71
60
72
mount ( selector , pathName , state ) {
61
73
const target = ( typeof selector ) === 'string' ? document . querySelector ( selector ) : selector
74
+ if ( target === null ) {
75
+ console . warn ( 'Tram-One: could not find target, is the element on the page yet?' )
76
+ }
62
77
if ( ! target . firstElementChild ) {
63
78
const targetChild = document . createElement ( 'div' )
64
79
target . appendChild ( targetChild )
@@ -85,6 +100,11 @@ class Tram {
85
100
}
86
101
87
102
static html ( registry ) {
103
+ if ( registry ) {
104
+ assert . equal ( typeof registry , 'object' , 'Tram-One: registry should be an object' )
105
+ assert . ok ( ! ( registry instanceof Array ) , 'Tram-One: registry should be an object' )
106
+ }
107
+
88
108
return rbelRegister ( belCreateElement , registry || { } )
89
109
}
90
110
}
0 commit comments