From 8a34d08decb2bc27fbaa868154678f5aba6313ef Mon Sep 17 00:00:00 2001 From: Pixelastic Date: Thu, 5 Apr 2018 12:46:28 +0200 Subject: [PATCH] test(link): Add unit and integration tests for the link tag --- Rakefile | 6 ++- lib/jekyll-algolia.rb | 32 +------------ lib/jekyll/algolia/overwrites.rb | 32 +++++++++++++ scripts/test_integration_prepare | 2 +- spec/integration/main_spec.rb | 6 +-- spec/jekyll-algolia_spec.rb | 7 +++ spec/jekyll/algolia/overwrites_spec.rb | 63 ++++++++++++++++++++++++++ spec/site/links.md | 16 +++++++ spec/site/subdir/subpage.md | 5 ++ 9 files changed, 132 insertions(+), 37 deletions(-) create mode 100644 spec/jekyll/algolia/overwrites_spec.rb create mode 100644 spec/site/links.md create mode 100644 spec/site/subdir/subpage.md diff --git a/Rakefile b/Rakefile index 88ade7e..3258d6a 100644 --- a/Rakefile +++ b/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 '' diff --git a/lib/jekyll-algolia.rb b/lib/jekyll-algolia.rb index 5cec9d6..d6b480f 100644 --- a/lib/jekyll-algolia.rb +++ b/lib/jekyll-algolia.rb @@ -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 diff --git a/lib/jekyll/algolia/overwrites.rb b/lib/jekyll/algolia/overwrites.rb index 42c45ed..32e03d1 100644 --- a/lib/jekyll/algolia/overwrites.rb +++ b/lib/jekyll/algolia/overwrites.rb @@ -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 diff --git a/scripts/test_integration_prepare b/scripts/test_integration_prepare index 5aa2b99..bac9dda 100755 --- a/scripts/test_integration_prepare +++ b/scripts/test_integration_prepare @@ -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 diff --git a/spec/integration/main_spec.rb b/spec/integration/main_spec.rb index 1ffd032..189545a 100644 --- a/spec/integration/main_spec.rb +++ b/spec/integration/main_spec.rb @@ -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 diff --git a/spec/jekyll-algolia_spec.rb b/spec/jekyll-algolia_spec.rb index 69ef60b..c4cc09b 100644 --- a/spec/jekyll-algolia_spec.rb +++ b/spec/jekyll-algolia_spec.rb @@ -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 diff --git a/spec/jekyll/algolia/overwrites_spec.rb b/spec/jekyll/algolia/overwrites_spec.rb new file mode 100644 index 0000000..322d5f4 --- /dev/null +++ b/spec/jekyll/algolia/overwrites_spec.rb @@ -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 diff --git a/spec/site/links.md b/spec/site/links.md new file mode 100644 index 0000000..c7b93ce --- /dev/null +++ b/spec/site/links.md @@ -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 %} + diff --git a/spec/site/subdir/subpage.md b/spec/site/subdir/subpage.md new file mode 100644 index 0000000..d2360eb --- /dev/null +++ b/spec/site/subdir/subpage.md @@ -0,0 +1,5 @@ +--- +title: This is a page in a subdirectory +--- + +This page is in a subdirectory.