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