From efbba9d6b3002250e6b2301a90008d96a9cc0486 Mon Sep 17 00:00:00 2001 From: kbparagua Date: Sat, 12 Oct 2013 16:56:45 +0800 Subject: [PATCH] Jasmine spec for Router --- test_app/config/routes.rb | 55 +------- test_app/spec/javascripts/router_spec.js | 123 ++++++++++++++++++ test_app/spec/javascripts/support/jasmine.yml | 20 +++ 3 files changed, 144 insertions(+), 54 deletions(-) create mode 100644 test_app/spec/javascripts/router_spec.js create mode 100644 test_app/spec/javascripts/support/jasmine.yml diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb index 09a54ea..a0d4890 100644 --- a/test_app/config/routes.rb +++ b/test_app/config/routes.rb @@ -1,59 +1,6 @@ TestApp::Application.routes.draw do - # The priority is based upon order of creation: - # first created -> highest priority. + mount JasmineRails::Engine => "/specs" if defined?(JasmineRails) - # Sample of regular route: - # match 'products/:id' => 'catalog#view' - # Keep in mind you can assign values other than :controller and :action - - # Sample of named route: - # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase - # This route can be invoked with purchase_url(:id => product.id) - - # Sample resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Sample resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Sample resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Sample resource route with more complex sub-resources - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', :on => :collection - # end - # end - - # Sample resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end - - # You can have the root of your site routed with "root" - # just remember to delete public/index.html. root :to => 'main#index' - - # See how all your routes lay out with "rake routes" - - # This is a legacy wild controller route that's not recommended for RESTful applications. - # Note: This route will make all actions in every controller accessible via GET requests. - # match ':controller(/:action(/:id))(.:format)' end diff --git a/test_app/spec/javascripts/router_spec.js b/test_app/spec/javascripts/router_spec.js new file mode 100644 index 0000000..de50d20 --- /dev/null +++ b/test_app/spec/javascripts/router_spec.js @@ -0,0 +1,123 @@ + +describe('Paloma.Router', function(){ + var delimiter = {namespace: '/', action: '#'}, + router = new Paloma.Router(delimiter); + + describe('#parse(path)', function(){ + + describe('when path has a namespace', function(){ + var result; + + beforeEach(function(){ + result = router.parse('Foo/Bar/Baz/Controller#action'); + }); + + it('returns the array of the namespaces', function(){ + expect(result.namespaces).toEqual(['Foo', 'Bar', 'Baz']); + }); + + it('returns the controller', function(){ + expect(result.controller).toEqual('Controller'); + }); + + it('returns the action', function(){ + expect(result.action).toEqual('action'); + }); + + it('returns the controllerPath', function(){ + expect(result.controllerPath).toEqual(['Foo', 'Bar', 'Baz', 'Controller']); + }); + }); + + + + describe('when path has no namespace', function(){ + var result; + + beforeEach(function(){ + result = router.parse('Controller#action'); + }); + + it('returns an empty array of namespaces', function(){ + expect(result.namespaces).toEqual([]); + }); + + it('returns the controller', function(){ + expect(result.controller).toEqual('Controller'); + }); + + it('returns the action', function(){ + expect(result.action).toEqual('action'); + }); + + it('returns the controllerPath', function(){ + expect(result.controllerPath).toEqual(['Controller']); + }); + }); + + }); + + + + + + describe('#controllerFor(resource)', function(){ + + var resource = 'MySuperResource'; + + describe('when no route is found', function(){ + it('returns the resource', function(){ + var controller = router.controllerFor(resource); + expect(controller).toEqual(resource); + }); + }); + + + describe('when route is found', function(){ + it('returns the set controller', function(){ + router.resource(resource, {controller: 'MyController'}); + + var controller = router.controllerFor(resource); + expect(controller).toEqual('MyController'); + }); + }); + }); + + + + + + describe('#redirectFor(resource, action)', function(){ + var router = new Paloma.Router(delimiter), + result; + + describe('when has a redirect', function(){ + beforeEach(function(){ + router.redirect('Foo#edit', {to: 'Bar#revise'}); + result = router.redirectFor('Foo', 'edit'); + }); + + it('returns controller of the redirect', function(){ + expect(result.controller).toEqual('Bar'); + }); + + it('returns action of the redirect', function(){ + expect(result.action).toEqual('revise'); + }); + + router.reset(); + }); + + + describe('when has no redirect', function(){ + var router = new Paloma.Router(delimiter), + result = router.redirectFor('Foo', 'edit'); + + it('returns null', function(){ + expect(result).toBeNull(); + }); + }); + + }); + +}); \ No newline at end of file diff --git a/test_app/spec/javascripts/support/jasmine.yml b/test_app/spec/javascripts/support/jasmine.yml new file mode 100644 index 0000000..c4da6dd --- /dev/null +++ b/test_app/spec/javascripts/support/jasmine.yml @@ -0,0 +1,20 @@ +# path to parent directory of src_files +# relative path from Rails.root +# defaults to app/assets/javascripts +src_dir: "app/assets/javascripts" + # list of file expressions to include as source files +# relative path from scr_dir +src_files: + - "application.{js,coffee}" + # path to parent directory of spec_files +# relative path from Rails.root +# defaults to spec/javascripts +spec_dir: spec/javascripts + # list of file expressions to include as helpers into spec runner +# relative path from spec_dir +helpers: + - "helpers/**/*.{js,coffee}" + # list of file expressions to include as specs into spec runner +# relative path from spec_dir +spec_files: + - "**/*[Ss]pec.{js,coffee}"