discourse/spec/components/oneboxer_spec.rb

177 lines
4.7 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require 'spec_helper'
require 'oneboxer'
2013-02-26 00:42:20 +08:00
describe "Dynamic Oneboxer" do
2013-02-06 13:22:11 +08:00
class DummyDynamicOnebox < Oneboxer::BaseOnebox
2013-02-26 00:42:20 +08:00
matcher do
2013-02-06 13:22:11 +08:00
/^https?:\/\/dummy2.localhost/
end
2013-02-26 00:42:20 +08:00
2013-02-06 13:22:11 +08:00
def onebox
"dummy2!"
end
end
before do
Oneboxer.add_onebox DummyDynamicOnebox
@dummy_onebox_url = "http://dummy2.localhost/dummy-object"
end
2013-03-02 07:48:49 +08:00
context 'find onebox for url' do
2013-02-06 13:22:11 +08:00
2013-03-02 07:48:49 +08:00
it 'returns blank with an unknown url' do
Oneboxer.onebox_for_url('http://asdfasdfasdfasdf.asdf').should be_blank
end
2013-02-06 13:22:11 +08:00
2013-03-02 07:48:49 +08:00
it 'returns something when matched' do
Oneboxer.onebox_for_url(@dummy_onebox_url).should be_present
end
2013-02-06 13:22:11 +08:00
2013-03-02 07:48:49 +08:00
it 'returns an instance of our class when matched' do
Oneboxer.onebox_for_url(@dummy_onebox_url).kind_of?(DummyDynamicOnebox).should be_true
end
2013-02-06 13:22:11 +08:00
2013-03-02 07:48:49 +08:00
end
2013-02-06 13:22:11 +08:00
end
2013-02-06 03:16:51 +08:00
describe Oneboxer do
# A class to help us test
class DummyOnebox < Oneboxer::BaseOnebox
matcher /^https?:\/\/dummy.localhost/
def onebox
"dummy!"
end
end
let(:dummy_onebox_url) { "http://dummy.localhost/dummy-object" }
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
before do
Oneboxer.add_onebox DummyOnebox
end
it 'should have matchers set up by default' do
Oneboxer.matchers.should be_present
end
context 'caching' do
let(:result) { "onebox result string" }
context "with invalidate_oneboxes true" do
it "invalidates the url" do
Oneboxer.expects(:invalidate).with(dummy_onebox_url)
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: true)
end
it "doesn't render from cache" do
Oneboxer.expects(:render_from_cache).never
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: true)
end
it "calls fetch and cache" do
Oneboxer.expects(:fetch_and_cache).returns(result)
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: true).should == result
end
end
context 'with invalidate_oneboxes false' do
it "doesn't invalidate the url" do
Oneboxer.expects(:invalidate).with(dummy_onebox_url).never
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: false)
end
it "returns render_from_cache if present" do
Oneboxer.expects(:render_from_cache).with(dummy_onebox_url).returns(result)
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: false).should == result
end
it "doesn't call fetch_and_cache" do
Oneboxer.expects(:render_from_cache).with(dummy_onebox_url).returns(result)
Oneboxer.expects(:fetch_and_cache).never
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: false)
end
it "calls fetch_and_cache if render from cache is blank" do
Oneboxer.stubs(:render_from_cache)
Oneboxer.expects(:fetch_and_cache).returns(result)
Oneboxer.onebox(dummy_onebox_url, invalidate_oneboxes: false).should == result
end
end
end
2013-02-06 03:16:51 +08:00
context 'find onebox for url' do
it 'returns blank with an unknown url' do
Oneboxer.onebox_for_url('http://asdfasdfasdfasdf.asdf').should be_blank
end
it 'returns something when matched' do
Oneboxer.onebox_for_url(dummy_onebox_url).should be_present
2013-02-06 03:16:51 +08:00
end
it 'returns an instance of our class when matched' do
Oneboxer.onebox_for_url(dummy_onebox_url).kind_of?(DummyOnebox).should be_true
2013-02-06 03:16:51 +08:00
end
end
2013-03-02 07:46:55 +08:00
describe '#nice_host' do
it 'strips www from the domain' do
DummyOnebox.new('http://www.cnn.com/thing').nice_host.should eq 'cnn.com'
end
it 'respects double TLDs' do
DummyOnebox.new('http://news.bbc.co.uk/thing').nice_host.should eq 'news.bbc.co.uk'
end
it 'returns an empty string if the URL is bogus' do
DummyOnebox.new('whatever').nice_host.should eq ''
end
it 'returns an empty string if the URL unparsable' do
DummyOnebox.new(nil).nice_host.should eq ''
end
end
2013-02-26 00:42:20 +08:00
context 'without caching' do
2013-02-06 03:16:51 +08:00
it 'calls the onebox method of our matched class' do
Oneboxer.onebox_nocache(dummy_onebox_url).should == 'dummy!'
2013-02-06 03:16:51 +08:00
end
end
context 'each_onebox_link' do
before do
@html = "<a href='http://discourse.org' class='onebox'>Discourse Link</a>"
end
it 'yields each url and element when given a string' do
result = Oneboxer.each_onebox_link(@html) do |url, element|
2013-02-12 22:46:45 +08:00
element.is_a?(Nokogiri::XML::Element).should be_true
2013-02-06 03:16:51 +08:00
url.should == 'http://discourse.org'
end
2013-02-12 22:46:45 +08:00
result.kind_of?(Nokogiri::HTML::Document).should be_true
2013-02-06 03:16:51 +08:00
end
it 'yields each url and element when given a doc' do
2013-02-12 22:46:45 +08:00
doc = Nokogiri::HTML(@html)
2013-02-06 03:16:51 +08:00
Oneboxer.each_onebox_link(doc) do |url, element|
2013-02-12 22:46:45 +08:00
element.is_a?(Nokogiri::XML::Element).should be_true
2013-02-06 03:16:51 +08:00
url.should == 'http://discourse.org'
end
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
end