<commit_before>import { graphql } from "react-relay"

import { createTestEnv } from "v2/DevTools/createTestEnv"

import { RequestConditionReportQueryResponse } from "v2/__generated__/RequestConditionReportQuery.graphql"
import { AnalyticsSchema as Schema } from "v2/System"
import { RequestConditionReportFragmentContainer } from "../RequestConditionReport"
import { RequestConditionReportTestPage } from "./Utils/RequestConditionReportTestPage"
import { mediator } from "lib/mediator"

jest.unmock("react-relay")
jest.unmock("react-tracking")
jest.mock("v2/Utils/Events", () => ({
  postEvent: jest.fn(),
}))
const mockPostEvent = require("v2/Utils/Events").postEvent as jest.Mock

const setupTestEnv = () => {
  return createTestEnv({
    TestPage: RequestConditionReportTestPage,
    Component: (props: RequestConditionReportQueryResponse) => (
      // @ts-expect-error PLEASE_FIX_ME_STRICT_NULL_CHECK_MIGRATION
      <RequestConditionReportFragmentContainer {...props} />
    ),
    query: graphql`
      query RequestConditionReportTestQuery
        @raw_response_type
        @relay_test_operation {
        me {
          ...RequestConditionReport_me
        }

        artwork(id: "artwork-id") {
          ...RequestConditionReport_artwork
        }
      }
    `,
    defaultData: {
      me: { internalID: "user-id", email: "<EMAIL>" },
      artwork: {
        internalID: "artwork-id",
        slug: "artwork-slug",
        saleArtwork: { internalID: "sale-artwork-id" },
      },
    },
    defaultMutationResults: {
      requestConditionReport: {},
    },
    systemContextProps: { mediator },
  })
}

describe("RequestConditionReport", () => {
  beforeAll(() => {
    mediator.on("open:auth", () => {})
  })

  beforeAll(() => {
    jest.spyOn(mediator, "trigger")
  })

  it("requests a condition report and tracks click event", async () => {
    const env = setupTestEnv()

    env.mutations.useResultsOnce({
      requestConditionReport: {
        conditionReportRequest: { internalID: "condition-report-request-id" },
      },
    })

    const page = await env.buildPage()

    await page.clickRequestConditionReportButton()

    expect(mockPostEvent).toHaveBeenCalledWith({
      action_type: Schema.ActionType.ClickedRequestConditionReport,
      subject: Schema.Subject.RequestConditionReport,
      context_page: Schema.PageName.ArtworkPage,
      context_module: Schema.ContextModule.AboutTheWorkCondition,
      context_page_owner_id: "artwork-id",
      context_page_owner_slug: "artwork-slug",
      context_page_owner_type: "Artwork",
      sale_artwork_id: "sale-artwork-id",
    })

    expect(page.text()).toContain("Condition report requested")
  })

  it("shows a modal if the mutation fails", async () => {
    const env = setupTestEnv()
    env.mutations.useResultsOnce({<commit_msg>fix: fixes spec (#10299)<commit_after>import { graphql } from "react-relay"

import { createTestEnv } from "v2/DevTools/createTestEnv"

import { RequestConditionReportQueryResponse } from "v2/__generated__/RequestConditionReportQuery.graphql"
import { AnalyticsSchema as Schema } from "v2/System"
import { RequestConditionReportFragmentContainer } from "../RequestConditionReport"
import { RequestConditionReportTestPage } from "./Utils/RequestConditionReportTestPage"
import { mediator } from "lib/mediator"
import { Toasts, ToastsProvider } from "@artsy/palette"

jest.unmock("react-relay")
jest.unmock("react-tracking")
jest.mock("v2/Utils/Events", () => ({
  postEvent: jest.fn(),
}))
const mockPostEvent = require("v2/Utils/Events").postEvent as jest.Mock

jest.mock("@artsy/palette", () => {
  return {
    ...jest.requireActual("@artsy/palette"),
    ModalDialog: ({ title, children }) => (
      <div>
        {title}
        <div>{children}</div>
      </div>
    ),
  }
})

const setupTestEnv = () => {
  return createTestEnv({
    TestPage: RequestConditionReportTestPage,
    Component: (props: RequestConditionReportQueryResponse) => (
      <ToastsProvider>
        <Toasts />
        {/* @ts-ignore */}
        <RequestConditionReportFragmentContainer {...props} />
      </ToastsProvider>
    ),
    query: graphql`
      query RequestConditionReportTestQuery
        @raw_response_type
        @relay_test_operation {
        me {
          ...RequestConditionReport_me
        }

        artwork(id: "artwork-id") {
          ...RequestConditionReport_artwork
        }
      }
    `,
    defaultData: {
      me: { internalID: "user-id", email: "<EMAIL>" },
      artwork: {
        internalID: "artwork-id",
        slug: "artwork-slug",
        saleArtwork: { internalID: "sale-artwork-id" },
      },
    },
    defaultMutationResults: {
      requestConditionReport: {},
    },
    systemContextProps: { mediator },
  })
}

describe("RequestConditionReport", () => {
  beforeAll(() => {
    mediator.on("open:auth", () => {})
  })

  beforeAll(() => {
    jest.spyOn(mediator, "trigger")
  })

  it("requests a condition report and tracks click event", async () => {
    const env = setupTestEnv()

    env.mutations.useResultsOnce({
      requestConditionReport: {
        conditionReportRequest: { internalID: "condition-report-request-id" },
      },
    })

    const page = await env.buildPage()

    await page.clickRequestConditionReportButton()

    expect(mockPostEvent).toHaveBeenCalledWith({
      action_type: Schema.ActionType.ClickedRequestConditionReport,
      subject: Schema.Subject.RequestConditionReport,
      context_page: Schema.PageName.ArtworkPage,
      context_module: Schema.ContextModule.AboutTheWorkCondition,
      context_page_owner_id: "artwork-id",
      context_page_owner_slug: "artwork-slug",
      context_page_owner_type: "Artwork",
      sale_artwork_id: "sale-artwork-id",
    })

    expect(page.text()).toContain("Condition report requested")
  })

  it("shows a toast if the mutation fails", async () => {
    const env = setupTestEnv()
    env.mutations.useResultsOnce({
{'fraction_non_alphanumeric': 0.13087248322147652, 'fraction_numerical': 0.0025167785234899327, 'mean_word_length': 3.383088235294118, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6991765', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1761, 'n_words': 305}