|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe 'CssFetcher' do |
| 4 | + describe '#fetch' do |
| 5 | + let(:subject) { CriticalPathCss::CssFetcher.new(config) } |
| 6 | + let(:response) { |
| 7 | + ['foo','', OpenStruct.new(exitstatus: 0)] |
| 8 | + } |
| 9 | + let(:routes) { ['/', '/new_route'] } |
| 10 | + let(:config) do |
| 11 | + OpenStruct.new( |
| 12 | + base_url: 'http://0.0.0.0:9292', |
| 13 | + css_path: css_path, |
| 14 | + css_paths: css_paths, |
| 15 | + penthouse_options: {}, |
| 16 | + routes: routes |
| 17 | + ) |
| 18 | + end |
| 19 | + |
| 20 | + context 'when a single css_path is configured' do |
| 21 | + let(:css_path) { '/test.css' } |
| 22 | + let(:css_paths) { [] } |
| 23 | + |
| 24 | + it 'generates css for each route from the same file' do |
| 25 | + expect(Open3).to receive(:capture3) do |arg1, arg2, arg3| |
| 26 | + options = JSON.parse(arg3) |
| 27 | + expect(options['css']).to eq '/test.css' |
| 28 | + end.twice.and_return(response) |
| 29 | + subject.fetch |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context 'when multiple css_paths are configured' do |
| 34 | + let(:css_path) { '' } |
| 35 | + let(:css_paths) { ['/test.css', '/test2.css'] } |
| 36 | + |
| 37 | + it 'generates css for each route from the respective file' do |
| 38 | + expect(Open3).to receive(:capture3) do |arg1, arg2, arg3| |
| 39 | + options = JSON.parse(arg3) |
| 40 | + expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/' |
| 41 | + expect(options['css']).to eq '/test2.css' if options['url'] == 'http://0.0.0.0:9292/new_route' |
| 42 | + end.twice.and_return(response) |
| 43 | + subject.fetch |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'when same css file applies to multiple routes' do |
| 48 | + let(:css_path) { '' } |
| 49 | + let(:css_paths) { ['/test.css', '/test2.css', '/test.css'] } |
| 50 | + let(:routes) { ['/', '/new_route', '/newer_route'] } |
| 51 | + |
| 52 | + it 'generates css for each route from the respective file' do |
| 53 | + expect(Open3).to receive(:capture3) do |arg1, arg2, arg3| |
| 54 | + options = JSON.parse(arg3) |
| 55 | + expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/' |
| 56 | + expect(options['css']).to eq '/test2.css' if options['url'] == 'http://0.0.0.0:9292/new_route' |
| 57 | + expect(options['css']).to eq '/test.css' if options['url'] == 'http://0.0.0.0:9292/newer_route' |
| 58 | + end.thrice.and_return(response) |
| 59 | + subject.fetch |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments