@leansdk/leanrc
Version:
LeanRC is a MVC framework for creating graceful applications
233 lines (220 loc) • 8.67 kB
text/coffeescript
{ assert } = require 'chai'
sinon = require 'sinon'
_ = require 'lodash'
LeanRC = require.main.require 'lib'
{
Router
Utils: { co }
} = LeanRC::
describe 'Router', ->
describe '.new, .map, #map, #resource, #namespace, #routes', ->
it 'should create new router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test2'
'sub2', ->
'subtest2'
Test::TestRouter.initialize()
router = Test::TestRouter.new 'TEST_ROUTER'
assert.lengthOf router.routes, 15, 'Routes did not initialized'
yield return
describe '#defineMethod', ->
it 'should define methods for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2'
[], 'get', '/get', resource: 'test2'
[], 'post', '/post', resource: 'test2'
[], 'put', '/put', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 3, 'Methods did not defined'
yield return
it 'should define `get` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test3', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 1, 'Methods did not defined'
yield return
describe '#post', ->
it 'should define `post` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test3', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 1, 'Methods did not defined'
yield return
describe '#put', ->
it 'should define `put` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test3', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 1, 'Methods did not defined'
yield return
describe '#patch', ->
it 'should define `patch` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test3', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 1, 'Methods did not defined'
yield return
describe '#delete', ->
it 'should define `delete` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', ->
'test3', resource: 'test2'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.equal spyDefineMethod.callCount, 1, 'Methods did not defined'
yield return
describe '#member', ->
it 'should define `member` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', except: 'patch', ->
->
'test4'
'test5'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.lengthOf router.routes, 7, 'Methods did not defined'
yield return
describe '#collection', ->
it 'should define `collection` method for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'test2', except: 'patch', ->
->
'test4'
'test5'
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
assert.lengthOf router.routes, 7, 'Methods did not defined'
yield return
describe 'complex test', ->
it 'should define all methods for router', ->
co ->
class Test extends LeanRC
Test.initialize()
class Test::TestRouter extends LeanRC::Router
Test
->
'/auth/signin', to: 'auth#signin'
'users', except: 'delete', ->
->
'verify'
->
'today_watches'
'descendants', only: 'list', above:
entityName: 'user'
recordName: 'user'
, ->
'count', at: 'collection'
'invitations', except: 'delete', ->
'confirm', at: 'collection'
'spaces', resource: 'areas'
'version', module: '', prefix: ':v', ->
'space', module: '', prefix: ':space', ->
'uploads', ->
'add_attachment', at: 'member', entityName: 'attachment'
'add_test', at: 'member', entityName: 'test_entity', recordName: 'test', keyName: 'test'
'cucumbers', only: ['list', 'detail']
'admin', ->
'tomatos', except: ['delete']
'test', module: '', ->
'tomatos', except: ['delete']
'super', module: '', prefix: '', ->
'tomatos', except: ['delete']
'cucumber', prefix: '', ->
'tomatos', except: ['delete']
'cucumber', prefix: 'onion', ->
'tomatos', except: ['delete']
'carrot', prefix: 'onion', ->
'tomatos', except: ['delete']
Test::TestRouter.initialize()
spyDefineMethod = sinon.spy Test::TestRouter::, 'defineMethod'
router = Test::TestRouter.new 'TEST_ROUTER'
keys = require.main.require 'test/integration/complex-router/router.json'
{ routes } = router
# console.log 'ROUTES:', routes
# console.log 'KEYS:', keys
for key in keys
assert.isDefined _.find(routes, key), "Cannot find route #{JSON.stringify key}"
yield return