1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
require 'spec_helper'
require "fileutils"
describe JsRoutes do
before(:each) do
evaljs(JsRoutes.generate)
end
describe "generated js" do
subject { JsRoutes.generate }
it "should include a comment in the header" do
app_class = "App"
is_expected.to include("File generated by js-routes #{JsRoutes::VERSION}")
is_expected.to include("Based on Rails #{ActionPack.version} routes of #{app_class}")
end
it "should call route function for each route" do
is_expected.to include("inboxes_path: Utils.route(")
end
it "should have correct function without arguments signature" do
is_expected.to include("inboxes_path: Utils.route([[\"format\",false]]")
end
it "should have correct function with arguments signature" do
is_expected.to include("inbox_message_path: Utils.route([[\"inbox_id\",true],[\"id\",true],[\"format\",false]]")
end
it "should have correct function signature with unordered hash" do
is_expected.to include("inbox_message_attachment_path: Utils.route([[\"inbox_id\",true],[\"message_id\",true],[\"id\",true],[\"format\",false]]")
end
it "should have correct function comment with options argument" do
is_expected.to include("// function(options)\n inboxes_path: Utils.route")
end
it "should have correct function comment with arguments" do
is_expected.to include("// function(inbox_id, message_id, options)\n new_inbox_message_attachment_path: Utils.route")
end
it "routes should be sorted in alphabetical order" do
expect(subject.index("book_path")).to be <= subject.index("inboxes_path")
end
end
describe ".generate!" do
let(:name) { Rails.root.join('app', 'assets', 'javascripts', 'routes.js') }
before(:each) do
FileUtils.rm_f(name)
JsRoutes.generate!({:file => name})
end
after(:each) do
FileUtils.rm_f(name)
end
after(:all) do
FileUtils.rm_f("#{File.dirname(__FILE__)}/../routes.js") # let(:name) is not available here
end
it "should not generate file before initialization" do
# This method is alread fixed in Rails master
# But in 3.2 stable we need to hack it like this
if Rails.application.instance_variable_get("@initialized")
pending
end
expect(File.exists?(name)).to be_falsey
end
end
describe "compiled javascript asset" do
subject { ERB.new(File.read("app/assets/javascripts/js-routes.js.erb")).result(binding) }
it "should have js routes code" do
is_expected.to include("inbox_message_path: Utils.route([[\"inbox_id\",true],[\"id\",true],[\"format\",false]]")
end
end
end
|