|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using FakeItEasy; |
| 4 | +using MoBi.Core.Domain.Model; |
| 5 | +using MoBi.Core.Events; |
| 6 | +using MoBi.Presentation.Mappers; |
| 7 | +using MoBi.Presentation.Presenter; |
| 8 | +using MoBi.Presentation.Settings; |
| 9 | +using MoBi.Presentation.Views; |
| 10 | +using OSPSuite.BDDHelper; |
| 11 | +using OSPSuite.BDDHelper.Extensions; |
| 12 | +using OSPSuite.Core.Domain; |
| 13 | +using OSPSuite.Core.Domain.Builder; |
| 14 | +using OSPSuite.Presentation.Nodes; |
| 15 | +using IBuildingBlockRepository = MoBi.Core.Domain.Repository.IBuildingBlockRepository; |
| 16 | + |
| 17 | +namespace MoBi.Presentation |
| 18 | +{ |
| 19 | + public abstract class concern_for_SelectReferenceAtReactionPresenter : ContextSpecification<ISelectReferenceAtReactionPresenter> |
| 20 | + { |
| 21 | + protected ISelectReferenceView _view; |
| 22 | + protected IMoBiContext _context; |
| 23 | + protected IObjectBaseToObjectBaseDTOMapper _objectBaseDTOMapper; |
| 24 | + protected IObjectBaseDTOToReferenceNodeMapper _referenceMapper; |
| 25 | + protected IBuildingBlockRepository _buildingBlockRepository; |
| 26 | + protected ReactionBuilder _reaction; |
| 27 | + protected ITreeNode _reactionNode; |
| 28 | + protected List<ITreeNode> _renderedNodes; |
| 29 | + |
| 30 | + protected override void Context() |
| 31 | + { |
| 32 | + _view = A.Fake<ISelectReferenceView>(); |
| 33 | + _context = A.Fake<IMoBiContext>(); |
| 34 | + _objectBaseDTOMapper = new ObjectBaseToObjectBaseDTOMapper(); |
| 35 | + _referenceMapper = new ObjectBaseDTOToReferenceNodeMapper(_objectBaseDTOMapper); |
| 36 | + _buildingBlockRepository = A.Fake<IBuildingBlockRepository>(); |
| 37 | + |
| 38 | + _reaction = new ReactionBuilder().WithName("newReaction"); |
| 39 | + _reaction.Id = "reactionId"; |
| 40 | + |
| 41 | + var repo = A.Fake<IWithIdRepository>(); |
| 42 | + A.CallTo(() => _context.ObjectRepository).Returns(repo); |
| 43 | + A.CallTo(() => repo.ContainsObjectWithId(_reaction.Id)).Returns(true); |
| 44 | + A.CallTo(() => _context.Get<IObjectBase>(_reaction.Id)).Returns(_reaction); |
| 45 | + |
| 46 | + sut = new SelectReferenceAtReactionPresenter(_view, _objectBaseDTOMapper, _context, A.Fake<IUserSettings>(), |
| 47 | + A.Fake<IObjectBaseToDummyMoleculeDTOMapper>(), A.Fake<IParameterToDummyParameterDTOMapper>(), |
| 48 | + _referenceMapper, A.Fake<IObjectPathCreatorAtReaction>(), _buildingBlockRepository); |
| 49 | + |
| 50 | + _reactionNode = _referenceMapper.MapFrom(_reaction); |
| 51 | + A.CallTo(() => _view.Shows(_reaction)).Returns(true); |
| 52 | + A.CallTo(() => _view.GetNodes(_reaction)).Returns(new List<ITreeNode> { _reactionNode }); |
| 53 | + |
| 54 | + _renderedNodes = new List<ITreeNode>(); |
| 55 | + A.CallTo(() => _view.AddNode(A<ITreeNode>._)).Invokes(x => _renderedNodes.Add(x.GetArgument<ITreeNode>(0))); |
| 56 | + } |
| 57 | + |
| 58 | + protected void AddParameterAndRaiseEvent(string name = "lastParam", string id = "pNew") |
| 59 | + { |
| 60 | + var newParameter = new Parameter().WithName(name).WithId(id); |
| 61 | + _reaction.Add(newParameter); |
| 62 | + sut.Handle(new AddedEvent<IParameter>(newParameter, _reaction)); |
| 63 | + } |
| 64 | + |
| 65 | + protected void EnumerateReactionNodeChildren() => _reactionNode.Children.ToList(); |
| 66 | + } |
| 67 | + |
| 68 | + public class When_a_parameter_is_added_to_a_reaction_whose_children_are_not_yet_enumerated : concern_for_SelectReferenceAtReactionPresenter |
| 69 | + { |
| 70 | + protected override void Context() |
| 71 | + { |
| 72 | + base.Context(); |
| 73 | + _reaction.Add(new Parameter().WithName("k_on").WithId("p1")); |
| 74 | + _reaction.Add(new Parameter().WithName("k_off").WithId("p2")); |
| 75 | + } |
| 76 | + |
| 77 | + protected override void Because() |
| 78 | + { |
| 79 | + AddParameterAndRaiseEvent(); |
| 80 | + } |
| 81 | + |
| 82 | + [Observation] |
| 83 | + public void should_not_add_the_node_so_the_tree_still_enumerates_all_parameters_when_the_reaction_is_expanded() |
| 84 | + { |
| 85 | + _renderedNodes.ShouldBeEmpty(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public class When_a_parameter_is_added_to_a_reaction_whose_children_are_already_enumerated : concern_for_SelectReferenceAtReactionPresenter |
| 90 | + { |
| 91 | + protected override void Context() |
| 92 | + { |
| 93 | + base.Context(); |
| 94 | + _reaction.Add(new Parameter().WithName("k_on").WithId("p1")); |
| 95 | + _reaction.Add(new Parameter().WithName("k_off").WithId("p2")); |
| 96 | + EnumerateReactionNodeChildren(); |
| 97 | + } |
| 98 | + |
| 99 | + protected override void Because() |
| 100 | + { |
| 101 | + AddParameterAndRaiseEvent(); |
| 102 | + } |
| 103 | + |
| 104 | + [Observation] |
| 105 | + public void should_add_the_newly_created_parameter_node() |
| 106 | + { |
| 107 | + _renderedNodes.Select(x => x.Text).ShouldContain("lastParam"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public class When_the_first_parameter_is_added_to_an_already_enumerated_empty_reaction : concern_for_SelectReferenceAtReactionPresenter |
| 112 | + { |
| 113 | + protected override void Context() |
| 114 | + { |
| 115 | + base.Context(); |
| 116 | + EnumerateReactionNodeChildren(); |
| 117 | + } |
| 118 | + |
| 119 | + protected override void Because() |
| 120 | + { |
| 121 | + AddParameterAndRaiseEvent("firstParam", "p1"); |
| 122 | + } |
| 123 | + |
| 124 | + [Observation] |
| 125 | + public void should_add_the_first_parameter_node() |
| 126 | + { |
| 127 | + _renderedNodes.Select(x => x.Text).ShouldContain("firstParam"); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments