spec/integration/exposures_spec.rb in dry-view-0.3.0 vs spec/integration/exposures_spec.rb in dry-view-0.4.0

- old
+ new

@@ -8,12 +8,12 @@ config.layout = 'app' config.template = 'users' config.default_format = :html end - expose :users do |input| - input.fetch(:users).map { |user| + expose :users do |users:| + users.map { |user| user.merge(name: user[:name].upcase) } end end.new @@ -41,12 +41,12 @@ def initialize super @prefix = "My friend " end - expose :users do |input| - input.fetch(:users).map { |user| + expose :users do |users:| + users.map { |user| user.merge(name: prefix + user[:name]) } end end.new @@ -71,12 +71,12 @@ expose :users private - def users(input) - input.fetch(:users).map { |user| + def users(users:) + users.map { |user| user.merge(name: user[:name].upcase) } end end.new @@ -110,24 +110,56 @@ expect(vc.(users: users, context: context)).to eql( '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><div class="users"><table><tbody><tr><td>Jane</td><td>jane@doe.org</td></tr><tr><td>Joe</td><td>joe@doe.org</td></tr></tbody></table></div><img src="mindblown.jpg" /></body></html>' ) end + it 'using default values' do + vc = Class.new(Dry::View::Controller) do + configure do |config| + config.paths = SPEC_ROOT.join('fixtures/templates') + config.layout = 'app' + config.template = 'users' + config.default_format = :html + end + + expose :users, default: [{name: 'John', email: 'john@william.org'}] + end.new + + expect(vc.(context: context)).to eql( + '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><div class="users"><table><tbody><tr><td>John</td><td>john@william.org</td></tr></tbody></table></div><img src="mindblown.jpg" /></body></html>' + ) + end + + it 'having default values but passing nil as value for exposure' do + vc = Class.new(Dry::View::Controller) do + configure do |config| + config.paths = SPEC_ROOT.join('fixtures/templates') + config.layout = 'app' + config.template = 'greeting' + config.default_format = :html + end + + expose :greeting, default: 'Hello Dry-rb' + end.new + + expect(vc.(greeting: nil, context: context)).to eql( + '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><p></p></body></html>' + ) + end + it 'allows exposures to depend on each other' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' config.template = 'users_with_count' config.default_format = :html end - expose :users do |input| - input.fetch(:users) - end + expose :users - expose :users_count do |users| + expose :users_count do |users:| "#{users.length} users" end end.new users = [ @@ -138,10 +170,79 @@ expect(vc.(users: users, context: context)).to eql( '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><ul><li>Jane (jane@doe.org)</li><li>Joe (joe@doe.org)</li></ul><div class="count">2 users</div></body></html>' ) end + it 'allows exposures to depend on each other and access keywords args from input' do + vc = Class.new(Dry::View::Controller) do + configure do |config| + config.paths = SPEC_ROOT.join('fixtures/templates') + config.layout = 'app' + config.template = 'greeting' + config.default_format = :html + end + + expose :greeting do |prefix, greeting:| + "#{prefix} #{greeting}" + end + + expose :prefix do + 'Hello' + end + end.new + + expect(vc.(greeting: 'From dry-view internals', context: context)).to eql( + '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><p>Hello From dry-view internals</p></body></html>' + ) + end + + it 'set default values for keyword arguments' do + vc = Class.new(Dry::View::Controller) do + configure do |config| + config.paths = SPEC_ROOT.join('fixtures/templates') + config.layout = 'app' + config.template = 'greeting' + config.default_format = :html + end + + expose :greeting do |prefix, greeting: 'From the defaults'| + "#{prefix} #{greeting}" + end + + expose :prefix do + 'Hello' + end + end.new + + expect(vc.(context: context)).to eql( + '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><p>Hello From the defaults</p></body></html>' + ) + end + + it 'only pass keywords arguments that are needit in the block and allow for default values' do + vc = Class.new(Dry::View::Controller) do + configure do |config| + config.paths = SPEC_ROOT.join('fixtures/templates') + config.layout = 'app' + config.template = 'edit' + config.default_format = :html + end + + expose :pretty_id do |id:| + "Beautiful #{id}" + end + + expose :errors do |errors: []| + errors + end + end.new + + expect(vc.(id: 1, context: context)).to eql( + '<!DOCTYPE html><html><head><title>dry-view rocks!</title></head><body><h1>Edit</h1><p>No Errors</p><p>Beautiful 1</p></body></html>' + ) + end + it 'supports defining multiple exposures at once' do vc = Class.new(Dry::View::Controller) do configure do |config| config.paths = SPEC_ROOT.join('fixtures/templates') config.layout = 'app' @@ -151,15 +252,11 @@ expose :users, :users_count private - def users(input) - input.fetch(:users) - end - - def users_count(users) + def users_count(users:) "#{users.length} users" end end.new users = [ @@ -183,14 +280,12 @@ private_expose :prefix do "COUNT: " end - expose :users do |input| - input.fetch(:users) - end + expose :users - expose :users_count do |prefix, users| + expose :users_count do |prefix, users:| "#{prefix}#{users.length} users" end end.new users = [