test(link): Add unit and integration tests for the link tag
This commit is contained in:
parent
366c9e10b4
commit
8a34d08dec
6
Rakefile
6
Rakefile
@ -71,13 +71,15 @@ namespace 'test' do
|
||||
task :watch do
|
||||
puts 'Please, run ./scripts/test_integration_watch manually'
|
||||
end
|
||||
# Delete the test index
|
||||
task :_delete_index do
|
||||
# Delete the test indices
|
||||
task :_delete_indices do
|
||||
Algolia.init(
|
||||
application_id: ENV['ALGOLIA_APPLICATION_ID'],
|
||||
api_key: ENV['ALGOLIA_API_KEY']
|
||||
)
|
||||
Algolia::Index.new(ENV['ALGOLIA_INDEX_NAME']).delete_index!
|
||||
Algolia::Index
|
||||
.new("#{ENV['ALGOLIA_INDEX_NAME']}_object_ids").delete_index!
|
||||
end
|
||||
# Run only the integration tests
|
||||
desc ''
|
||||
|
||||
@ -151,7 +151,7 @@ module Jekyll
|
||||
@original_site_files = {
|
||||
pages: @pages,
|
||||
collections: @collections,
|
||||
static_files: @static_file
|
||||
static_files: @static_files
|
||||
}
|
||||
|
||||
@pages = indexable_list(@pages)
|
||||
@ -206,33 +206,3 @@ module Jekyll
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The default `link` tag allow to link to a specific page, using its relative
|
||||
# path. Because we might not be indexing the destination of the link, we might
|
||||
# not have the representation of the page in our data. If that happens, the
|
||||
# `link` tag fails.
|
||||
#
|
||||
# To fix that we'll overwrite the default `link` tag to loop over a backup copy
|
||||
# of the original files (before we clean it for indexing)
|
||||
class JekyllAlgoliaLink < Jekyll::Tags::Link
|
||||
def render(context)
|
||||
original_files = context.registers[:site].original_site_files
|
||||
|
||||
original_files[:pages].each do |page|
|
||||
return page.url if page.relative_path == @relative_path
|
||||
end
|
||||
|
||||
original_files[:collections].each do |collection|
|
||||
collection.each do |item|
|
||||
return item.url if item.relative_path == @relative_path
|
||||
end
|
||||
end
|
||||
|
||||
original_files[:static_files].each do |asset|
|
||||
return asset.url if asset.relative_path == @relative_path
|
||||
return asset.url if asset.relative_path == "/#{@relative_path}"
|
||||
end
|
||||
|
||||
'/'
|
||||
end
|
||||
end
|
||||
|
||||
@ -11,3 +11,35 @@ module Jekyll
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The default `link` tag allow to link to a specific page, using its relative
|
||||
# path. Because we might not be indexing the destination of the link, we might
|
||||
# not have the representation of the page in our data. If that happens, the
|
||||
# `link` tag fails.
|
||||
#
|
||||
# To fix that we'll overwrite the default `link` tag to loop over a backup copy
|
||||
# of the original files (before we clean it for indexing)
|
||||
#
|
||||
# https://github.com/algolia/jekyll-algolia/issues/62
|
||||
class JekyllAlgoliaLink < Jekyll::Tags::Link
|
||||
def render(context)
|
||||
original_files = context.registers[:site].original_site_files
|
||||
|
||||
original_files[:pages].each do |page|
|
||||
return page.url if page.relative_path == @relative_path
|
||||
end
|
||||
|
||||
original_files[:collections].each_value do |collection|
|
||||
collection.docs.each do |item|
|
||||
return item.url if item.relative_path == @relative_path
|
||||
end
|
||||
end
|
||||
|
||||
original_files[:static_files].each do |asset|
|
||||
return asset.url if asset.relative_path == @relative_path
|
||||
return asset.url if asset.relative_path == "/#{@relative_path}"
|
||||
end
|
||||
|
||||
'/'
|
||||
end
|
||||
end
|
||||
|
||||
@ -20,5 +20,5 @@ bundle install --quiet
|
||||
|
||||
bundle exec jekyll build --trace
|
||||
|
||||
rake test:integration:_delete_index
|
||||
rake test:integration:_delete_indices
|
||||
bundle exec jekyll algolia --trace
|
||||
|
||||
@ -17,15 +17,15 @@ describe('pushed index') do
|
||||
|
||||
context 'by default' do
|
||||
let(:distinct) { nil }
|
||||
it { should eq 33 }
|
||||
it { should eq 35 }
|
||||
end
|
||||
context 'with distinct:true' do
|
||||
let(:distinct) { true }
|
||||
it { should eq 33 }
|
||||
it { should eq 35 }
|
||||
end
|
||||
context 'with distinct:false' do
|
||||
let(:distinct) { false }
|
||||
it { should eq 65 }
|
||||
it { should eq 72 }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -37,6 +37,13 @@ describe(Jekyll::Algolia) do
|
||||
it 'should check for deprecation warnings' do
|
||||
expect(configurator).to receive(:warn_of_deprecated_options)
|
||||
|
||||
current.init(config)
|
||||
end
|
||||
it 'should register the custom link tag' do
|
||||
expect(Liquid::Template)
|
||||
.to receive(:register_tag)
|
||||
.with('link', JekyllAlgoliaLink)
|
||||
|
||||
current.init(config)
|
||||
end
|
||||
end
|
||||
|
||||
63
spec/jekyll/algolia/overwrites_spec.rb
Normal file
63
spec/jekyll/algolia/overwrites_spec.rb
Normal file
@ -0,0 +1,63 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'overwrites' do
|
||||
let(:site) { init_new_jekyll_site }
|
||||
before do
|
||||
allow(Jekyll.logger).to receive(:info)
|
||||
allow(Jekyll.logger).to receive(:warn)
|
||||
end
|
||||
|
||||
describe(Jekyll::Document) do
|
||||
let(:current) { site.__find_file('collection-item.md') }
|
||||
|
||||
subject { current.date }
|
||||
|
||||
before do
|
||||
allow(current).to receive(:data).and_return(data)
|
||||
end
|
||||
|
||||
describe 'return the date if we have it' do
|
||||
let(:data) { { 'date' => 'my_date' } }
|
||||
it { should eq 'my_date' }
|
||||
end
|
||||
|
||||
describe 'return nil if no date defined' do
|
||||
let(:data) { {} }
|
||||
it { should eq nil }
|
||||
end
|
||||
end
|
||||
|
||||
describe(JekyllAlgoliaLink) do
|
||||
let(:current) { site.__find_file('links.md') }
|
||||
|
||||
subject { current.content }
|
||||
|
||||
describe 'should link to a page' do
|
||||
it { should include('page: /about.html') }
|
||||
end
|
||||
|
||||
describe 'should link to a page in a subdir' do
|
||||
it { should include('page in subdir: /subdir/subpage.html') }
|
||||
end
|
||||
|
||||
describe 'should link to file excluded from indexing' do
|
||||
it { should include('excluded page: /excluded.html') }
|
||||
end
|
||||
|
||||
describe 'should link to an asset' do
|
||||
it { should include('asset: /assets/ring.png') }
|
||||
end
|
||||
|
||||
describe 'should link to a blog post' do
|
||||
it { should include('blog post: /foo/bar/2015/07/02/test-post.html') }
|
||||
end
|
||||
|
||||
describe 'should link to collection item' do
|
||||
it do
|
||||
should include('collection item: /my-collection/collection-item.html')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
16
spec/site/links.md
Normal file
16
spec/site/links.md
Normal file
@ -0,0 +1,16 @@
|
||||
---
|
||||
title: A page with links to other pages
|
||||
---
|
||||
|
||||
page: {% link about.md %}
|
||||
|
||||
page in subdir: {% link subdir/subpage.md %}
|
||||
|
||||
excluded page: {% link excluded.html %}
|
||||
|
||||
asset: {% link assets/ring.png %}
|
||||
|
||||
blog post: {% link _posts/2015-07-02-test-post.md %}
|
||||
|
||||
collection item: {% link _my-collection/collection-item.md %}
|
||||
|
||||
5
spec/site/subdir/subpage.md
Normal file
5
spec/site/subdir/subpage.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
title: This is a page in a subdirectory
|
||||
---
|
||||
|
||||
This page is in a subdirectory.
|
||||
Loading…
x
Reference in New Issue
Block a user