require "generator_spec"
require_relative '../../../lib/generators/nexus_cqrs/command_generator'

describe NexusCqrs::CommandGenerator, type: :generator do
  destination File.expand_path("../../tmp", __FILE__)

  before(:all) do
    prepare_destination
    run_generator ["TestCommand"]
  end

  it "creates the registration config file for handlers" do
    assert_file "config/initializers/register_cqrs_handlers.rb"
    assert_file "app/domain/commands/test_command.rb"
    assert_file "app/domain/commands/test_command_handler.rb"
  end

  it "creates the registration config file for handlers" do
    line = "$COMMAND_EXECUTOR.register_command(TestCommand, TestCommandHandler.new)"
    base_path = File.expand_path("../../tmp", __FILE__)
    expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line))
  end

  context 'namespacing class names' do
    before(:all) do
      prepare_destination
      run_generator ["Test::TestCommand"]
    end

    it "creates the registration config file for handlers" do
      assert_file "config/initializers/register_cqrs_handlers.rb"
      assert_file "app/domain/commands/test/test_command.rb"
      assert_file "app/domain/commands/test/test_command_handler.rb"
    end

    it "creates the registration config file for handlers" do
      line = "$COMMAND_EXECUTOR.register_command(Test::TestCommand, Test::TestCommandHandler.new)"
      base_path = File.expand_path("../../tmp", __FILE__)
      expect(File.read(base_path + "/config/initializers/register_cqrs_handlers.rb")).to(include(line))
    end
  end
end