Skip to content

Commit

Permalink
Merge pull request #143 from contentstack/development
Browse files Browse the repository at this point in the history
Staging | DX-2186
  • Loading branch information
harshithad0703 authored Feb 13, 2025
2 parents 29f3ffc + 60e794a commit d880132
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
23 changes: 9 additions & 14 deletions __test__/mock/gql-asset-url-update-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,13 @@ export const gqlResponseForAssetUpdateMultipleEntries = {
"title": "merry-marketplace.png",
"url": "actual_asset_url.png",
"content_type": "image/png",
"description": null,
"description": "null",
"file_size": 273858,
"filename": "merry-marketplace.png",
"permanent_url": "Permanent URL Not Defined!"
}
},
{
"node": {
"title": "Screenshot.png",
"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",
"content_type": "image/png",
"description": "",
"file_size": 287954,
"filename": "Screenshot_2024-12-09_at_7.28.28_PM.png",
"permanent_url": "Permanent URL Not Defined!"
"permanent_url": "Permanent URL Not Defined!",
"system" : {
"uid": "asset_uid_1"
}
}
},
{
Expand All @@ -289,7 +281,10 @@ export const gqlResponseForAssetUpdateMultipleEntries = {
"description": "",
"file_size": 1050317,
"filename": "Aadhaar.pdf",
"permanent_url": "Permanent URL Not Defined!"
"permanent_url": "Permanent URL Not Defined!",
"system" : {
"uid": "asset_uid_2"
}
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions __test__/updateAssetURLForGQL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gqlResponseForAssetUpdate, gqlResponseForAssetUpdateWithoutSystemUid, g

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

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

Expand All @@ -15,19 +15,19 @@ describe('updateAssetURLForGQL test', () => {
done();
});

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

const rteField = testResponse.data.page_json_rte.items[0].body_new[0].body.body_12;
const assetLink = rteField.json.children[0].attrs['asset-link'];
const expectedUrl = rteField.embedded_itemsConnection.edges[0].node.url;

expect(assetLink).toBe(expectedUrl);
done();
});

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

const testResponse = { ...gqlResponseForAssetUpdateWithoutSystemUid };
Expand Down
35 changes: 24 additions & 11 deletions src/updateAssetURLForGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,34 @@ export function updateAssetURLForGQL(gqlResponse:any) {
function processEntry(entry:any) {
for (let field in entry) {
const fieldData = entry[field];
const rteField = findRTEField(fieldData)
const edges = rteField?.embedded_itemsConnection?.edges;
edges.forEach((edge:any) => {
const node = edge.node;
if (node?.url && node?.filename) {

if (!node?.system?.uid) throw new Error('Asset UID not found in the response');

const correspondingAsset = rteField?.json?.children?.find((child:any) => child.attrs['asset-uid'] === node.system.uid);
correspondingAsset.attrs['asset-link'] = node.url;
}
if (fieldData instanceof Array) {
fieldData.forEach((data:any) => {
findRTEFieldAndUpdateURL(data);
});
} else if (fieldData && typeof fieldData === 'object') {
findRTEFieldAndUpdateURL(fieldData);
}
}
}

function findRTEFieldAndUpdateURL(fieldData:any) {
const rteField = findRTEField(fieldData);

if (!rteField) return;

const edges = rteField?.embedded_itemsConnection?.edges;

Check warning on line 38 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 38 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
edges.forEach((edge:any) => {
const node = edge.node;
if (node?.url && node?.filename) {

Check warning on line 41 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 41 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

if (!node?.system?.uid) throw new Error('Asset UID not found in the response');

Check warning on line 43 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

const correspondingAsset = rteField?.json?.children?.find((child:any) => child.attrs['asset-uid'] === node.system.uid);

Check warning on line 45 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 45 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 45 in src/updateAssetURLForGQL.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
correspondingAsset.attrs['asset-link'] = node.url;
}
});
}

function findRTEField(fieldData: any): any {
if (fieldData && fieldData.embedded_itemsConnection) {
return fieldData;
Expand Down

0 comments on commit d880132

Please sign in to comment.