Skip to content

Commit

Permalink
Added basic tests related to sheets-connection main methods and range…
Browse files Browse the repository at this point in the history
… logic tests, little fixes in README and sheets-connection.ts
  • Loading branch information
M1chalS committed Jul 12, 2023
1 parent 3dc5b99 commit eca6065
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
package-lock.json
build
.idea
coverage
coverage
.env
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
"testPathIgnorePatterns" : [
"./build",
]
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions src/config/configurations.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,7 +33,7 @@ export interface ClearRequestConfiguration {
}

export interface Configuration {
auth: GoogleSheetsAuth;
auth: any;
spreadsheetId: string;
sheet?: string;
range?: string;
Expand Down
226 changes: 223 additions & 3 deletions src/sheets/__tests__/sheets-connection.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
10 changes: 6 additions & 4 deletions src/sheets/sheets-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
};

Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit eca6065

Please sign in to comment.