Files
medusa-store/integration-tests/http/__tests__/tax-region/admin/tax-region.spec.ts
T
Carlos R. L. Rodrigues e413cfefc2 feat(utils): define file config (#13283)
** What
 - Allow auto-loaded Medusa files to export a config object.
 - Currently supports isDisabled to control loading.
 - new instance `FeatureFlag` exported by `@medusajs/framework/utils`
 - `feature-flags` is now a supported folder for medusa projects, modules, providers and plugins. They will be loaded and added to `FeatureFlag`

** Why
 - Enables conditional loading of routes, migrations, jobs, subscribers, workflows, and other files based on feature flags.

```ts
// /src/feature-flags

import { FlagSettings } from "@medusajs/framework/feature-flags"

const CustomFeatureFlag: FlagSettings = {
  key: "custom_feature",
  default_val: false,
  env_key: "FF_MY_CUSTOM_FEATURE",
  description: "Enable xyz",
}

export default CustomFeatureFlag
```

```ts
// /src/modules/my-custom-module/migration/Migration20250822135845.ts

import { FeatureFlag } from "@medusajs/framework/utils"

export class Migration20250822135845 extends Migration {
  override async up(){ }
  override async down(){ }
}

defineFileConfig({
  isDisabled: () => !FeatureFlag.isFeatureEnabled("custom_feature")
})
```
2025-08-26 12:22:30 +00:00

160 lines
4.6 KiB
TypeScript

import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
jest.setTimeout(50000)
const env = {}
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("/admin/tax-regions", () => {
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, getContainer())
})
describe("POST /admin/tax-regions/:id", () => {
let taxRegion
beforeEach(async () => {
const parentRegion = await api.post(
"/admin/tax-regions",
{
country_code: "us",
provider_id: "tp_system",
},
adminHeaders
)
taxRegion = (
await api.post(
"/admin/tax-regions",
{
country_code: "us",
province_code: "tx",
parent_id: parentRegion.data.tax_region.id,
metadata: { test: "created" },
},
adminHeaders
)
).data.tax_region
})
it("should successfully update a tax region's fieleds", async () => {
let taxRegionResponse = await api.post(
`/admin/tax-regions/${taxRegion.id}`,
{
province_code: "ny",
metadata: { test: "updated" },
},
adminHeaders
)
expect(taxRegionResponse.status).toEqual(200)
expect(taxRegionResponse.data.tax_region).toEqual(
expect.objectContaining({
id: expect.any(String),
province_code: "ny",
metadata: { test: "updated" },
})
)
taxRegionResponse = await api.post(
`/admin/tax-regions/${taxRegion.id}`,
{ metadata: { test: "updated 2" } },
adminHeaders
)
expect(taxRegionResponse.status).toEqual(200)
expect(taxRegionResponse.data.tax_region).toEqual(
expect.objectContaining({
id: expect.any(String),
province_code: "ny",
metadata: { test: "updated 2" },
})
)
taxRegionResponse = await api.post(
`/admin/tax-regions/${taxRegion.id}`,
{ province_code: "ca" },
adminHeaders
)
expect(taxRegionResponse.status).toEqual(200)
expect(taxRegionResponse.data.tax_region).toEqual(
expect.objectContaining({
id: expect.any(String),
province_code: "ca",
metadata: { test: "updated 2" },
})
)
})
it("should create a province tax region without a provider", async () => {
const response = await api.post(
`/admin/tax-regions`,
{
country_code: "us",
parent_id: taxRegion.id,
province_code: "ny",
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.tax_region).toEqual(
expect.objectContaining({
id: expect.any(String),
country_code: "us",
province_code: "ny",
provider_id: null,
})
)
})
it("should fail to create a country tax region without a provider", async () => {
const {
response: { status, data },
} = await api
.post(
`/admin/tax-regions`,
{
country_code: "uk",
},
adminHeaders
)
.catch((err) => err)
expect(status).toEqual(400)
expect(data).toEqual({
message:
"Invalid request: Provider is required when creating a non-province tax region.",
type: "invalid_data",
})
})
it("should throw if tax region does not exist", async () => {
const {
response: { status, data },
} = await api
.post(
`/admin/tax-regions/does-not-exist`,
{ province_code: "ny", metadata: { test: "updated" } },
adminHeaders
)
.catch((err) => err)
expect(status).toEqual(404)
expect(data).toEqual({
message: 'TaxRegion with id "does-not-exist" not found',
type: "not_found",
})
})
})
})
},
})