Skip to content

Refactor tests #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions spec/fixtures/files/config/mutliple-css-paths.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route

development:
<<: *defaults

test:
<<: *defaults
11 changes: 11 additions & 0 deletions spec/fixtures/files/config/no-paths-specified.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
manifest_name: application
routes:
- /

development:
<<: *defaults

test:
<<: *defaults
15 changes: 15 additions & 0 deletions spec/fixtures/files/config/paths-and-routes-not-same-length.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route
- /newer_route

development:
<<: *defaults

test:
<<: *defaults
15 changes: 15 additions & 0 deletions spec/fixtures/files/config/paths-both-specified.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
css_path: /test.css
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route

development:
<<: *defaults

test:
<<: *defaults
11 changes: 11 additions & 0 deletions spec/fixtures/files/config/single-css-path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defaults: &defaults
base_url: http://0.0.0.0:9292
css_path: /test.css
routes:
- /

development:
<<: *defaults

test:
<<: *defaults
26 changes: 18 additions & 8 deletions spec/lib/critical_path_css/css_fetcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'spec_helper'

RSpec.describe 'CssFetcher' do
let(:subject) { CriticalPathCss::CssFetcher.new(config) }
subject { CriticalPathCss::CssFetcher.new(config) }

let(:base_url) { 'http://0.0.0.0:9292' }
let(:response) { ['foo','', OpenStruct.new(exitstatus: 0)] }
let(:routes) { ['/', '/new_route'] }
let(:routes) { ['/', '/new_route'] }
let(:config) do
OpenStruct.new(
base_url: 'http://0.0.0.0:9292',
base_url: base_url,
css_path: css_path,
css_paths: css_paths,
penthouse_options: {},
Expand All @@ -22,6 +24,7 @@
it 'generates css for the single route' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
options = JSON.parse(arg3)

expect(options['css']).to eq '/test.css'
end.once.and_return(response)

Expand All @@ -38,8 +41,10 @@
it 'generates css for each route from the same file' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
options = JSON.parse(arg3)

expect(options['css']).to eq '/test.css'
end.twice.and_return(response)

subject.fetch
end
end
Expand All @@ -51,9 +56,12 @@
it 'generates css for each route from the respective file' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
options = JSON.parse(arg3)
expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/'
expect(options['css']).to eq '/test2.css' if options['url'] == 'http://0.0.0.0:9292/new_route'

css_paths.each_with_index do |path, index|
expect(options['css']).to eq path if options['url'] == "#{base_url}/#{routes[index]}"
end
end.twice.and_return(response)

subject.fetch
end
end
Expand All @@ -66,10 +74,12 @@
it 'generates css for each route from the respective file' do
expect(Open3).to receive(:capture3) do |arg1, arg2, arg3|
options = JSON.parse(arg3)
expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/'
expect(options['css']).to eq '/test2.css' if options['url'] == 'http://0.0.0.0:9292/new_route'
expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/newer_route'

css_paths.each_with_index do |path, index|
expect(options['css']).to eq path if options['url'] == "#{base_url}/#{routes[index]}"
end
end.thrice.and_return(response)

subject.fetch
end
end
Expand Down
93 changes: 6 additions & 87 deletions spec/lib/critical_path_css/rails/config_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
require 'spec_helper'

RSpec.describe 'ConfigLoader' do
let(:subject) { CriticalPathCss::Rails::ConfigLoader.new }
subject { CriticalPathCss::Rails::ConfigLoader.new }

describe '#load' do
before do
allow(File).to receive(:read).and_return(config_file)
end

context 'when single css_path is specified' do
let(:config_file) {
<<~CONFIG
defaults: &defaults
base_url: http://0.0.0.0:9292
css_path: /test.css
routes:
- /

development:
<<: *defaults

test:
<<: *defaults
CONFIG
}
let(:config_file) { file_fixture('config/single-css-path.yml').read }

it 'sets css_path with the path' do
expect(subject.config['css_path']).to eq '/app/spec/internal/public/test.css'
Expand All @@ -35,24 +21,7 @@
end

context 'when multiple css_paths are specified' do
let(:config_file) {
<<~CONFIG
defaults: &defaults
base_url: http://0.0.0.0:9292
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route

development:
<<: *defaults

test:
<<: *defaults
CONFIG
}
let(:config_file) { file_fixture('config/mutliple-css-paths.yml').read }

it 'sets css_path to empty string' do
expect(subject.config['css_path']).to eq ''
Expand All @@ -64,21 +33,7 @@
end

context 'when no paths are specified' do
let(:config_file) {
<<~CONFIG
defaults: &defaults
base_url: http://0.0.0.0:9292
manifest_name: application
routes:
- /

development:
<<: *defaults

test:
<<: *defaults
CONFIG
}
let(:config_file) { file_fixture('config/no-paths-specified.yml').read }

it 'sets css_path with the path' do
expect(subject.config['css_path']).to eq '/stylesheets/application.css'
Expand All @@ -90,51 +45,15 @@
end

context 'when single css_path and multiple css_paths are both specified' do
let(:config_file) {
<<~CONFIG
defaults: &defaults
base_url: http://0.0.0.0:9292
css_path: /test.css
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route

development:
<<: *defaults

test:
<<: *defaults
CONFIG
}
let(:config_file) { file_fixture('config/paths-both-specified.yml').read }

it 'raises an error' do
expect { subject }.to raise_error LoadError, 'Cannot specify both css_path and css_paths'
end
end

context 'when css_paths and routes are not the same length' do
let(:config_file) {
<<~CONFIG
defaults: &defaults
base_url: http://0.0.0.0:9292
css_paths:
- /test.css
- /test2.css
routes:
- /
- /new_route
- /newer_route

development:
<<: *defaults

test:
<<: *defaults
CONFIG
}
let(:config_file) { file_fixture('config/paths-and-routes-not-same-length.yml').read }

it 'raises an error' do
expect { subject }.to raise_error LoadError, 'Must specify css_paths for each route'
Expand Down