import { render, screen, fireEvent } from "@testing-library/react"
import { PreferencesApp } from "../PreferencesApp"

describe("PreferencesApp", () => {
  it("renders the preference center", () => {
    render(<PreferencesApp></PreferencesApp>)

    expect(screen.getByText("Preferences Center")).toBeInTheDocument()
  })

  it("allows user to uncheck all boxes with unsubscribe all", () => {
    render(<PreferencesApp></PreferencesApp>)

    let checkboxes = screen.getAllByRole("checkbox")
    let unsubscribeFromAllCheckbox = checkboxes[checkboxes.length - 1]

    // Unsubscribe from all defaults to checked, so we click it twice!
    fireEvent.click(unsubscribeFromAllCheckbox)
    fireEvent.click(unsubscribeFromAllCheckbox)

    expect(screen.getAllByRole("checkbox")[checkboxes.length - 1]).toBeChecked()

    checkboxes.pop()

    checkboxes.forEach(checkbox => {
      expect(checkbox).not.toBeChecked()
    })
  })
})

import { render, screen, fireEvent } from "@testing-library/react"
import { PreferencesApp } from "../PreferencesApp"

describe("PreferencesApp", () => {
  it("renders the preference center", () => {
    render(<PreferencesApp></PreferencesApp>)

    expect(screen.getByText("Preferences Center")).toBeInTheDocument()
  })

  it("allows user to uncheck all boxes with unsubscribe from all", () => {
    render(<PreferencesApp></PreferencesApp>)

    expect(screen.getByText("Subscribe to all")).toBeInTheDocument()

    let checkboxes = screen.getAllByRole("checkbox")
    let unsubscribeFromAllCheckbox = checkboxes.pop()!

    fireEvent.click(checkboxes[3])
    fireEvent.click(checkboxes[4])

    fireEvent.click(unsubscribeFromAllCheckbox)

    expect(unsubscribeFromAllCheckbox).toBeChecked()

    checkboxes?.forEach(checkbox => {
      expect(checkbox).not.toBeChecked()
    })
  })

  it("unchecks unsubscribe/subscribe all when other checkboxes are checked", () => {
    render(<PreferencesApp></PreferencesApp>)

    expect(screen.getByText("Unsubscribe from all")).toBeInTheDocument()

    let checkboxes = screen.getAllByRole("checkbox")
    let subscribeToAllCheckbox = checkboxes[0]
    let unsubscribeFromAllCheckbox = checkboxes.pop()

    fireEvent.click(checkboxes[3])

    expect(subscribeToAllCheckbox).not.toBeChecked()
    expect(unsubscribeFromAllCheckbox).not.toBeChecked()
  })

  it("allows user to check all boxes with subscribe to all", () => {
    render(<PreferencesApp></PreferencesApp>)

    expect(screen.getByText("Unsubscribe from all")).toBeInTheDocument()

    let checkboxes = screen.getAllByRole("checkbox")
    let subscribeToAllCheckbox = checkboxes[0]
    let unsubscribeFromAllCheckbox = checkboxes.pop()

    fireEvent.click(subscribeToAllCheckbox)

    expect(subscribeToAllCheckbox).toBeChecked()

    expect(unsubscribeFromAllCheckbox).not.toBeChecked()

    checkboxes.forEach(checkbox => {
      expect(checkbox).toBeChecked()
    })
  })
})

Expand testing for the preference center
chore: Expand testing for the preference center
