StepPanelTest.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package org.leumasjaffe.recipe.view;
  2. import static org.junit.jupiter.api.Assertions.*;
  3. import static org.mockito.Mockito.*;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import javax.swing.JLabel;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import org.junit.jupiter.api.extension.ExtendWith;
  11. import org.leumasjaffe.mock.MockObserverListener;
  12. import org.leumasjaffe.observer.ObserverDispatch;
  13. import org.leumasjaffe.recipe.model.Amount;
  14. import org.leumasjaffe.recipe.model.Duration;
  15. import org.leumasjaffe.recipe.model.Ingredient;
  16. import org.leumasjaffe.recipe.model.Step;
  17. import org.mockito.InjectMocks;
  18. import org.mockito.Spy;
  19. import org.mockito.junit.jupiter.MockitoExtension;
  20. @ExtendWith(MockitoExtension.class)
  21. class StepPanelTest extends SwingTestCase {
  22. List<Ingredient> ingredients;
  23. Step stuff;
  24. @Spy JLabel lblIndex;
  25. @Spy AutoGrowPanel<IngredientPanel, Ingredient> panelIngredients =
  26. new AutoGrowPanel<>(Ingredient::new, IngredientPanel::new);
  27. @InjectMocks StepPanel panel = new StepPanel();
  28. @BeforeEach
  29. void setUp() {
  30. ingredients = new ArrayList<>(Arrays.asList(new Ingredient("Onion", "Sliced", new Amount("100 g"))));
  31. stuff = new Step();
  32. stuff.setDuration(new Duration("30 s"));
  33. stuff.setIngredients(ingredients);
  34. stuff.setInstruction("These are test instructions");
  35. panel.setModel(stuff);
  36. }
  37. @Test
  38. void testFilledOutWithContent() {
  39. assertEquals("These are test instructions", panel.getTxtpnInstructions().getText());
  40. verify(panelIngredients).setModel(same(ingredients), any());
  41. }
  42. @Test
  43. void testSetListPositionUpdatesIndex() {
  44. panel.setListPosition(1);
  45. verify(lblIndex).setText("Step 2");
  46. }
  47. @Test
  48. void testUpdatesContentOnNotification() {
  49. stuff.setInstruction("New instructions");
  50. ObserverDispatch.notifySubscribers(stuff);
  51. assertEquals("New instructions", panel.getTxtpnInstructions().getText());
  52. }
  53. @Test
  54. void testViewUpdatesAltersModel() {
  55. panel.getTxtpnInstructions().setText("New instructions");
  56. waitForSwing();
  57. assertEquals("New instructions", stuff.getInstruction());
  58. }
  59. @Test
  60. void testPropogatesNotifications() {
  61. final MockObserverListener listener = spy(MockObserverListener.class);
  62. listener.setObserved(stuff);
  63. // setObserved invokes our callback.
  64. clearInvocations(listener);
  65. ObserverDispatch.notifySubscribers(ingredients.get(0));
  66. verify(listener, atLeast(1)).updateWasSignalled();
  67. }
  68. }