diff --git a/plugins/jest.config.js b/plugins/jest.config.js index 8cbf8940cc..61bcc4a0d1 100644 --- a/plugins/jest.config.js +++ b/plugins/jest.config.js @@ -2,4 +2,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + rootDir: '.', }; \ No newline at end of file diff --git a/plugins/package-lock.json b/plugins/package-lock.json index b0da9b41f4..fa2490660e 100644 --- a/plugins/package-lock.json +++ b/plugins/package-lock.json @@ -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": { diff --git a/plugins/package.json b/plugins/package.json index a1be9c5ec6..ad6e090adf 100644 --- a/plugins/package.json +++ b/plugins/package.json @@ -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", diff --git a/plugins/packages/googlesheets/__tests__/googlesheets.test.js b/plugins/packages/googlesheets/__tests__/googlesheets.test.js index 5ea0a5ca74..89de2a501a 100644 --- a/plugins/packages/googlesheets/__tests__/googlesheets.test.js +++ b/plugins/packages/googlesheets/__tests__/googlesheets.test.js @@ -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' }]); + }); }); diff --git a/plugins/packages/googlesheets/lib/operations.ts b/plugins/packages/googlesheets/lib/operations.ts index f8e9442efa..1ccff84d91 100644 --- a/plugins/packages/googlesheets/lib/operations.ts +++ b/plugins/packages/googlesheets/lib/operations.ts @@ -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 -} \ No newline at end of file +} + +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 +}; \ No newline at end of file diff --git a/plugins/tsconfig.json b/plugins/tsconfig.json index 06a361f85d..a88c8575b9 100644 --- a/plugins/tsconfig.json +++ b/plugins/tsconfig.json @@ -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", "/__tests__/*", "dist"] }