# frozen_string_literal: true

require "rails_helper"

def reload_mailer_class(class_name)
  Alchemy.send(:remove_const, class_name)
  load("app/mailers/alchemy/#{class_name.underscore}.rb")
end

module Alchemy
  describe MessagesMailer do
    let(:message) { Message.new(email: "jon@doe.com", message: "Lorem ipsum") }
    let(:mail) { MessagesMailer.contact_form_mail(message, "admin@page.com", "contact@page.com", "Subject") }

    it "inherits from BaseMailer" do
      expect(MessagesMailer < BaseMailer).to eq(true)
    end

    context "with ApplicationMailer defined" do
      let(:application_mailer) { Class.new(ActionMailer::Base) }

      before do
        Object.const_set(:ApplicationMailer, application_mailer)
        reload_mailer_class("BaseMailer")
        reload_mailer_class("MessagesMailer")
      end

      it "inherits from ApplicationMailer" do
        expect(MessagesMailer < ApplicationMailer).to eq(true)
      end
    end

    it "delivers a mail to owner" do
      expect(mail.to).to eq(["admin@page.com"])
      expect(mail.subject).to eq("Subject")
    end

    it "reply_to should be set to senders email" do
      expect(mail.reply_to).to eq([message.email])
    end

    it "mail body includes message" do
      expect(mail.body).to match(/#{message.message}/)
    end
  end
end
