1
- const Tram = window [ 'tram-one' ]
2
- const testemPath = window . location . pathname
1
+ const Tram = require ( '../../dist/tram-one.esm' )
2
+
3
+ const isBrowser = typeof window !== 'undefined'
4
+ const testemPath = isBrowser ? window . location . pathname : '/'
5
+ const document = isBrowser ? window . document : require ( 'min-document' )
6
+
7
+ const stringify = ( node ) => {
8
+ if ( node . outerHTML !== undefined ) {
9
+ return node . outerHTML
10
+ }
11
+ return node . toString ( )
12
+ }
3
13
4
14
describe ( 'Tram' , ( ) => {
5
15
let app
@@ -23,22 +33,22 @@ describe('Tram', () => {
23
33
app = new Tram ( )
24
34
25
35
app . addRoute ( '/404' , errorPage )
26
- expect ( app . toString ( '/' ) ) . toEqual ( errorPage ( ) . outerHTML )
36
+ expect ( app . toString ( '/' ) ) . toEqual ( stringify ( errorPage ( ) ) )
27
37
} )
28
38
29
39
it ( 'should take in a default route' , ( ) => {
30
40
app = new Tram ( { defaultRoute : '/200' } )
31
41
32
42
app . addRoute ( '/200' , successPage )
33
- expect ( app . toString ( '/' ) ) . toEqual ( successPage ( ) . outerHTML )
43
+ expect ( app . toString ( '/' ) ) . toEqual ( stringify ( successPage ( ) ) )
34
44
} )
35
45
36
46
it ( 'should not always go to the default' , ( ) => {
37
47
app = new Tram ( )
38
48
39
49
app . addRoute ( '/404' , errorPage )
40
50
app . addRoute ( '/200' , successPage )
41
- expect ( app . toString ( '/200' ) ) . not . toEqual ( errorPage ( ) . outerHTML )
51
+ expect ( app . toString ( '/200' ) ) . not . toEqual ( stringify ( errorPage ( ) ) )
42
52
} )
43
53
} )
44
54
@@ -63,10 +73,10 @@ describe('Tram', () => {
63
73
app . addRoute ( '/good' , successPage )
64
74
app . addRoute ( '/bad' , errorPage )
65
75
app . addRoute ( '/404' , errorPage )
66
- expect ( app . toString ( '/' ) ) . toEqual ( successPage ( ) . outerHTML )
67
- expect ( app . toString ( '/good' ) ) . toEqual ( successPage ( ) . outerHTML )
68
- expect ( app . toString ( '/bad' ) ) . toEqual ( errorPage ( ) . outerHTML )
69
- expect ( app . toString ( '/404' ) ) . toEqual ( errorPage ( ) . outerHTML )
76
+ expect ( app . toString ( '/' ) ) . toEqual ( stringify ( successPage ( ) ) )
77
+ expect ( app . toString ( '/good' ) ) . toEqual ( stringify ( successPage ( ) ) )
78
+ expect ( app . toString ( '/bad' ) ) . toEqual ( stringify ( errorPage ( ) ) )
79
+ expect ( app . toString ( '/404' ) ) . toEqual ( stringify ( errorPage ( ) ) )
70
80
} )
71
81
72
82
it ( 'should include the default state in app' , ( ) => {
@@ -86,6 +96,8 @@ describe('Tram', () => {
86
96
} )
87
97
88
98
describe ( 'start' , ( ) => {
99
+ if ( ! isBrowser ) { return }
100
+
89
101
beforeEach ( ( ) => {
90
102
const childDiv = document . createElement ( 'div' )
91
103
childDiv . id = 'tram_test_container'
@@ -120,6 +132,8 @@ describe('Tram', () => {
120
132
} )
121
133
122
134
describe ( 'mount' , ( ) => {
135
+ if ( ! isBrowser ) { return }
136
+
123
137
beforeEach ( ( ) => {
124
138
const childDiv = document . createElement ( 'div' )
125
139
childDiv . id = 'tram_test_container'
@@ -136,9 +150,9 @@ describe('Tram', () => {
136
150
137
151
app . addRoute ( '/' , queryablePage )
138
152
const target = document . getElementById ( 'tram_test_container' )
139
- app . mount ( target , '/' )
153
+ app . mount ( target , '/' , undefined , document )
140
154
const mountedTarget = document . querySelector ( queryableSelector )
141
- expect ( mountedTarget . outerHTML ) . toEqual ( queryablePage ( ) . outerHTML )
155
+ expect ( mountedTarget . outerHTML ) . toEqual ( stringify ( queryablePage ( ) ) )
142
156
} )
143
157
144
158
it ( 'should use the default route' , ( ) => {
@@ -149,7 +163,7 @@ describe('Tram', () => {
149
163
const target = document . getElementById ( 'tram_test_container' )
150
164
app . mount ( target )
151
165
const mountedTarget = document . querySelector ( queryableSelector )
152
- expect ( mountedTarget . outerHTML ) . toEqual ( queryablePage ( 200 ) . outerHTML )
166
+ expect ( mountedTarget . outerHTML ) . toEqual ( stringify ( queryablePage ( 200 ) ) )
153
167
} )
154
168
155
169
it ( 'should attach the app to a selector' , ( ) => {
@@ -158,7 +172,7 @@ describe('Tram', () => {
158
172
app . addRoute ( '/' , queryablePage )
159
173
app . mount ( '#tram_test_container' , '/' )
160
174
const mountedTarget = document . querySelector ( queryableSelector )
161
- expect ( mountedTarget . outerHTML ) . toEqual ( queryablePage ( ) . outerHTML )
175
+ expect ( mountedTarget . outerHTML ) . toEqual ( stringify ( queryablePage ( ) ) )
162
176
} )
163
177
164
178
it ( 'should update the app on re-mount' , ( ) => {
@@ -169,15 +183,15 @@ describe('Tram', () => {
169
183
app . mount ( '#tram_test_container' , '/' )
170
184
app . mount ( '#tram_test_container' , '/200' )
171
185
const mountedTarget = document . querySelector ( queryableSelector )
172
- expect ( mountedTarget . outerHTML ) . toEqual ( queryablePage ( 200 ) . outerHTML )
186
+ expect ( mountedTarget . outerHTML ) . toEqual ( stringify ( queryablePage ( 200 ) ) )
173
187
} )
174
188
} )
175
189
176
190
describe ( 'toNode' , ( ) => {
177
191
it ( 'should resolve the path' , ( ) => {
178
192
app = new Tram ( )
179
193
app . addRoute ( '/' , successPage )
180
- expect ( app . toNode ( '/' ) . outerHTML ) . toEqual ( successPage ( ) . outerHTML )
194
+ expect ( stringify ( app . toNode ( '/' ) ) ) . toEqual ( stringify ( successPage ( ) ) )
181
195
} )
182
196
183
197
it ( 'should have the default state' , ( ) => {
@@ -198,7 +212,7 @@ describe('Tram', () => {
198
212
it ( 'should return a string' , ( ) => {
199
213
app = new Tram ( )
200
214
app . addRoute ( '/404' , errorPage )
201
- expect ( app . toString ( '/' ) ) . toEqual ( errorPage ( ) . outerHTML )
215
+ expect ( app . toString ( '/' ) ) . toEqual ( stringify ( errorPage ( ) ) )
202
216
} )
203
217
} )
204
218
0 commit comments