Adding tests for Configurator

This commit is contained in:
Pixelastic 2017-11-08 14:12:49 +01:00
parent f15ac944b6
commit 9f6d3f963a
4 changed files with 127 additions and 75 deletions

View File

@ -15,12 +15,12 @@ require 'rspec/core/rake_task'
desc 'Run tests (with simple progress)'
RSpec::Core::RakeTask.new(:test) do |spec|
spec.rspec_opts = '--color --format progress'
spec.pattern = FileList['spec/*_spec.rb']
spec.pattern = FileList['spec/**/**_spec.rb']
end
desc 'Run tests (with full details)'
RSpec::Core::RakeTask.new(:test_details) do |spec|
spec.rspec_opts = '--color --format documentation'
spec.pattern = FileList['spec/*_spec.rb']
spec.pattern = FileList['spec/**/**_spec.rb']
end
desc 'Run tests in all Ruby versions (with full details)'
RSpec::Core::RakeTask.new(:test_all_ruby_versions) do

View File

@ -2,29 +2,45 @@ module AlgoliaPlugin
def self.do_something(config)
site = Jekyll::Site.new(config)
site.process
42
end
end
describe 'AlgoliaPlugin' do
it 'should create a new site with the config passed' do
# Given
config = { foo: 'bar' }
describe AlgoliaPlugin do
describe '.do_something' do
let(:config) { {foo: 'bar'} }
let(:jekyll_site) { double('Jekyll::Site', process: nil) }
before { expect(Jekyll::Site).to receive(:new).with(config).and_return(jekyll_site) }
before { expect(jekyll_site).to receive(:process) }
# When
AlgoliaPlugin.do_something(config)
it { AlgoliaPlugin.do_something(config) }
# Then
# ???
# expect(Jekyll::Site).to receive(:new).with(input) <= Does not work because
# I haven't allowed listening to it
#
# mock_site = double('Jekyll::Site', process: nil)
# allow(Jekyll::Site).to receive(:new).and_return(mock_site) <= Ok, I can
# call the method without failing but the expect does not work
#
# Only thing that works is writing the full expect (including
# with/and_return) from the start, but that has me writing my expectation at
# the beginning...
#
end
describe '.do_something' do
let(:config) { {foo: 'bar'} }
let(:jekyll_site) { double('Jekyll::Site', process: nil) }
before { allow(Jekyll::Site).to receive(:new).and_return(jekyll_site) }
before { allow(jekyll_site).to receive(:process) }
before { AlgoliaPlugin.do_something(config) }
it { expect(Jekyll::Site).to have_received(:new).with(config) }
it { expect(jekyll_site).to have_received(:process).with(config) }
end
describe '.do_something' do
let(:config) { {foo: 'bar'} }
let(:jekyll_site) { double('Jekyll::Site', process: nil) }
before { allow(Jekyll::Site).to receive(:new).and_return(jekyll_site) }
before { allow(jekyll_site).to receive(:process) }
subject { AlgoliaPlugin.do_something(config) }
context 'config is valid' do
let(:config) { false }
it { should eq 42 }
it { expect(subject).to eq 42 }
end
end
end

View File

@ -1,83 +1,69 @@
require 'spec_helper'
describe(Jekyll::Algolia) do
let(:subject) { Jekyll::Algolia }
let(:current) { Jekyll::Algolia }
# Suppress all Jekyll error messages during tests
before(:each) do
before do
allow(Jekyll.logger).to receive(:info)
allow(Jekyll.logger).to receive(:warn)
allow(Jekyll.logger).to receive(:error)
end
describe 'config' do
it 'should set the @config accessible from outside' do
# Given
input = { 'foo' => 'bar' }
describe '.config' do
# Given
let(:config) { { 'foo' => 'bar' } }
# When
subject.init(input)
# When
subject { current.init(config) }
# Then
expect(subject.config).to include(input)
# Then
it 'should make the config accessible from the outside' do
expect(subject.config).to include(config)
end
end
describe 'monkey_patch_site' do
it 'should change the site write method' do
# Given
site = Jekyll::Site.new(Jekyll.configuration)
initial_method = site.method(:write).source_location
describe '.monkey_patch_site' do
# Given
let(:site) { Jekyll::Site.new(Jekyll.configuration) }
let!(:initial_method) { site.method(:write).source_location }
# When
subject.monkey_patch_site(site)
# When
subject do
current.monkey_patch_site(site)
site.method(:write).source_location
end
# Then
actual = site.method(:write).source_location
expect(actual).not_to eq initial_method
# Then
it 'should change the initial .write method' do
expect(subject).to_not eq initial_method
end
end
describe 'run' do
mock_site = nil
before(:each) do
# We mock Jekyll::Site.new so it always returns an object that answers to
# .process
mock_site = double('Jekyll::Site', process: nil)
allow(Jekyll::Site).to receive(:new).and_return(mock_site)
# Given
let(:configuration) { {} }
let(:jekyll_site) { double('Jekyll::Site', process: nil) }
before { allow(Jekyll::Site).to receive(:new).and_return(jekyll_site) }
before do
allow(current).to receive(:monkey_patch_site).and_return(jekyll_site)
end
it 'should create a new site with the initialized config' do
# Given
input = Jekyll.configuration
# Then
expect(Jekyll::Site).to receive(:new).with(input)
# When
subject.init(input)
subject.run
# When
before do
current.init(configuration)
current.run
end
it 'should monkey patch the created site' do
# Given
input = Jekyll.configuration
# Then
expect(Jekyll::Algolia).to receive(:monkey_patch_site).with(mock_site)
# When
subject.init(input)
subject.run
# Then
it 'should have created a new Jekyll::Site with the configuration' do
expect(Jekyll::Site).to have_received(:new).with(configuration)
end
it 'should call process on the created site' do
# Given
input = Jekyll.configuration
# Then
expect(mock_site).to receive(:process)
# When
subject.init(input)
subject.run
it 'should monkey patch the Jekyll site' do
expect(current).to have_received(:monkey_patch_site).with(jekyll_site)
end
it 'should call .process on the Jekyll site' do
expect(jekyll_site).to have_received(:process)
end
end
end

View File

@ -0,0 +1,50 @@
require 'spec_helper'
describe(Jekyll::Algolia::Configurator) do
let(:current) { Jekyll::Algolia::Configurator }
before do
allow(Jekyll::Algolia).to receive(:config).and_return(config)
end
describe '.get' do
# Given
let(:config) { { 'foo' => 'bar' } }
# When
subject { current.get('foo') }
# Then
it { should eq 'bar' }
end
describe '.algolia' do
subject { current.algolia(input) }
context 'with an algolia config defined' do
let(:config) { { 'algolia' => { 'foo' => 'bar' } } }
context 'with a config option that is set' do
let(:input) { 'foo' }
it { should eq 'bar' }
end
context 'with a config option that is not set' do
let(:input) { 'baz' }
it { should eq nil }
end
describe 'should get the default nodes_to_index' do
let(:input) { 'nodes_to_index' }
it { should eq 'p' }
end
end
context 'with no algolia config defined' do
let(:config) { {} }
let(:input) { 'foo' }
it { should eq nil }
describe 'should get the default nodes_to_index' do
let(:input) { 'nodes_to_index' }
it { should eq 'p' }
end
end
end
end