<commit_before>require 'spec_helper'
require 'core/entities/article'

describe ArticleDecorator do

  let(:decorator) { ArticleDecorator }

  let(:article) do
    decorator.decorate(
      double(Core::Article,
             id:          'bob',
             title:       'uncle-bob-is-richer-than-you',
             description: 'uncle is rich',
             body:        api_article['body']))
  end

  describe '#content' do
    describe 'sanitizes HTML' do
      context 'with content than needs sanitizing' do
        let(:api_article) do
          MultiJson.load(File.read('spec/fixtures/pawnbrokers-how-they-work.json'))
        end

        let(:html) { Nokogiri::HTML(decorator.decorate(article).content) }

        it 'strips images from intro paragraphs' do
          expect(html.search(HTMLProcessor::INTRO_IMG)).to be_empty
        end

        it 'strips action email links' do
          expect(html.search(HTMLProcessor::ACTION_EMAIL)).to be_empty
        end

        it 'strips action forms' do
          expect(html.search(HTMLProcessor::ACTION_FORM)).to be_empty
        end
      end

      context 'with a video embeded in an iframe' do
        let(:xpath) do
          '//div[@class="video-wrapper"]/p/iframe[starts-with(@src, "https://www.youtube.com/embed")]'
        end

        let(:api_article) do
          MultiJson.load(File.read('spec/fixtures/responsive-video.json'))
        end
        let(:html) { Nokogiri::HTML(decorator.decorate(article).content) }

        it 'wraps the content in a div[@class="video-wrapper"]' do
          expect(html.search(xpath)).not_to be_empty
        end
      end

      context 'with a table' do
        let(:xpath) do
          '//div[@class="table-wrapper"]/table[@class="datatable-default"]'
        end

        let(:api_article) do
          MultiJson.load(File.read('spec/fixtures/responsive-table.json'))
        end
        let(:html) { Nokogiri::HTML(decorator.decorate(article).content) }

        it 'wraps the content in a div[@class="table-wrapper"]' do
          expect(html.search(xpath)).not_to be_empty
        end
      end
    end
  end
end<commit_msg>Refactor ArticleDecorator spec<commit_after>require 'spec_helper'
require 'core/entities/article'

describe ArticleDecorator do
  subject(:decorator) { described_class.decorate(article) }

  let(:article) do
    double(Core::Article,
           id:          'bob',
           title:       'uncle-bob-is-richer-than-you',
           description: 'uncle is rich',
           body:        MultiJson.load(File.read(fixture))['body'])
  end

  describe '#content' do
    let(:html) { Nokogiri::HTML(decorator.content) }

    context 'when the object body needs processing' do
      let(:fixture) { 'spec/fixtures/pawnbrokers-how-they-work.json' }

      it 'strips images from intro paragraphs' do
        expect(html.search(HTMLProcessor::INTRO_IMG)).to be_empty
      end

      it 'strips action email links' do
        expect(html.search(HTMLProcessor::ACTION_EMAIL)).to be_empty
      end

      it 'strips action forms' do
        expect(html.search(HTMLProcessor::ACTION_FORM)).to be_empty
      end
    end

    context 'when the object body contains a video embeded in an iframe' do
      let(:fixture) { 'spec/fixtures/responsive-video.json' }

      it 'wraps the content in a div[@class="video-wrapper"]' do
        nodes = html.search('//div[@class="video-wrapper"]/p/iframe' +
                              '[starts-with(@src, "https://www.youtube.com/embed")]')

        expect(nodes).to_not be_empty
      end
    end

    context 'when the object contains a table' do
      let(:fixture) { 'spec/fixtures/responsive-table.json' }

      it 'wraps the content in a div[@class="table-wrapper"]' do
        nodes = html.search('//div[@class="table-wrapper"]/table[@class="datatable-default"]')

        expect(nodes).to_not be_empty
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12189500640204866, 'fraction_numerical': 0.0, 'mean_word_length': 2.503139013452915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4141555', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1174, 'n_words': 258}