discourse/spec/models/permalink_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

82 lines
2.4 KiB
Ruby

require "rails_helper"
describe Permalink do
describe "normalization" do
it "correctly normalizes" do
normalizer = Permalink::Normalizer.new("/(\\/hello.*)\\?.*/\\1|/(\\/bye.*)\\?.*/\\1")
expect(normalizer.normalize("/hello?a=1")).to eq("/hello")
expect(normalizer.normalize("/bye?test=1")).to eq("/bye")
expect(normalizer.normalize("/bla?a=1")).to eq("/bla?a=1")
end
end
describe "new record" do
it "strips blanks" do
permalink = described_class.create(url: " my/old/url ")
expect(permalink.url).to eq("my/old/url")
end
it "removes leading slash" do
permalink = described_class.create(url: "/my/old/url")
expect(permalink.url).to eq("my/old/url")
end
end
describe "target_url" do
let(:permalink) { Fabricate.build(:permalink) }
let(:topic) { Fabricate(:topic) }
let(:post) { Fabricate(:post, topic: topic) }
let(:category) { Fabricate(:category) }
subject(:target_url) { permalink.target_url }
it "returns a topic url when topic_id is set" do
permalink.topic_id = topic.id
expect(target_url).to eq(topic.relative_url)
end
it "returns nil when topic_id is set but topic is not found" do
permalink.topic_id = 99999
expect(target_url).to eq(nil)
end
it "returns a post url when post_id is set" do
permalink.post_id = post.id
expect(target_url).to eq(post.url)
end
it "returns nil when post_id is set but post is not found" do
permalink.post_id = 99999
expect(target_url).to eq(nil)
end
it "returns a post url when post_id and topic_id are both set" do
permalink.post_id = post.id
permalink.topic_id = topic.id
expect(target_url).to eq(post.url)
end
it "returns a category url when category_id is set" do
permalink.category_id = category.id
expect(target_url).to eq(category.url)
end
it "returns nil when category_id is set but category is not found" do
permalink.category_id = 99999
expect(target_url).to eq(nil)
end
it "returns a post url when topic_id, post_id, and category_id are all set for some reason" do
permalink.post_id = post.id
permalink.topic_id = topic.id
permalink.category_id = category.id
expect(target_url).to eq(post.url)
end
it "returns nil when nothing is set" do
expect(target_url).to eq(nil)
end
end
end