Skip to content

Commit 60e794a

Browse files
authored
Merge pull request #142 from contentstack/hotfix/updateAssetURLForGQL-enhancement
fix: update test for asset url method for gql
2 parents 53ec08e + a384722 commit 60e794a

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

__test__/mock/gql-asset-url-update-mock.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,13 @@ export const gqlResponseForAssetUpdateMultipleEntries = {
264264
"title": "merry-marketplace.png",
265265
"url": "actual_asset_url.png",
266266
"content_type": "image/png",
267-
"description": null,
267+
"description": "null",
268268
"file_size": 273858,
269269
"filename": "merry-marketplace.png",
270-
"permanent_url": "Permanent URL Not Defined!"
271-
}
272-
},
273-
{
274-
"node": {
275-
"title": "Screenshot.png",
276-
"url": "https://azure-na-images.contentstack.com/v3/assets/folder_uid/asset_uid_2/folder_uid_4/Screenshot_2024-12-09_at_7.28.28_PM.png?branch=test2",
277-
"content_type": "image/png",
278-
"description": "",
279-
"file_size": 287954,
280-
"filename": "Screenshot_2024-12-09_at_7.28.28_PM.png",
281-
"permanent_url": "Permanent URL Not Defined!"
270+
"permanent_url": "Permanent URL Not Defined!",
271+
"system" : {
272+
"uid": "asset_uid_1"
273+
}
282274
}
283275
},
284276
{
@@ -289,7 +281,10 @@ export const gqlResponseForAssetUpdateMultipleEntries = {
289281
"description": "",
290282
"file_size": 1050317,
291283
"filename": "Aadhaar.pdf",
292-
"permanent_url": "Permanent URL Not Defined!"
284+
"permanent_url": "Permanent URL Not Defined!",
285+
"system" : {
286+
"uid": "asset_uid_2"
287+
}
293288
}
294289
}
295290
]

__test__/updateAssetURLForGQL.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { gqlResponseForAssetUpdate, gqlResponseForAssetUpdateWithoutSystemUid, g
33

44
describe('updateAssetURLForGQL test', () => {
55

6-
it.skip('should update the asset URL in the GQL response when proper response is passed', done => {
6+
it('should update the asset URL in the GQL response when proper response is passed', done => {
77
const testResponse = { ...gqlResponseForAssetUpdate };
88
updateAssetURLForGQL(testResponse);
99

@@ -15,19 +15,19 @@ describe('updateAssetURLForGQL test', () => {
1515
done();
1616
});
1717

18-
it.skip('should update the asset URL in the GQL response when proper response is passed', done => {
18+
it('should update the asset URL in the GQL response with multiple entries when proper response is passed', done => {
1919
const testResponse = { ...gqlResponseForAssetUpdateMultipleEntries };
2020
updateAssetURLForGQL(testResponse);
2121

2222
const rteField = testResponse.data.page_json_rte.items[0].body_new[0].body.body_12;
2323
const assetLink = rteField.json.children[0].attrs['asset-link'];
2424
const expectedUrl = rteField.embedded_itemsConnection.edges[0].node.url;
25-
25+
2626
expect(assetLink).toBe(expectedUrl);
2727
done();
2828
});
2929

30-
it.skip('should throw error when system.uid is not present', done => {
30+
it('should throw error when system.uid is not present', done => {
3131
jest.spyOn(console, 'error').mockImplementation(() => {});
3232

3333
const testResponse = { ...gqlResponseForAssetUpdateWithoutSystemUid };

src/updateAssetURLForGQL.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,34 @@ export function updateAssetURLForGQL(gqlResponse:any) {
2020
function processEntry(entry:any) {
2121
for (let field in entry) {
2222
const fieldData = entry[field];
23-
const rteField = findRTEField(fieldData)
24-
const edges = rteField?.embedded_itemsConnection?.edges;
25-
edges.forEach((edge:any) => {
26-
const node = edge.node;
27-
if (node?.url && node?.filename) {
28-
29-
if (!node?.system?.uid) throw new Error('Asset UID not found in the response');
30-
31-
const correspondingAsset = rteField?.json?.children?.find((child:any) => child.attrs['asset-uid'] === node.system.uid);
32-
correspondingAsset.attrs['asset-link'] = node.url;
33-
}
23+
if (fieldData instanceof Array) {
24+
fieldData.forEach((data:any) => {
25+
findRTEFieldAndUpdateURL(data);
3426
});
27+
} else if (fieldData && typeof fieldData === 'object') {
28+
findRTEFieldAndUpdateURL(fieldData);
29+
}
3530
}
3631
}
3732

33+
function findRTEFieldAndUpdateURL(fieldData:any) {
34+
const rteField = findRTEField(fieldData);
35+
36+
if (!rteField) return;
37+
38+
const edges = rteField?.embedded_itemsConnection?.edges;
39+
edges.forEach((edge:any) => {
40+
const node = edge.node;
41+
if (node?.url && node?.filename) {
42+
43+
if (!node?.system?.uid) throw new Error('Asset UID not found in the response');
44+
45+
const correspondingAsset = rteField?.json?.children?.find((child:any) => child.attrs['asset-uid'] === node.system.uid);
46+
correspondingAsset.attrs['asset-link'] = node.url;
47+
}
48+
});
49+
}
50+
3851
function findRTEField(fieldData: any): any {
3952
if (fieldData && fieldData.embedded_itemsConnection) {
4053
return fieldData;

0 commit comments

Comments
 (0)