Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 34b4055

Browse files
fix: finding matching preset when names aren't unique
1 parent 73186d0 commit 34b4055

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/commands/components/push/actions.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ const mockComponentPresetExisting: SpaceComponentPreset = {
7373
description: '',
7474
};
7575

76+
const mockComponentPresetExistingDifferentComponent: SpaceComponentPreset = {
77+
id: 3,
78+
// component present names aren't globally unique, they're only unique for each component
79+
name: 'preset-name-2',
80+
component_id: 2,
81+
preset: { field: 'value' },
82+
space_id: 12345,
83+
created_at: '2021-08-09T12:00:00Z',
84+
updated_at: '2021-08-09T12:00:00Z',
85+
image: '',
86+
color: '',
87+
icon: '',
88+
description: '',
89+
};
90+
7691
const mockInternalTag: SpaceComponentInternalTag = {
7792
id: 1,
7893
name: 'tag-name',
@@ -91,7 +106,6 @@ const handlers = [
91106
const token = request.headers.get('Authorization');
92107
if (token === 'valid-token') {
93108
const body: any = await request.json();
94-
// TODO: verify whether it's correct that component don't have a component body field
95109
if (body.id === mockComponentExisting.id) {
96110
return HttpResponse.json({ name: ['has already been taken'] }, { status: 422 });
97111
}
@@ -128,7 +142,6 @@ const handlers = [
128142
const token = request.headers.get('Authorization');
129143
if (token === 'valid-token') {
130144
const body: any = await request.json();
131-
// TODO: verify whether it's correct that component groups don't have a component_group body field
132145
if (body.id === mockComponentPresetExisting.id) {
133146
return HttpResponse.json({ name: ['has already been taken'] }, { status: 422 });
134147
}
@@ -165,7 +178,7 @@ const handlers = [
165178
const token = request.headers.get('Authorization');
166179
if (token === 'valid-token') {
167180
const body: any = await request.json();
168-
if (body.preset.id === mockComponentPresetExisting.id) {
181+
if (body.preset.id === mockComponentPresetExisting.id || body.preset.id === mockComponentPresetExistingDifferentComponent.id) {
169182
return HttpResponse.json({ name: ['has already been taken'] }, { status: 422 });
170183
}
171184
else {
@@ -184,7 +197,7 @@ const handlers = [
184197
http.get('https://api.storyblok.com/v1/spaces/12345/presets', async ({ request }) => {
185198
const token = request.headers.get('Authorization');
186199
if (token === 'valid-token') {
187-
return HttpResponse.json({ presets: [mockComponentPresetExisting] });
200+
return HttpResponse.json({ presets: [mockComponentPresetExisting, mockComponentPresetExistingDifferentComponent] });
188201
}
189202
return new HttpResponse('Unauthorized', { status: 401 });
190203
}),
@@ -195,13 +208,19 @@ const handlers = [
195208
}
196209
return new HttpResponse('Unauthorized', { status: 401 });
197210
}),
211+
http.put('https://api.storyblok.com/v1/spaces/12345/presets/3', async ({ request }) => {
212+
const token = request.headers.get('Authorization');
213+
if (token === 'valid-token') {
214+
return HttpResponse.json({ preset: mockComponentPresetExistingDifferentComponent });
215+
}
216+
return new HttpResponse('Unauthorized', { status: 401 });
217+
}),
198218

199219
// Internal tag handlers
200220
http.post('https://api.storyblok.com/v1/spaces/12345/internal_tags', async ({ request }) => {
201221
const token = request.headers.get('Authorization');
202222
if (token === 'valid-token') {
203223
const body: any = await request.json();
204-
// TODO: verify whether it's correct that component internal tags don't have a internal_tag body field
205224
if (body.id === mockInternalTagExisting.id) {
206225
return HttpResponse.json({ name: ['has already been taken'] }, { status: 422 });
207226
}
@@ -335,6 +354,11 @@ describe('push components actions', () => {
335354
const result = await upsertComponentPreset('12345', mockComponentPresetExisting, 'valid-token', 'eu');
336355
expect(result).toEqual(mockComponentPresetExisting);
337356
});
357+
358+
it('should upsert existing component preset with a duplicated name successfully with a valid token', async () => {
359+
const result = await upsertComponentPreset('12345', mockComponentPresetExistingDifferentComponent, 'valid-token', 'eu');
360+
expect(result).toEqual(mockComponentPresetExistingDifferentComponent);
361+
});
338362
});
339363

340364
describe('component internal tag', () => {

src/commands/components/push/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export const upsertComponentPreset = async (space: string, preset: Partial<Space
181181
if (responseData?.name?.[0] === 'has already been taken') {
182182
// Find existing preset by name
183183
const existingPresets = await fetchComponentPresets(space, token, region);
184-
const existingPreset = existingPresets?.find(p => p.name === preset.name);
184+
const existingPreset = existingPresets?.find(p => p.name === preset.name && p.component_id === preset.component_id);
185185
if (existingPreset) {
186186
// Update existing preset
187187
return await updateComponentPreset(space, existingPreset.id, { preset }, token, region);

0 commit comments

Comments
 (0)