Skip to content

Commit 4d86931

Browse files
authored
Make methods Chainable ⛓, Release 1.2.0 (#17)
1 parent 1841f3f commit 4d86931

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tram-one",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "🚋 Batteries Included View Framework",
55
"main": "dist/tram-one.esm.js",
66
"browser": "dist/tram-one.umd.js",

tests/specs/tram-spec.js

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const stringify = (node) => {
1212
}
1313

1414
describe('Tram', () => {
15-
let app
1615
const errorPage = () => Tram.html()`<div>Error</div>`
1716
const successPage = () => Tram.html()`<div>Noraml Page</div>`
1817
const queryablePage = (value) => Tram.html()`<div id="tram_container">${value}</div>`
@@ -30,21 +29,21 @@ describe('Tram', () => {
3029

3130
describe('constructor', () => {
3231
it('should have a default route', () => {
33-
app = new Tram()
32+
const app = new Tram()
3433

3534
app.addRoute('/404', errorPage)
3635
expect(app.toString('/')).toEqual(stringify(errorPage()))
3736
})
3837

3938
it('should take in a default route', () => {
40-
app = new Tram({defaultRoute: '/200'})
39+
const app = new Tram({defaultRoute: '/200'})
4140

4241
app.addRoute('/200', successPage)
4342
expect(app.toString('/')).toEqual(stringify(successPage()))
4443
})
4544

4645
it('should not always go to the default', () => {
47-
app = new Tram()
46+
const app = new Tram()
4847

4948
app.addRoute('/404', errorPage)
5049
app.addRoute('/200', successPage)
@@ -54,21 +53,30 @@ describe('Tram', () => {
5453

5554
describe('addReducer', () => {
5655
it('should include reducer in app', () => {
57-
app = new Tram()
56+
const app = new Tram()
5857
app.addReducer('counter', counterReducer, {})
5958
expect(app.reducers['counter']).toEqual(counterReducer)
6059
})
6160

6261
it('should include state in app', () => {
63-
app = new Tram()
62+
const app = new Tram()
6463
app.addReducer('counter', counterReducer, counterState)
6564
expect(app.state['counter']).toEqual(counterState)
6665
})
66+
67+
it('should be chainable', () => {
68+
const app = new Tram()
69+
.addReducer('counter', counterReducer, counterState)
70+
.addReducer('counter2', counterReducer, counterState)
71+
72+
expect(app.state['counter']).toEqual(counterState)
73+
expect(app.state['counter2']).toEqual(counterState)
74+
})
6775
})
6876

6977
describe('addRoute', () => {
7078
it('should handle new routes in the app', () => {
71-
app = new Tram()
79+
const app = new Tram()
7280
app.addRoute('/', successPage)
7381
app.addRoute('/good', successPage)
7482
app.addRoute('/bad', errorPage)
@@ -80,19 +88,28 @@ describe('Tram', () => {
8088
})
8189

8290
it('should include the default state in app', () => {
83-
app = new Tram()
91+
const app = new Tram()
8492
app.addRoute('/', counterPage)
8593
app.addReducer('counter', counterReducer, counterState)
8694
expect(app.toNode('/')).toEqual(counterState)
8795
})
8896

8997
it('should pass in path params in app', () => {
90-
app = new Tram()
98+
const app = new Tram()
9199
app.addRoute('/:path_param',
92100
(state) => Tram.html()`${state.path_param}`
93101
)
94102
expect(app.toNode('/foo')).toEqual('foo')
95103
})
104+
105+
it('should be chainable', () => {
106+
const app = new Tram()
107+
.addRoute('/good', successPage)
108+
.addRoute('/bad', errorPage)
109+
110+
expect(app.toString('/good')).toEqual(stringify(successPage()))
111+
expect(app.toString('/bad')).toEqual(stringify(errorPage()))
112+
})
96113
})
97114

98115
describe('start', () => {
@@ -110,7 +127,7 @@ describe('Tram', () => {
110127
})
111128

112129
it('should mount the app to the target', () => {
113-
app = new Tram()
130+
const app = new Tram()
114131
app.addReducer('counter', counterReducer, counterState)
115132
app.addRoute(testemPath, queryableCounterPage)
116133
app.start('#tram_test_container')
@@ -120,7 +137,7 @@ describe('Tram', () => {
120137
})
121138

122139
it('should update the app on state change', () => {
123-
app = new Tram()
140+
const app = new Tram()
124141
app.addReducer('counter', counterReducer, counterState)
125142
app.addRoute(testemPath, queryableCounterPage)
126143
app.start('#tram_test_container')
@@ -129,6 +146,17 @@ describe('Tram', () => {
129146

130147
expect(mountedTarget.innerHTML).toEqual('3')
131148
})
149+
150+
it('should be chainable', () => {
151+
const pageNode = new Tram()
152+
.addRoute(testemPath, queryablePage.bind(this, 5))
153+
.start('#tram_test_container')
154+
.toNode(testemPath)
155+
156+
const mountedTarget = document.querySelector(queryableSelector)
157+
expect(mountedTarget.innerHTML).toEqual('5')
158+
expect(pageNode.innerHTML).toEqual('5')
159+
})
132160
})
133161

134162
describe('mount', () => {
@@ -146,7 +174,7 @@ describe('Tram', () => {
146174
})
147175

148176
it('should attach the app to a node', () => {
149-
app = new Tram()
177+
const app = new Tram()
150178

151179
app.addRoute('/', queryablePage)
152180
const target = document.getElementById('tram_test_container')
@@ -156,7 +184,7 @@ describe('Tram', () => {
156184
})
157185

158186
it('should use the default route', () => {
159-
app = new Tram()
187+
const app = new Tram()
160188

161189
app.addRoute('/', queryablePage)
162190
app.addRoute(testemPath, queryablePage.bind(this, 200))
@@ -167,7 +195,7 @@ describe('Tram', () => {
167195
})
168196

169197
it('should attach the app to a selector', () => {
170-
app = new Tram()
198+
const app = new Tram()
171199

172200
app.addRoute('/', queryablePage)
173201
app.mount('#tram_test_container', '/')
@@ -176,7 +204,7 @@ describe('Tram', () => {
176204
})
177205

178206
it('should update the app on re-mount', () => {
179-
app = new Tram()
207+
const app = new Tram()
180208

181209
app.addRoute('/', queryablePage)
182210
app.addRoute('/200', queryablePage.bind(this, 200))
@@ -185,32 +213,43 @@ describe('Tram', () => {
185213
const mountedTarget = document.querySelector(queryableSelector)
186214
expect(mountedTarget.outerHTML).toEqual(stringify(queryablePage(200)))
187215
})
216+
217+
it('should be chainable', () => {
218+
const pageNode = new Tram()
219+
.addRoute('/', queryablePage.bind(this, 5))
220+
.mount('#tram_test_container', '/')
221+
.toNode('/')
222+
223+
const mountedTarget = document.querySelector(queryableSelector)
224+
expect(mountedTarget.innerHTML).toEqual('5')
225+
expect(pageNode.innerHTML).toEqual('5')
226+
})
188227
})
189228

190229
describe('toNode', () => {
191230
it('should resolve the path', () => {
192-
app = new Tram()
231+
const app = new Tram()
193232
app.addRoute('/', successPage)
194233
expect(stringify(app.toNode('/'))).toEqual(stringify(successPage()))
195234
})
196235

197236
it('should have the default state', () => {
198-
app = new Tram()
237+
const app = new Tram()
199238
app.addRoute('/', counterPage)
200239
app.addReducer('counter', counterReducer, counterState)
201240
expect(app.toNode('/')).toEqual(counterState)
202241
})
203242

204243
it('should take in a state', () => {
205-
app = new Tram()
244+
const app = new Tram()
206245
app.addRoute('/', counterPage)
207246
expect(app.toNode('/', {counter: counterState})).toEqual(counterState)
208247
})
209248
})
210249

211250
describe('toString', () => {
212251
it('should return a string', () => {
213-
app = new Tram()
252+
const app = new Tram()
214253
app.addRoute('/404', errorPage)
215254
expect(app.toString('/')).toEqual(stringify(errorPage()))
216255
})

tram-one.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Tram {
1919
addReducer(key, reducer, state) {
2020
this.reducers[key] = reducer
2121
this.state[key] = state
22+
23+
return this
2224
}
2325

2426
addRoute(path, page) {
@@ -29,6 +31,8 @@ class Tram {
2931
)
3032
return page(completeState)
3133
})
34+
35+
return this
3236
}
3337

3438
start(selector, pathName) {
@@ -40,6 +44,8 @@ class Tram {
4044
})
4145

4246
this.mount(selector, pathName, this.store.getState())
47+
48+
return this
4349
}
4450

4551
mount(selector, pathName, state) {
@@ -52,6 +58,8 @@ class Tram {
5258

5359
const routePath = pathName || window.location.href.replace(window.location.origin, '')
5460
yoyoUpdate(targetChild, this.toNode(routePath, state))
61+
62+
return this
5563
}
5664

5765
toNode(pathName, state) {

0 commit comments

Comments
 (0)