|
31 | 31 | describe '.global' do
|
32 | 32 | it { expect(described_class.global).to be_a Hash }
|
33 | 33 | end
|
| 34 | + |
| 35 | + describe '.merge_global_stylesheets' do |
| 36 | + let(:config_with_stylesheets) do |
| 37 | + { |
| 38 | + stylesheets: [{ href: '/custom.xsl', type: 'text/xsl' }], |
| 39 | + feeds: { |
| 40 | + example: { |
| 41 | + channel: { url: 'https://example.com' }, |
| 42 | + selectors: { items: { selector: 'div' } } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + end |
| 47 | + |
| 48 | + let(:config_without_stylesheets) do |
| 49 | + { |
| 50 | + stylesheets: [{ href: '/rss.xsl', type: 'text/xsl' }], |
| 51 | + feeds: { |
| 52 | + example: { |
| 53 | + channel: { url: 'https://example.com' }, |
| 54 | + selectors: { items: { selector: 'div' } } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + end |
| 59 | + |
| 60 | + let(:config_no_global_stylesheets) do |
| 61 | + { |
| 62 | + feeds: { |
| 63 | + example: { |
| 64 | + channel: { url: 'https://example.com' }, |
| 65 | + selectors: { items: { selector: 'div' } } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + end |
| 70 | + |
| 71 | + context 'when config has no stylesheets and global has stylesheets' do |
| 72 | + before do |
| 73 | + allow(described_class).to receive(:global).and_return(config_without_stylesheets) |
| 74 | + end |
| 75 | + |
| 76 | + it 'merges global stylesheets into the config' do |
| 77 | + config = { channel: { url: 'https://test.com' } } |
| 78 | + result = described_class.merge_global_stylesheets(config) |
| 79 | + |
| 80 | + expect(result[:stylesheets]).to eq([{ href: '/rss.xsl', type: 'text/xsl' }]) |
| 81 | + end |
| 82 | + |
| 83 | + it 'preserves original config data' do |
| 84 | + config = { channel: { url: 'https://test.com' } } |
| 85 | + result = described_class.merge_global_stylesheets(config) |
| 86 | + |
| 87 | + expect(result[:channel]).to eq({ url: 'https://test.com' }) |
| 88 | + end |
| 89 | + |
| 90 | + it 'duplicates the config to avoid mutation' do |
| 91 | + config = { channel: { url: 'https://test.com' } } |
| 92 | + result = described_class.merge_global_stylesheets(config) |
| 93 | + |
| 94 | + expect(result).not_to be(config) |
| 95 | + end |
| 96 | + |
| 97 | + it 'creates a new object instance' do |
| 98 | + config = { channel: { url: 'https://test.com' } } |
| 99 | + result = described_class.merge_global_stylesheets(config) |
| 100 | + |
| 101 | + expect(result.object_id).not_to eq(config.object_id) |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context 'when config already has stylesheets' do |
| 106 | + before do |
| 107 | + allow(described_class).to receive(:global).and_return(config_without_stylesheets) |
| 108 | + end |
| 109 | + |
| 110 | + it 'does not override existing stylesheets' do |
| 111 | + config = { stylesheets: [{ href: '/custom.xsl', type: 'text/xsl' }] } |
| 112 | + result = described_class.merge_global_stylesheets(config) |
| 113 | + |
| 114 | + expect(result[:stylesheets]).to eq([{ href: '/custom.xsl', type: 'text/xsl' }]) |
| 115 | + end |
| 116 | + |
| 117 | + it 'returns the original config without duplication' do |
| 118 | + config = { stylesheets: [{ href: '/custom.xsl', type: 'text/xsl' }] } |
| 119 | + result = described_class.merge_global_stylesheets(config) |
| 120 | + |
| 121 | + expect(result).to be(config) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context 'when global config has no stylesheets' do |
| 126 | + before do |
| 127 | + allow(described_class).to receive(:global).and_return(config_no_global_stylesheets) |
| 128 | + end |
| 129 | + |
| 130 | + it 'returns the original config unchanged' do |
| 131 | + config = { channel: { url: 'https://test.com' } } |
| 132 | + result = described_class.merge_global_stylesheets(config) |
| 133 | + |
| 134 | + expect(result).to be(config) |
| 135 | + end |
| 136 | + |
| 137 | + it 'does not add stylesheets when none exist globally' do |
| 138 | + config = { channel: { url: 'https://test.com' } } |
| 139 | + result = described_class.merge_global_stylesheets(config) |
| 140 | + |
| 141 | + expect(result[:stylesheets]).to be_nil |
| 142 | + end |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + describe '.find with stylesheet merging' do |
| 147 | + before do |
| 148 | + allow(described_class).to receive_messages(feeds: { |
| 149 | + example: { |
| 150 | + channel: { url: 'https://example.com' }, |
| 151 | + selectors: { items: { selector: 'div' } } |
| 152 | + } |
| 153 | + }, global: { |
| 154 | + stylesheets: [{ href: '/rss.xsl', type: 'text/xsl' }] |
| 155 | + }) |
| 156 | + end |
| 157 | + |
| 158 | + it 'merges global stylesheets when finding a feed' do |
| 159 | + result = described_class.find('example') |
| 160 | + |
| 161 | + expect(result[:stylesheets]).to eq([{ href: '/rss.xsl', type: 'text/xsl' }]) |
| 162 | + end |
| 163 | + |
| 164 | + it 'preserves feed configuration when finding a feed' do |
| 165 | + result = described_class.find('example') |
| 166 | + |
| 167 | + expect(result[:channel]).to eq({ url: 'https://example.com' }) |
| 168 | + end |
| 169 | + end |
34 | 170 | end
|
0 commit comments