Skip to content

Commit

Permalink
ZETA-7644: fix multiple upload logic
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieGreenman committed Jan 14, 2024
1 parent a373027 commit 7595cb1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
74 changes: 68 additions & 6 deletions src/rz/json/add-json/add-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { morphCode } from '../../morph';
import { EditJson } from './../interfaces/json-morph.interface';
import { addJsonKeyValue } from './add-json';

Expand All @@ -14,13 +15,21 @@ describe('addJson', () => {
}
}`;

const mockEditJson: EditJson = {
nodeType: 'addJsonKeyValue',
valueToModify: 'targets',
codeBlock: codeBlock
const mockAddJson: EditJson = {
nodeType: 'addJsonKeyValue',
valueToModify: 'targets',
codeBlock: codeBlock
}

const editInput: any = {
fileType: 'json',
fileToBeAddedTo: mockJson,
edits: [
mockAddJson
]
};

const modifiedJson = addJsonKeyValue(mockEditJson, mockJson);
const modifiedJson = morphCode(editInput);
const expected = {
"test": "123",
"targets": {
Expand All @@ -30,7 +39,9 @@ describe('addJson', () => {
}
};

expect(modifiedJson).toEqual(expected);
const result = JSON.parse(modifiedJson);

expect(result).toEqual(expected);
});

it('should add a key value', () => {
Expand All @@ -56,6 +67,57 @@ describe('addJson', () => {

expect(modifiedJson).toEqual(expected);
});

it('should add json key value if nested json being added and edit json', () => {
const mockJson = `{
"scripts": {
"test": "npm run test"
},
"test": "123",
"targets": {}
}`;

const codeBlock = `{
"server": {
"executor": "@angular-devkit/build-angular:server"
}
}`;

const mockAddJson: EditJson = {
nodeType: 'addJsonKeyValue',
valueToModify: 'targets',
codeBlock: codeBlock
}

const mockEditJson: EditJson = {
nodeType: 'editJson',
valueToModify: '/contributes/menus',
codeBlock: {data: "test"}
}

const editInput: any = {
fileType: 'json',
fileToBeAddedTo: mockJson,
edits: [
mockAddJson,
mockEditJson
]
};

const modifiedJson = morphCode(editInput);
const expected = {
"test": "123",
"targets": {
"server": {
"executor": "@angular-devkit/build-angular:server"
}
}
};

const result = JSON.parse(modifiedJson);

expect(result).toEqual(expected);
});

});

Expand Down
2 changes: 1 addition & 1 deletion src/rz/json/add-json/add-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as pointer from 'json-pointer';
export function addJsonKeyValue(editJson: EditJson, json: string): any {
// double json parse hack to make sure /n are removed from string
const codeBlock = typeof editJson.codeBlock === 'string' ? JSON.parse(JSON.parse(JSON.stringify(editJson.codeBlock))) : editJson.codeBlock;
json = JSON.parse(json);
json = typeof json === 'string' ? JSON.parse(json) : json;
//Get Pointer
const JsonPointer = JSONPath({path: `$..${editJson.valueToModify}`, json, resultType: 'pointer'});
const firstJsonMatchedPointer = JsonPointer[0];
Expand Down
2 changes: 1 addition & 1 deletion src/rz/json/edit-json/edit-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as pointer from 'json-pointer';

export function editJson(editJson: EditJson, json: string): any {
const codeBlock = parseJsonOrString(editJson.codeBlock);
json = JSON.parse(json);
json = typeof json === 'string' ? JSON.parse(json) : json;
//2. Set value
pointer.set(json as any, editJson.valueToModify, codeBlock);

Expand Down

0 comments on commit 7595cb1

Please sign in to comment.