import { InMemoryLocationRepository } from "../adapters/inMemoryLocationsRepository";
import Location from "../entities/location";
import createLocation, { CreateLocationData } from "./createLocation";
import Presenter from "./presenter";

export class DummyPresenter<T> implements Presenter<T> {
  constructor(public result?: T) {}

  show(result: T): void {
    this.result = result;
  }
}

describe("use case createLocation", () => {
  it("persist a location", () => {
    const cmd: CreateLocationData = {
      name: "Bourg-Achard",
      lat: 49.443232,
      lon: 1.099971,
    };
    const presenter = new DummyPresenter<Location>();
    const repository = new InMemoryLocationRepository();

    createLocation(repository, presenter, cmd);

    expect(repository.locations).toHaveLength(1);
    const newLocation = repository.locations[0];

    expect(newLocation.name).toEqual(cmd.name);
    expect(newLocation.lat).toEqual(cmd.lat);
    expect(newLocation.lon).toEqual(cmd.lon);
  });
});
