mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
[Unit test] Plugins - Googlesheet update operation (#2450)
* refactored updateoperation * adds unit test for generating the request body for update operation * fixes array methods for looping over the array
This commit is contained in:
parent
7775ef53cb
commit
030cc727b1
6 changed files with 89 additions and 40 deletions
|
|
@ -2,4 +2,5 @@
|
|||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
rootDir: '.',
|
||||
};
|
||||
21
plugins/package-lock.json
generated
21
plugins/package-lock.json
generated
|
|
@ -35,6 +35,7 @@
|
|||
"@tooljet-plugins/typesense": "file:packages/typesense"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^27.4.1",
|
||||
"jest": "^27.4.5",
|
||||
"lerna": "^4.0.0",
|
||||
"rimraf": "^3.0.2",
|
||||
|
|
@ -4575,6 +4576,16 @@
|
|||
"@types/istanbul-lib-report": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/jest": {
|
||||
"version": "27.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.4.1.tgz",
|
||||
"integrity": "sha512-23iPJADSmicDVrWk+HT58LMJtzLAnB2AgIzplQuq/bSrGaxCrlvRFjGbXmamnnk/mAmCdLStiGqggu28ocUyiw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"jest-matcher-utils": "^27.0.0",
|
||||
"pretty-format": "^27.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/keyv": {
|
||||
"version": "3.1.3",
|
||||
"license": "MIT",
|
||||
|
|
@ -19312,6 +19323,16 @@
|
|||
"@types/istanbul-lib-report": "*"
|
||||
}
|
||||
},
|
||||
"@types/jest": {
|
||||
"version": "27.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.4.1.tgz",
|
||||
"integrity": "sha512-23iPJADSmicDVrWk+HT58LMJtzLAnB2AgIzplQuq/bSrGaxCrlvRFjGbXmamnnk/mAmCdLStiGqggu28ocUyiw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"jest-matcher-utils": "^27.0.0",
|
||||
"pretty-format": "^27.0.0"
|
||||
}
|
||||
},
|
||||
"@types/keyv": {
|
||||
"version": "3.1.3",
|
||||
"requires": {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@
|
|||
"build": "npm run create:client && npm run create:server && npm run build:packages && npm run build:server",
|
||||
"prebuild:packages": "npm run clean:packages",
|
||||
"build:packages": "npx lerna run build --stream",
|
||||
"clean:packages": "npx lerna run clean --parallel"
|
||||
"clean:packages": "npx lerna run clean --parallel",
|
||||
"test": "NODE_ENV=test jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tooljet-plugins/airtable": "file:packages/airtable",
|
||||
|
|
@ -42,6 +43,7 @@
|
|||
"@tooljet-plugins/typesense": "file:packages/typesense"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^27.4.1",
|
||||
"jest": "^27.4.5",
|
||||
"lerna": "^4.0.0",
|
||||
"rimraf": "^3.0.2",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,30 @@
|
|||
'use strict';
|
||||
|
||||
const googlesheets = require('../lib');
|
||||
const { makeRequestBodyToBatchUpdate } =require('../lib/operations');
|
||||
|
||||
describe('googlesheets', () => {
|
||||
it.todo('needs tests');
|
||||
it('should generate the request body for update operation' ,() => {
|
||||
const requestBody = {
|
||||
caseOne: { Gender: 'Female' },
|
||||
caseTwo: { extra: '0 points' },
|
||||
caseThree: { Gender: 'Female', extra: '0 points' }
|
||||
}
|
||||
const filterCondition = { key: 'Student Name', value: 'Anna' }
|
||||
const filterOperator = '==='
|
||||
const data = [
|
||||
[ 'ID', '1', '2' ],[ 'Student Name', 'John', 'Anna' ],[ 'Major', 'Science', 'English' ],[],[ 'Gender', 'Male', 'Female' ],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[ 'extra', 'extra-update', '0 points' ]
|
||||
]
|
||||
|
||||
const queryOptionsOne = { requestBody: requestBody.caseOne, filterCondition, filterOperator, data }
|
||||
const queryOptionsTwo = { requestBody: requestBody.caseTwo, filterCondition, filterOperator, data }
|
||||
const queryOptionsThree = { requestBody: requestBody.caseThree, filterCondition, filterOperator, data }
|
||||
|
||||
const expectedBodyForCaseOne = makeRequestBodyToBatchUpdate(queryOptionsOne.requestBody, queryOptionsOne.filterCondition, queryOptionsOne.filterOperator, queryOptionsOne.data)
|
||||
const expectedBodyForCaseTwo = makeRequestBodyToBatchUpdate(queryOptionsTwo.requestBody, queryOptionsOne.filterCondition, queryOptionsOne.filterOperator, queryOptionsOne.data)
|
||||
const expectedBodyForCaseThree = makeRequestBodyToBatchUpdate(queryOptionsThree.requestBody, queryOptionsOne.filterCondition, queryOptionsOne.filterOperator, queryOptionsOne.data)
|
||||
|
||||
|
||||
expect(expectedBodyForCaseOne).toEqual([{ cellValue: 'Female', cellIndex: 'E3' }]);
|
||||
expect(expectedBodyForCaseTwo).toEqual([{ cellValue: '0 points', cellIndex: 'AB3' }]);
|
||||
expect(expectedBodyForCaseThree).toEqual([{ cellValue: 'Female', cellIndex: 'E3' }, { cellValue: '0 points', cellIndex: 'AB3' }]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -47,43 +47,20 @@ export async function batchUpdateToSheet(
|
|||
}
|
||||
|
||||
const lookUpData = await lookUpSheetData(spreadSheetId,spreadsheetRange, sheet, authHeader);
|
||||
const body = await makeRequestBodyToBatchUpdate(requestBody, filterData, filterOperator, lookUpData);
|
||||
|
||||
const updateBody = (requestBody, filterCondition, filterOperator, data) => {
|
||||
const rowsIndexes = getRowsIndex(filterCondition, filterOperator, data) as any[];
|
||||
const colIndexes = getInputKeys(requestBody, data);
|
||||
|
||||
const updateCellIndexes = [];
|
||||
colIndexes.map((col) => {
|
||||
rowsIndexes.map((rowIndex) =>
|
||||
updateCellIndexes.push({
|
||||
...col,
|
||||
cellIndex: `${col.colIndex}${rowIndex}`,
|
||||
})
|
||||
);
|
||||
});
|
||||
const _data = body.map((data) => {
|
||||
return {
|
||||
majorDimension: 'ROWS',
|
||||
range: `${sheet}!${data.cellIndex}`,
|
||||
values: [[data.cellValue]],
|
||||
};
|
||||
});
|
||||
|
||||
const body = [];
|
||||
Object.entries(requestBody).map((item) => {
|
||||
updateCellIndexes.map((cell) => {
|
||||
if (item[0] === cell.col) {
|
||||
body.push({ cellValue: item[1], cellIndex: cell.cellIndex });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const _data = body.map((data) => {
|
||||
return {
|
||||
majorDimension: 'ROWS',
|
||||
range: `${sheet}!${data.cellIndex}`,
|
||||
values: [[data.cellValue]],
|
||||
};
|
||||
});
|
||||
|
||||
return _data;
|
||||
};
|
||||
|
||||
const reqBody = {
|
||||
data: updateBody(requestBody, filterData, filterOperator, lookUpData),
|
||||
data: _data,
|
||||
valueInputOption: 'USER_ENTERED',
|
||||
includeValuesInResponse: true,
|
||||
};
|
||||
|
|
@ -197,7 +174,7 @@ const getInputKeys = (inputBody, data) => {
|
|||
const keys = Object.keys(inputBody);
|
||||
const arr = [];
|
||||
keys.map((key) =>
|
||||
data.filter((val, index) => {
|
||||
data.forEach((val, index) => {
|
||||
if (val[0] === key) {
|
||||
let keyIndex = '';
|
||||
if(index >= 26) {
|
||||
|
|
@ -250,4 +227,30 @@ function numberToLetters(num) {
|
|||
num = Math.floor(num / 26) - 1
|
||||
}
|
||||
return letters
|
||||
}
|
||||
}
|
||||
|
||||
export const makeRequestBodyToBatchUpdate = (requestBody, filterCondition, filterOperator, data) => {
|
||||
const rowsIndexes = getRowsIndex(filterCondition, filterOperator, data) as any[];
|
||||
const colIndexes = getInputKeys(requestBody, data);
|
||||
|
||||
const updateCellIndexes = [];
|
||||
colIndexes.map((col) => {
|
||||
rowsIndexes.map((rowIndex) =>
|
||||
updateCellIndexes.push({
|
||||
...col,
|
||||
cellIndex: `${col.colIndex}${rowIndex}`,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
const body = [];
|
||||
Object.entries(requestBody).map((item) => {
|
||||
updateCellIndexes.map((cell) => {
|
||||
if (item[0] === cell.col) {
|
||||
body.push({ cellValue: item[1], cellIndex: cell.cellIndex });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return body
|
||||
};
|
||||
|
|
@ -7,7 +7,6 @@
|
|||
"sourceMap": true,
|
||||
|
||||
"strict": false,
|
||||
|
||||
"skipLibCheck": true,
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
|
|
@ -18,5 +17,5 @@
|
|||
"composite": true,
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"exclude": ["packages/*/lib/*.json", "packages/*/__tests__/*", "dist"]
|
||||
"exclude": ["packages/*/lib/*.json", "<rootDir>/__tests__/*", "dist"]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue