diff --git a/.gitignore b/.gitignore index c94ba35..7c4b882 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules package-lock.json build .idea -coverage \ No newline at end of file +coverage +.env \ No newline at end of file diff --git a/README.md b/README.md index 38edf88..d905678 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,12 @@ npm install npm run build ``` +### Run tests + +```shell +npm run test +``` + Compiled JavaScript will be placed in `/build` folder. _Made by MichaƂ Szajner_ \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index b413e10..3f0dfa1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,4 +2,7 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + "testPathIgnorePatterns" : [ + "./build", + ] }; \ No newline at end of file diff --git a/package.json b/package.json index 0b2d3cd..79c3d58 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "googleapis": "^120.0.0" }, "devDependencies": { + "dotenv": "^16.3.1", "del-cli": "^5.0.0", "ts-jest": "^29.1.1", "typescript": "^5.1.6" diff --git a/src/config/configurations.ts b/src/config/configurations.ts index bf67b67..45001c5 100644 --- a/src/config/configurations.ts +++ b/src/config/configurations.ts @@ -1,5 +1,4 @@ import {DateTimeRenderOption, Dimension, InsertDataOption, ValueInputOption, ValueRenderOption} from "../types/types"; -import {GoogleSheetsAuth} from "../auth/google-sheets-auth"; export interface GetRequestConfiguration { range?: string; @@ -34,7 +33,7 @@ export interface ClearRequestConfiguration { } export interface Configuration { - auth: GoogleSheetsAuth; + auth: any; spreadsheetId: string; sheet?: string; range?: string; diff --git a/src/sheets/__tests__/sheets-connection.test.ts b/src/sheets/__tests__/sheets-connection.test.ts index 0999de4..3d83826 100644 --- a/src/sheets/__tests__/sheets-connection.test.ts +++ b/src/sheets/__tests__/sheets-connection.test.ts @@ -1,5 +1,225 @@ -import { test, expect } from '@jest/globals'; +import {describe, expect, test} from '@jest/globals'; +import {GoogleSheetsAuth} from "../../auth/google-sheets-auth"; +import {SheetsConnection} from "../sheets-connection"; +import dotenv from 'dotenv'; + +dotenv.config(); + +// Auth setup +const key = process.env.GOOGLE_KEY!.toString().split('\\n').map(i => i + "\n").join(""); + +const googleAuthWrapper = new GoogleSheetsAuth({ + email: process.env.GOOGLE_EMAIL!, + key: key +}).login(); + +describe('SheetsConnection main methods', () => { + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet: "Sheet1", + range: "A1:B" + }); + + test('Expect get method to work', async () => { + expect(await sheetsConnection.get()).toBeTruthy(); + }); + + test('Expect append method to work', async () => { + expect(await sheetsConnection.append([[ + "test1", + "test2" + ]])).toBeTruthy(); + }); + + test('Expect update method to work', async () => { + expect(await sheetsConnection.update([[ + "test3", + "test4" + ]])).toBeTruthy(); + }); + + test('Expect clear method to work', async () => { + expect(await sheetsConnection.clear()).toBeTruthy(); + }); + +}); + +describe('Range logic checks', () => { + + test('Expect range to be this.sheetRange when sheet and range in constructor', async () => { + const range = "A1:B2"; + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet, + range + }); + + const response = await sheetsConnection.get(); + + expect(response.data.range).toBe(sheet + "!" + range); + }); + + test('Expect range to be `${cfg.sheet}!${cfg.range}` when sheet and range in method', async () => { + const range = "A1:B2"; + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + }); + + const response = await sheetsConnection.get({ + sheet, + range + }); + + expect(response.data.range).toBe(sheet + "!" + range); + }); + + test('Expect range to be `${cfg.sheet}!${cfg.range}` when sheet and range in method and constructor', async () => { + const range = "A1:B2"; + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet: "Sheet2", + range: "C1:D2" + }); + + const response = await sheetsConnection.get({ + sheet, + range + }); + + expect(response.data.range).toBe(sheet + "!" + range); + }); + + test('Expect range to be `${this.sheet}!${cfg.range}` when sheet and range in constructor and range in method', async () => { + const range = "A1:B2"; + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet, + range: "C1:D2" + }); + + const response = await sheetsConnection.get({ + range + }); + + expect(response.data.range).toBe(sheet + "!" + range); + }); + + test('Expect range to be `${this.sheet}!${cfg.range}` when sheet in constructor and range in method', async () => { + const range = "A1:B2"; + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet, + }); + + const response = await sheetsConnection.get({ + range + }); + + expect(response.data.range).toBe(sheet + "!" + range); + }); + + test('Expect error when sheet only in constructor', async () => { + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet, + }); + + await expect(sheetsConnection.get()).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when range only in constructor', async () => { + const range = "A1:B2"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + range, + }); + + await expect(sheetsConnection.get()).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when sheet only in method', async () => { + const sheet = "Sheet1"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + }); + + await expect(sheetsConnection.get({sheet})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when range only in method', async () => { + const range = "A1:B2"; + + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + }); + + await expect(sheetsConnection.get({range})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when sheet and range in constructor and sheet in method', async () => { + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet: "Sheet1", + range: "A1:B2" + }); + + await expect(sheetsConnection.get({sheet: "Sheet2"})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when range in constructor and sheet in method', async () => { + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + range: "A1:B2" + }); + + await expect(sheetsConnection.get({sheet: "Sheet2"})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when range in constructor and range in method', async () => { + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + range: "A1:B2" + }); + + await expect(sheetsConnection.get({range: "C1:D2"})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); + + test('Expect error when sheet in constructor and sheet in method', async () => { + const sheetsConnection = new SheetsConnection({ + spreadsheetId: process.env.SHEET_ID!, + auth: googleAuthWrapper, + sheet: "Sheet1" + }); + + await expect(sheetsConnection.get({sheet: "Sheet2"})).rejects.toThrowError("Specify range or sheet in method or in constructor"); + }); -test('adds 1 + 2 to equal 3', () => { - expect(1+2).toBe(3); }); \ No newline at end of file diff --git a/src/sheets/sheets-connection.ts b/src/sheets/sheets-connection.ts index c9cce41..e0ce156 100644 --- a/src/sheets/sheets-connection.ts +++ b/src/sheets/sheets-connection.ts @@ -13,7 +13,7 @@ export class SheetsConnection { private sheets: sheets_v4.Sheets = google.sheets("v4"); public readonly sheetRange?: string; public readonly startingSheetIndex?: number; - private readonly authWrapper: GoogleSheetsAuth; + private readonly authWrapper: any; private readonly spreadsheetId: string; private readonly sheet?: string; private readonly range?: string; @@ -57,11 +57,11 @@ export class SheetsConnection { return await this.sheets.spreadsheets.values.append(this.appendRequestPayload(data, {...cfg})); }; - public update = async (data: any[], cfg: UpdateRequestConfiguration) => { + public update = async (data: any[], cfg?: UpdateRequestConfiguration) => { return await this.sheets.spreadsheets.values.update(this.updateRequestPayload(data, cfg)); }; - public clear = async (cfg: ClearRequestConfiguration) => { + public clear = async (cfg?: ClearRequestConfiguration) => { return await this.sheets.spreadsheets.values.clear(this.clearRequestPayload(cfg)); }; @@ -128,7 +128,9 @@ export class SheetsConnection { (!this.sheetRange && cfg?.sheet && !cfg?.range) || (this.sheet && !this.range && !cfg?.sheet && !cfg?.range) || (this.sheet && !this.range && cfg?.sheet && !cfg?.range) || - (this.sheetRange && cfg?.sheet && !cfg?.range) + (this.sheetRange && cfg?.sheet && !cfg?.range) || + (!this.sheet && !this.range && !cfg?.sheet && cfg?.range) || + (!this.sheet && this.range && !cfg?.sheet && cfg?.range) ) { throw new Error("Specify range or sheet in method or in constructor"); }