|
| 1 | +import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
| 2 | +import { |
| 3 | + adminHeaders, |
| 4 | + createAdminUser, |
| 5 | +} from "../../../../helpers/create-admin-user" |
| 6 | + |
| 7 | +jest.setTimeout(100000) |
| 8 | + |
| 9 | +process.env.MEDUSA_FF_TRANSLATION = "true" |
| 10 | + |
| 11 | +medusaIntegrationTestRunner({ |
| 12 | + testSuite: ({ dbConnection, getContainer, api }) => { |
| 13 | + describe("Admin Locale API", () => { |
| 14 | + beforeEach(async () => { |
| 15 | + await createAdminUser(dbConnection, adminHeaders, getContainer()) |
| 16 | + }) |
| 17 | + |
| 18 | + afterAll(async () => { |
| 19 | + delete process.env.MEDUSA_FF_TRANSLATION |
| 20 | + }) |
| 21 | + |
| 22 | + describe("GET /admin/locales", () => { |
| 23 | + it("should list all default locales", async () => { |
| 24 | + const response = await api.get("/admin/locales", adminHeaders) |
| 25 | + |
| 26 | + expect(response.status).toEqual(200) |
| 27 | + expect(response.data.locales.length).toBeGreaterThanOrEqual(45) |
| 28 | + expect(response.data.count).toBeGreaterThanOrEqual(45) |
| 29 | + expect(response.data.locales).toEqual( |
| 30 | + expect.arrayContaining([ |
| 31 | + expect.objectContaining({ |
| 32 | + code: "en-US", |
| 33 | + name: "English (United States)", |
| 34 | + }), |
| 35 | + expect.objectContaining({ |
| 36 | + code: "fr-FR", |
| 37 | + name: "French (France)", |
| 38 | + }), |
| 39 | + expect.objectContaining({ |
| 40 | + code: "de-DE", |
| 41 | + name: "German (Germany)", |
| 42 | + }), |
| 43 | + expect.objectContaining({ |
| 44 | + code: "es-ES", |
| 45 | + name: "Spanish (Spain)", |
| 46 | + }), |
| 47 | + expect.objectContaining({ |
| 48 | + code: "ja-JP", |
| 49 | + name: "Japanese (Japan)", |
| 50 | + }), |
| 51 | + ]) |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should filter locales by code", async () => { |
| 56 | + const response = await api.get( |
| 57 | + "/admin/locales?code=en-US", |
| 58 | + adminHeaders |
| 59 | + ) |
| 60 | + |
| 61 | + expect(response.status).toEqual(200) |
| 62 | + expect(response.data.locales).toHaveLength(1) |
| 63 | + expect(response.data.locales[0]).toEqual( |
| 64 | + expect.objectContaining({ |
| 65 | + code: "en-US", |
| 66 | + name: "English (United States)", |
| 67 | + }) |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + it("should filter locales by multiple codes", async () => { |
| 72 | + const response = await api.get( |
| 73 | + "/admin/locales?code[]=en-US&code[]=fr-FR&code[]=de-DE", |
| 74 | + adminHeaders |
| 75 | + ) |
| 76 | + |
| 77 | + expect(response.status).toEqual(200) |
| 78 | + expect(response.data.locales).toHaveLength(3) |
| 79 | + expect(response.data.locales).toEqual( |
| 80 | + expect.arrayContaining([ |
| 81 | + expect.objectContaining({ code: "en-US" }), |
| 82 | + expect.objectContaining({ code: "fr-FR" }), |
| 83 | + expect.objectContaining({ code: "de-DE" }), |
| 84 | + ]) |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + it("should filter locales using q parameter", async () => { |
| 89 | + const response = await api.get( |
| 90 | + "/admin/locales?q=french", |
| 91 | + adminHeaders |
| 92 | + ) |
| 93 | + |
| 94 | + expect(response.status).toEqual(200) |
| 95 | + expect(response.data.locales.length).toBeGreaterThanOrEqual(1) |
| 96 | + expect(response.data.locales).toEqual( |
| 97 | + expect.arrayContaining([ |
| 98 | + expect.objectContaining({ |
| 99 | + code: "fr-FR", |
| 100 | + name: "French (France)", |
| 101 | + }), |
| 102 | + ]) |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + it("should support pagination", async () => { |
| 107 | + const response = await api.get( |
| 108 | + "/admin/locales?limit=5&offset=0", |
| 109 | + adminHeaders |
| 110 | + ) |
| 111 | + |
| 112 | + expect(response.status).toEqual(200) |
| 113 | + expect(response.data.locales).toHaveLength(5) |
| 114 | + expect(response.data.limit).toEqual(5) |
| 115 | + expect(response.data.offset).toEqual(0) |
| 116 | + expect(response.data.count).toBeGreaterThanOrEqual(45) |
| 117 | + |
| 118 | + const response2 = await api.get( |
| 119 | + "/admin/locales?limit=5&offset=5", |
| 120 | + adminHeaders |
| 121 | + ) |
| 122 | + |
| 123 | + expect(response2.status).toEqual(200) |
| 124 | + expect(response2.data.locales).toHaveLength(5) |
| 125 | + expect(response2.data.offset).toEqual(5) |
| 126 | + |
| 127 | + const firstPageCodes = response.data.locales.map((l) => l.code) |
| 128 | + const secondPageCodes = response2.data.locales.map((l) => l.code) |
| 129 | + const overlap = firstPageCodes.filter((c) => |
| 130 | + secondPageCodes.includes(c) |
| 131 | + ) |
| 132 | + expect(overlap).toHaveLength(0) |
| 133 | + }) |
| 134 | + |
| 135 | + it("should return locales with expected fields", async () => { |
| 136 | + const response = await api.get( |
| 137 | + "/admin/locales?code=en-US", |
| 138 | + adminHeaders |
| 139 | + ) |
| 140 | + |
| 141 | + expect(response.status).toEqual(200) |
| 142 | + expect(response.data.locales[0]).toHaveProperty("code") |
| 143 | + expect(response.data.locales[0]).toHaveProperty("name") |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + describe("GET /admin/locales/:code", () => { |
| 148 | + it("should retrieve a locale by code", async () => { |
| 149 | + const response = await api.get("/admin/locales/en-US", adminHeaders) |
| 150 | + |
| 151 | + expect(response.status).toEqual(200) |
| 152 | + expect(response.data.locale).toEqual( |
| 153 | + expect.objectContaining({ |
| 154 | + code: "en-US", |
| 155 | + name: "English (United States)", |
| 156 | + }) |
| 157 | + ) |
| 158 | + }) |
| 159 | + |
| 160 | + it("should retrieve different locales by code", async () => { |
| 161 | + const frResponse = await api.get("/admin/locales/fr-FR", adminHeaders) |
| 162 | + expect(frResponse.status).toEqual(200) |
| 163 | + expect(frResponse.data.locale).toEqual( |
| 164 | + expect.objectContaining({ |
| 165 | + code: "fr-FR", |
| 166 | + name: "French (France)", |
| 167 | + }) |
| 168 | + ) |
| 169 | + |
| 170 | + const deResponse = await api.get("/admin/locales/de-DE", adminHeaders) |
| 171 | + expect(deResponse.status).toEqual(200) |
| 172 | + expect(deResponse.data.locale).toEqual( |
| 173 | + expect.objectContaining({ |
| 174 | + code: "de-DE", |
| 175 | + name: "German (Germany)", |
| 176 | + }) |
| 177 | + ) |
| 178 | + |
| 179 | + const jaResponse = await api.get("/admin/locales/ja-JP", adminHeaders) |
| 180 | + expect(jaResponse.status).toEqual(200) |
| 181 | + expect(jaResponse.data.locale).toEqual( |
| 182 | + expect.objectContaining({ |
| 183 | + code: "ja-JP", |
| 184 | + name: "Japanese (Japan)", |
| 185 | + }) |
| 186 | + ) |
| 187 | + }) |
| 188 | + |
| 189 | + it("should return 404 for non-existent locale", async () => { |
| 190 | + const response = await api |
| 191 | + .get("/admin/locales/xx-XX", adminHeaders) |
| 192 | + .catch((e) => e.response) |
| 193 | + |
| 194 | + expect(response.status).toEqual(404) |
| 195 | + }) |
| 196 | + }) |
| 197 | + }) |
| 198 | + }, |
| 199 | +}) |
0 commit comments