# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ProtocolImporters::TablesBuilder do
  # rubocop:disable Metrics/LineLength
  let(:description_string) { '
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 
| a | b | c | d | e | f | g | h | a | a | 
| 1 | 1 | 1 | 2 | 3 | 4 | 5 | 1 | 1 | 1 | 
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 
| asd | as | das | a | as | asd | sad | sa | asd | as124521 | 
' }
  let(:description_string_with_headers) { '' }
  # rubocop:enable Metrics/LineLength
  let(:extract_tables_from_string_result) { described_class.extract_tables_from_html_string(description_string) }
  let(:first_table_in_result) { extract_tables_from_string_result.first }
  describe '.extract_tables_from_string' do
    it 'returns array of Table instances' do
      expect(first_table_in_result).to be_instance_of(Table)
    end
    it 'returns 2 tables ' do
      expect(extract_tables_from_string_result.count).to be == 2
    end
    it 'returns valid table' do
      expect(first_table_in_result).to be_valid
    end
    it 'returns table with 5 rows' do
      expect(JSON.parse(first_table_in_result.contents)['data'].count).to be == 5
    end
    it 'returns table with 10 columns' do
      expect(JSON.parse(first_table_in_result.contents)['data'].first.count).to be == 10
    end
    context 'when droping headers' do
      it 'returns table with 2 rows and 3 columns' do
        table = described_class.extract_tables_from_html_string(description_string_with_headers, true).first
        expect(JSON.parse(table.contents)['data'].count).to be == 2
        expect(JSON.parse(table.contents)['data'].first.count).to be == 3
      end
    end
  end
  describe '.remove_tables_from_html' do
    it 'returns description string without tables' do
      expect(described_class.remove_tables_from_html(description_string)
               .scan('There was a table here, it was moved to tables section.
').size).to eq(2)
    end
  end
end