# ---- .prep -----------------------------------------------------------------

test_that(".prep errors on non-wid_df", {
  expect_error(widr:::.prep(data.frame(), NULL, NULL, NULL), "wid_df")
})

test_that(".prep errors when no rows remain after filtering", {
  expect_error(widr:::.prep(.wid(), NULL, "XX", NULL), "No data")
})

test_that(".prep applies country_labels recode", {
  p <- widr:::.prep(.wid(), NULL, NULL, c(US = "United States"))
  expect_true(any(p$d$country == "United States"))
})

test_that(".prep restricts to requested country", {
  d <- .wid(countries = c("US","FR"))
  p <- widr:::.prep(d, NULL, "US", NULL)
  expect_true(all(p$d$country == "US"))
})

# ---- .theme_wid ------------------------------------------------------------

test_that(".theme_wid returns a ggplot theme with dark background", {
  t <- widr:::.theme_wid()
  expect_s3_class(t, "theme")
  expect_equal(t$plot.background$fill, "#0d1117")
})

# ---- wid_plot_timeseries ---------------------------------------------------

test_that("wid_plot_timeseries returns ggplot", {
  d <- .wid(5); d$year <- 2016:2020
  expect_s3_class(wid_plot_timeseries(d), "ggplot")
})

test_that("wid_plot_timeseries applies dark theme", {
  d <- .wid(3); d$year <- 2018:2020
  p <- wid_plot_timeseries(d)
  expect_equal(p$theme$plot.background$fill, "#0d1117")
})

test_that("wid_plot_timeseries applies country_labels", {
  d <- .wid(3); d$year <- 2018:2020
  p <- wid_plot_timeseries(d, country_labels = c(US = "United States"))
  expect_true(any(p$data$country == "United States"))
})

test_that("wid_plot_timeseries facet=TRUE adds FacetWrap", {
  d <- .wid(5, countries = c("US","FR")); d$year <- rep(2016:2020, 2L)
  p <- wid_plot_timeseries(d, facet = TRUE)
  expect_true(inherits(p$facet, "FacetWrap"))
})

test_that("wid_plot_timeseries facet=FALSE has no FacetWrap", {
  d <- .wid(5); d$year <- 2016:2020
  p <- wid_plot_timeseries(d, facet = FALSE)
  expect_false(inherits(p$facet, "FacetWrap"))
})

# ---- wid_plot_compare ------------------------------------------------------

test_that("wid_plot_compare returns ggplot", {
  d <- .wid(2, countries = c("US","FR"))
  expect_s3_class(wid_plot_compare(d, year = 2020), "ggplot")
})

test_that("wid_plot_compare applies dark theme", {
  d <- .wid(2, countries = c("US","FR"))
  p <- wid_plot_compare(d, year = 2020)
  expect_equal(p$theme$plot.background$fill, "#0d1117")
})

test_that("wid_plot_compare type='point' returns ggplot", {
  d <- .wid(2, countries = c("US","FR"))
  expect_s3_class(wid_plot_compare(d, year = 2020, type = "point"), "ggplot")
})

test_that("wid_plot_compare errors when year not in data", {
  d <- .wid(2, countries = c("US","FR"))
  expect_error(wid_plot_compare(d, year = 1800), "No data for year")
})

test_that("wid_plot_compare uses most recent common year when year=NULL", {
  d <- .wid(2, countries = c("US","FR"))
  p <- wid_plot_compare(d)
  expect_s3_class(p, "ggplot")
})

# ---- wid_plot_lorenz -------------------------------------------------------

test_that("wid_plot_lorenz returns ggplot", {
  expect_s3_class(wid_plot_lorenz(.wid_dist()), "ggplot")
})

test_that("wid_plot_lorenz errors on non-wid_df", {
  expect_error(wid_plot_lorenz(data.frame()), "wid_df")
})

test_that("wid_plot_lorenz errors on non-share series", {
  expect_error(wid_plot_lorenz(.wid(type = "t")), "share")
})

test_that("wid_plot_lorenz errors on empty wid_df", {
  expect_error(wid_plot_lorenz(widr:::.wid_df()), "empty")
})

test_that("wid_plot_lorenz warns and drops unparseable percentile rows", {
  d              <- .wid_dist()
  d$percentile[1L] <- "bad"
  expect_warning(out <- wid_plot_lorenz(d), "dropped")
  expect_s3_class(out, "ggplot")
})

test_that("wid_plot_lorenz applies dark theme", {
  p <- wid_plot_lorenz(.wid_dist())
  expect_equal(p$theme$plot.background$fill, "#0d1117")
})

test_that("wid_plot_lorenz applies country_labels", {
  d <- rbind(.wid_dist("US"), .wid_dist("FR"))
  p <- wid_plot_lorenz(d, country_labels = c(US = "United States"))
  expect_true(any(p$data$country == "United States"))
})