module Lipstick
  RSpec.describe EmailMessage do
    let(:title) { 'An AAF Test Service' }

    let(:message) do
      <<~MESSAGE
        # A Message

        From Us!

        <http://example.com/with/a/link>
      MESSAGE
    end

    let(:url) { 'http://example.com/images/header.png' }
    let(:template_file) do
      File.expand_path('../../../app/views/layouts/email_branding.html.erb',
                       File.dirname(__FILE__))
    end

    let(:template) { File.read(template_file) }

    context 'template' do
      subject { template }
      it { is_expected.to match(/<%= content %>/) }
    end

    context 'rendered text' do
      let(:instance) do
        EmailMessage.new(title: title, content: message, image_url: url)
      end

      let(:link) { 'http://example.com/with/a/link' }

      subject { Capybara.string(instance.render) }

      it { is_expected.not_to have_content(/<%= content %>/) }
      it { is_expected.to have_css('.bodyContent div h1', text: 'A Message') }
      it { is_expected.to have_css('.bodyContent div p', text: 'From Us!') }
      it { is_expected.to have_xpath("//img[@src='#{url}']") }
      it { is_expected.to have_title(title) }
      it { is_expected.to have_xpath("//a[@href='#{link}']", text: link) }
    end

    context 'with a custom template' do
      let(:custom_template) { template.gsub(/AAF/, 'Example Organisation') }

      let(:instance) do
        EmailMessage.new(title: title, content: message, image_url: url,
                         template: custom_template)
      end

      subject { Capybara.string(instance.render) }

      it { is_expected.to have_content('Example Organisation') }
    end
  end
end
