test(link): Add unit and integration tests for the link tag

This commit is contained in:
Pixelastic 2018-04-05 12:46:28 +02:00
parent 366c9e10b4
commit 8a34d08dec
9 changed files with 132 additions and 37 deletions

View File

@ -71,13 +71,15 @@ namespace 'test' do
task :watch do task :watch do
puts 'Please, run ./scripts/test_integration_watch manually' puts 'Please, run ./scripts/test_integration_watch manually'
end end
# Delete the test index # Delete the test indices
task :_delete_index do task :_delete_indices do
Algolia.init( Algolia.init(
application_id: ENV['ALGOLIA_APPLICATION_ID'], application_id: ENV['ALGOLIA_APPLICATION_ID'],
api_key: ENV['ALGOLIA_API_KEY'] api_key: ENV['ALGOLIA_API_KEY']
) )
Algolia::Index.new(ENV['ALGOLIA_INDEX_NAME']).delete_index! Algolia::Index.new(ENV['ALGOLIA_INDEX_NAME']).delete_index!
Algolia::Index
.new("#{ENV['ALGOLIA_INDEX_NAME']}_object_ids").delete_index!
end end
# Run only the integration tests # Run only the integration tests
desc '' desc ''

View File

@ -151,7 +151,7 @@ module Jekyll
@original_site_files = { @original_site_files = {
pages: @pages, pages: @pages,
collections: @collections, collections: @collections,
static_files: @static_file static_files: @static_files
} }
@pages = indexable_list(@pages) @pages = indexable_list(@pages)
@ -206,33 +206,3 @@ module Jekyll
end end
end 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

View File

@ -11,3 +11,35 @@ module Jekyll
end end
end 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

View File

@ -20,5 +20,5 @@ bundle install --quiet
bundle exec jekyll build --trace bundle exec jekyll build --trace
rake test:integration:_delete_index rake test:integration:_delete_indices
bundle exec jekyll algolia --trace bundle exec jekyll algolia --trace

View File

@ -17,15 +17,15 @@ describe('pushed index') do
context 'by default' do context 'by default' do
let(:distinct) { nil } let(:distinct) { nil }
it { should eq 33 } it { should eq 35 }
end end
context 'with distinct:true' do context 'with distinct:true' do
let(:distinct) { true } let(:distinct) { true }
it { should eq 33 } it { should eq 35 }
end end
context 'with distinct:false' do context 'with distinct:false' do
let(:distinct) { false } let(:distinct) { false }
it { should eq 65 } it { should eq 72 }
end end
end end

View File

@ -37,6 +37,13 @@ describe(Jekyll::Algolia) do
it 'should check for deprecation warnings' do it 'should check for deprecation warnings' do
expect(configurator).to receive(:warn_of_deprecated_options) 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) current.init(config)
end end
end end

View 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
View 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 %}

View File

@ -0,0 +1,5 @@
---
title: This is a page in a subdirectory
---
This page is in a subdirectory.