Files
medusa-store/packages/medusa-react/test/hooks/admin/discounts/mutations.test.ts
Zakaria El Asri 2e384842d5 feat: medusa-react admin hooks (#978)
* add: medusa admin hooks + tests

* fix: remove unneeded props

* fix: deps

* fix: deps

* fix: deps

* fix: failing tests

* fix: failing tests

* fix: query key

* add: yarn workspaces

* fix: linting medusa-react

* fix: add prepare script

* fix: buildOptions

* fix: useAdminShippingOptions query

* fix: use qs instead for query params (#1019)

* fix: formatting

* debug: ci pipeline

* debug: log node_modules structure

* debug: use lerna bootstrap

* debug: update node version

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: add explicit build step

* fix: jsdoc

* debug: run build step

* debug: fix build errors

* debug: add build step to integration tests

* fix: failing test

* cleanup

Co-authored-by: Sebastian Rindom <seb@medusajs.com>
Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
2022-02-02 17:10:56 +01:00

233 lines
5.9 KiB
TypeScript

import {
useAdminCreateDiscount,
useAdminUpdateDiscount,
useAdminDeleteDiscount,
useAdminDiscountAddRegion,
useAdminDiscountAddValidProduct,
useAdminDiscountRemoveRegion,
useAdminDiscountRemoveValidProduct,
useAdminCreateDynamicDiscountCode,
useAdminDeleteDynamicDiscountCode,
} from "../../../../src/"
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { createWrapper } from "../../../utils"
describe("useAdminCreateDiscount hook", () => {
test("creates a discount and returns it", async () => {
const discount = {
code: "DISC",
rule: {
type: "percentage",
value: 10,
allocation: "total",
},
is_dynamic: false,
is_disabled: false,
}
const { result, waitFor } = renderHook(() => useAdminCreateDiscount(), {
wrapper: createWrapper(),
})
result.current.mutate(discount)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining({
...fixtures.get("discount"),
...discount,
})
)
})
})
describe("useAdminUpdateDiscount hook", () => {
test("updates a discount and returns it", async () => {
const discount = {
code: "SUMMER10",
}
const { result, waitFor } = renderHook(
() => useAdminUpdateDiscount(fixtures.get("discount").id),
{
wrapper: createWrapper(),
}
)
result.current.mutate(discount)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining({
...fixtures.get("discount"),
...discount,
})
)
})
})
describe("useAdminDeleteDiscount hook", () => {
test("updates a discount and returns it", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(() => useAdminDeleteDiscount(id), {
wrapper: createWrapper(),
})
result.current.mutate()
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data).toEqual(
expect.objectContaining({
id,
deleted: true,
})
)
})
})
describe("useAdminDiscountAddRegion hook", () => {
test("adds a region to the discount", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(
() => useAdminDiscountAddRegion(id),
{
wrapper: createWrapper(),
}
)
result.current.mutate("test-region")
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining(fixtures.get("discount"))
)
})
})
describe("useAdminDiscountAddValidProduct hook", () => {
test("adds a product to the discount", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(
() => useAdminDiscountAddValidProduct(id),
{
wrapper: createWrapper(),
}
)
result.current.mutate("test-product")
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining(fixtures.get("discount"))
)
})
})
describe("useAdminCreateDynamicDiscountCode hook", () => {
test("creates a dynamic discount code", async () => {
const discount = {
code: "LUCKY10",
usage_limit: 10,
}
const { result, waitFor } = renderHook(
() => useAdminCreateDynamicDiscountCode(fixtures.get("discount").id),
{
wrapper: createWrapper(),
}
)
result.current.mutate(discount)
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining({
...fixtures.get("discount"),
...discount,
})
)
})
})
describe("useAdminDiscountRemoveRegion hook", () => {
test("adds a region to the discount", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(
() => useAdminDiscountRemoveRegion(id),
{
wrapper: createWrapper(),
}
)
result.current.mutate("test-region")
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining(fixtures.get("discount"))
)
})
})
describe("useAdminDiscountRemoveValidProduct hook", () => {
test("adds a product to the discount", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(
() => useAdminDiscountRemoveValidProduct(id),
{
wrapper: createWrapper(),
}
)
result.current.mutate("test-product")
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining(fixtures.get("discount"))
)
})
})
describe("useAdminDeleteDynamicDiscountCode hook", () => {
test("creates a dynamic discount code", async () => {
const id = fixtures.get("discount").id
const { result, waitFor } = renderHook(
() => useAdminDeleteDynamicDiscountCode(fixtures.get("discount").id),
{
wrapper: createWrapper(),
}
)
result.current.mutate("LUCKY10")
await waitFor(() => result.current.isSuccess)
expect(result.current.data.response.status).toEqual(200)
expect(result.current.data.discount).toEqual(
expect.objectContaining(fixtures.get("discount"))
)
})
})