spec/unit/exposure_spec.rb in dry-view-0.3.0 vs spec/unit/exposure_spec.rb in dry-view-0.4.0

- old
+ new

@@ -1,10 +1,11 @@ RSpec.describe Dry::View::Exposure do - subject(:exposure) { described_class.new(:hello, proc, object) } + subject(:exposure) { described_class.new(:hello, proc, object, **options) } let(:proc) { -> input { "hi" } } let(:object) { nil } + let(:options) { {} } describe "initialization and attributes" do describe "#name" do it "accepts a name" do expect(exposure.name).to eql :hello @@ -40,10 +41,21 @@ it "can be set on initialization" do expect(described_class.new(:hello, private: true)).to be_private end end + + describe "#default_value" do + it "is nil by default" do + expect(exposure.default_value).to be_nil + end + + it "can be set on initialization" do + exposuse = described_class.new(:hello, default: 'Hi !') + expect(exposuse.default_value).to eq('Hi !') + end + end end describe "#bind" do subject(:bound_exposure) { exposure.bind(bind_object) } @@ -88,16 +100,16 @@ end end end end - describe "#dependencies" do + describe "#dependency_names" do context "proc provided" do let(:proc) { -> input, foo, bar { "hi" } } it "returns an array of exposure dependencies derived from the proc's argument names" do - expect(exposure.dependencies).to eql [:input, :foo, :bar] + expect(exposure.dependency_names).to eql [:input, :foo, :bar] end end context "matching instance method" do let(:proc) { nil } @@ -109,66 +121,94 @@ end end.new end it "returns an array of exposure dependencies derived from the instance method's argument names" do - expect(exposure.dependencies).to eql [:input, :bar, :baz] + expect(exposure.dependency_names).to eql [:input, :bar, :baz] end end context "proc is nil" do let(:proc) { nil } it "returns no dependencies" do - expect(exposure.dependencies).to eql [] + expect(exposure.dependency_names).to eql [] end end end describe "#call" do - let(:input) { "input" } + let(:input) { {name: "Jane"} } context "proc expects input only" do - let(:proc) { -> input { input } } + let(:proc) { -> name: { name } } it "sends the input to the proc" do - expect(exposure.(input)).to eql "input" + expect(exposure.(input)).to eql "Jane" end end context "proc expects input and dependencies" do - let(:proc) { -> input, greeting { "#{greeting}, #{input}" } } + let(:proc) { -> greeting, name: { "#{greeting}, #{name}" } } let(:locals) { {greeting: "Hola"} } it "sends the input and dependency values to the proc" do - expect(exposure.(input, locals)).to eq "Hola, input" + expect(exposure.(input, locals)).to eq "Hola, Jane" end end + context "Default value" do + let(:options) { { default: "John" } } + + context "use default value" do + let(:proc) { nil } + + it "use the default value" do + expect(exposure.({})).to eq "John" + end + end + + context "use input value instead of default" do + let(:proc) { nil } + + it "use the default value" do + expect(exposure.({hello: "Jane"})).to eq "Jane" + end + end + + context "use input value over default even when input is nil" do + let(:proc) { nil } + + it "use the default value" do + expect(exposure.({hello: nil})).to eq nil + end + end + end + context "proc expects dependencies only" do let(:proc) { -> greeting, farewell { "#{greeting}, #{farewell}" } } let(:locals) { {greeting: "Hola", farewell: "Adios"} } it "sends the dependency values to the proc" do expect(exposure.(input, locals)).to eq "Hola, Adios" end end context "proc accesses object instance" do - let(:proc) { -> input { "#{input} from #{name}" } } + let(:proc) { -> name: { "My name is #{name} but call me #{title} #{name}" } } let(:object) do Class.new do - attr_reader :name + attr_reader :title - def initialize(name) - @name = name + def initialize(title) + @title = title end - end.new("Jane") + end.new("Dr") end it "makes the instance available as self" do - expect(exposure.(input)).to eq "input from Jane" + expect(exposure.(input)).to eq "My name is Jane but call me Dr Jane" end end context "no proc" do let(:proc) { nil }