spec/unit/changeset/map_spec.rb in rom-repository-1.3.1 vs spec/unit/changeset/map_spec.rb in rom-repository-1.3.2
- old
+ new
@@ -40,10 +40,33 @@
])
end
end
end
+ context 'accessing data in a map block' do
+ subject(:changeset) do
+ Class.new(ROM::Changeset::Create[:users]) do
+ map do |tuple|
+ extend_data(tuple)
+ end
+
+ private
+
+ def extend_data(tuple)
+ tuple.merge(email: "#{self[:name].downcase}@test.com")
+ end
+ end.new(relation).data(user_data)
+ end
+
+ let(:relation) { double(:relation) }
+ let(:user_data) { { name: 'Jane' } }
+
+ it 'extends data in a map block' do
+ expect(changeset.to_h).to eql(name: 'Jane', email: 'jane@test.com')
+ end
+ end
+
context 'multi mapping with custom blocks' do
subject(:changeset) do
Class.new(ROM::Changeset::Create[:users]) do
map do |tuple|
tuple.merge(one: next_value)
@@ -51,10 +74,14 @@
map do |tuple|
tuple.merge(two: next_value)
end
+ map do |three: next_value, **other|
+ { three: three, **other }
+ end
+
def initialize(*args)
super
@counter = 0
end
@@ -70,10 +97,10 @@
let(:relation) { double(:relation) }
let(:user_data) { { name: 'Jane' } }
it 'applies mappings in order of definition' do
- expect(changeset.to_h).to eql(name: 'Jane', one: 1, two: 2)
+ expect(changeset.to_h).to eql(name: 'Jane', one: 1, two: 2, three: 3)
end
it 'inherits pipes' do
klass = Class.new(changeset.class)