Skip to content

Commit

Permalink
Refactor HomeView and add ServiceConfig
Browse files Browse the repository at this point in the history
Refactor the HomeView class to integrate ClockEventService and improve button state management. Introduce ServiceConfig class to configure ClockEventService bean with ClockEclipseAdapter. Added comprehensive unit tests to verify these changes.
  • Loading branch information
LiveNathan committed Aug 16, 2024
1 parent 23a7c60 commit 22d16d8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ drivers/
# Error screenshots generated by TestBench for failed integration tests
error-screenshots/
webpack.generated.js

storage
74 changes: 48 additions & 26 deletions src/main/java/dev/nathanlively/HomeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.Uses;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.ListItem;
import com.vaadin.flow.component.html.UnorderedList;
import com.vaadin.flow.component.icon.Icon;
Expand All @@ -16,22 +15,28 @@
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import com.vaadin.flow.theme.lumo.LumoUtility.Gap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@PageTitle("clock")
@Route(value = "")
@RouteAlias(value = "")
@Uses(Icon.class)
public class HomeView extends Composite<VerticalLayout> {
private static final Logger log = LoggerFactory.getLogger(HomeView.class);
private final ClockEventService service;
private final Button buttonPrimary;
private final Button buttonSecondary;
private final UnorderedList eventsList;

public HomeView() {
public HomeView(ClockEventService service) {
this.service = service;
HorizontalLayout mainRow = new HorizontalLayout();
VerticalLayout column1 = new VerticalLayout();
VerticalLayout column2 = new VerticalLayout();

Button buttonPrimary = new Button();
Button buttonSecondary = new Button();
UnorderedList eventsList = new UnorderedList();

getContent().setWidth("100%");
getContent().getStyle().set("flex-grow", "1");
mainRow.addClassName(Gap.MEDIUM);
Expand All @@ -42,16 +47,24 @@ public HomeView() {
column1.setJustifyContentMode(JustifyContentMode.CENTER);
column1.setAlignItems(Alignment.CENTER);

buttonPrimary.setText("Clock In");
buttonSecondary.setText("Clock Out");
buttonPrimary = new Button("Clock In", event -> clockIn());
buttonSecondary = new Button("Clock Out", event -> clockOut());
eventsList = new UnorderedList();

buttonPrimary.setId("button-primary");
buttonSecondary.setId("button-secondary");
eventsList.setId("events-list");

buttonPrimary.setWidth("min-content");
buttonSecondary.setWidth("min-content");
buttonPrimary.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

column2.getStyle().set("flex-grow", "1");
eventsList.setWidth("100%");
eventsList.setHeight("100%");
addSampleEvents(eventsList);
addClockEvents();
updateButtonVisibility();

getContent().add(mainRow);
mainRow.add(column1);
column1.add(buttonPrimary);
Expand All @@ -60,26 +73,35 @@ public HomeView() {
column2.add(eventsList);
}

private void addSampleEvents(UnorderedList eventsList) {
String[] sampleEvents = {
"8/15/24 9:12 IN",
"8/15/24 17:12 OUT",
"8/16/24 9:10 IN",
"8/16/24 17:09 OUT"
};

for (String event : sampleEvents) {
ListItem item = new ListItem(event);
eventsList.add(item);
private void clockIn() {
ClockEventView clockEventView = service.clockIn();
eventsList.addComponentAsFirst(new ListItem(clockEventView.toString()));
updateButtonVisibility();
}

private void clockOut() {
ClockEventView clockEventView = service.clockOut();
eventsList.addComponentAsFirst(new ListItem(clockEventView.toString()));
updateButtonVisibility();
}

void addClockEvents() {
List<ClockEventView> allClockEvents = service.all();
for (ClockEventView clockEvent : allClockEvents) {
log.info("Adding event: {}", clockEvent);
eventsList.add(new ListItem(clockEvent.toString()));
}
}

private void setGridSampleData(Grid grid) {
// grid.setItems(query -> samplePersonService.list(
// PageRequest.of(query.getPage(), query.getPageSize(), VaadinSpringDataHelpers.toSpringDataSort(query)))
// .stream());
private void updateButtonVisibility() {
ClockEventType lastEventType = service.getLastClockEventType();
if (lastEventType == ClockEventType.IN) {
buttonPrimary.setVisible(false);
buttonSecondary.setVisible(true);
} else {
buttonPrimary.setVisible(true);
buttonSecondary.setVisible(false);
}
}

// @Autowired()
// private SamplePersonService samplePersonService;
}
15 changes: 15 additions & 0 deletions src/main/java/dev/nathanlively/ServiceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.nathanlively;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Clock;

@Configuration
public class ServiceConfig {
@Bean
public ClockEventService clockEventService(EclipseClockRepository eclipseClockRepository) {
ClockRepository clockEclipseAdapter = new ClockEclipseAdapter(eclipseClockRepository);
return new ClockEventService(clockEclipseAdapter, Clock.systemDefaultZone());
}
}
46 changes: 30 additions & 16 deletions src/test/java/dev/nathanlively/views/clock/HomeViewTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package dev.nathanlively.views.clock;

import com.vaadin.flow.component.UI;
import dev.nathanlively.HomeView;
import com.vaadin.flow.component.html.ListItem;
import com.vaadin.flow.component.html.UnorderedList;
import dev.nathanlively.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

import static com.github.mvysny.kaributesting.v10.LocatorJ._get;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -13,11 +19,16 @@
class HomeViewTest extends KaribuTest {

private HomeView view;
private static final Logger log = LoggerFactory.getLogger(HomeViewTest.class);
private ClockRepository repository;
private ClockEvent clockInEvent;

@BeforeEach
public void login() {
UI.getCurrent().navigate(HomeView.class);
view = _get(HomeView.class);
repository = InMemoryClockRepository.createEmpty();
clockInEvent = new ClockEvent(ClockService.aug7at8am(), ClockEventType.IN);
ClockEventService clockEventService = new ClockEventService(repository, ClockService.fixedAtAug7at8am());
view = new HomeView(clockEventService);
}

@Test
Expand All @@ -27,17 +38,20 @@ public void getRequestToIndex_returnsView() {

@Test
void viewIndex_returnsListOfClockEventViews() {
// Arrange: Mock the ClockEventService to return a list of ClockEvent views
// List<ClockEvent> mockEvents = List.of(
// new ClockEvent(LocalDateTime.now(), ClockEventType.IN),
// new ClockEvent(LocalDateTime.now(), ClockEventType.OUT)
// );

// Act: Initialize the View and check if the list is injected correctly.
// HomeView homeView = new HomeView(clockEventService);
// UnorderedList eventsList = homeView.getContent().getComponentAt(0, UnorderedList.class);

// Assert: Check if the list contains the expected items
// assertThat(eventsList.getComponentCount()).isEqualTo(mockEvents.size());
repository.save(clockInEvent);
assertThat(repository.findAll()).hasSize(1);
UnorderedList eventList = _get(UnorderedList.class, spec -> spec.withId("events-list"));

assertThat(eventList).isNotNull();


List<ListItem> items = eventList.getChildren()
.filter(component -> component instanceof ListItem)
.map(component -> (ListItem) component)
.collect(Collectors.toList());

items.forEach(item -> log.info(item.getText()));

assertThat(items).isNotEmpty();
}
}

0 comments on commit 22d16d8

Please sign in to comment.