Skip to content

Commit

Permalink
Tests for association manipulation on has many owned root
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisandreae committed Jul 5, 2019
1 parent 29d03db commit 36864e1
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/unit/view_model/active_record/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,65 @@ def new_model
assert_equal(2, model.children.size)
assert_equal('renamed', model.children.first.name)
end

describe 'with association manipulation' do
it 'appends a child' do
view = create_viewmodel!

view.append_associated(:children, { '_type' => 'Child', 'name' => 'newchildname' })

view.model.reload
assert_equal(3, view.children.size)
assert_equal('newchildname', view.children.last.name)
end

it 'inserts a child' do
view = create_viewmodel!
c1 = view.children.first

view.append_associated(:children,
{ '_type' => 'Child', 'name' => 'newchildname' },
after: c1.to_reference)
view.model.reload

assert_equal(3, view.children.size)
assert_equal('newchildname', view.children[1].name)
end

it 'moves a child' do
view = create_viewmodel!
c1, c2 = view.children

view.append_associated(:children,
{ '_type' => 'Child', 'id' => c2.id },
before: c1.to_reference)
view.model.reload

assert_equal(2, view.children.size)
assert_equal(['c2', 'c1'], view.children.map(&:name))
end

it 'replaces children' do
view = create_viewmodel!
view.replace_associated(:children,
[{ '_type' => 'Child', 'name' => 'newchildname' }])

view.model.reload

assert_equal(1, view.children.size)
assert_equal('newchildname', view.children[0].name)
end

it 'deletes a child' do
view = create_viewmodel!
view.delete_associated(:children, view.children.first.id)

view.model.reload

assert_equal(1, view.children.size)
assert_equal('c2', view.children[0].name)
end
end
end

describe 'renaming associations' do
Expand Down

0 comments on commit 36864e1

Please sign in to comment.