Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add test login in project #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
{
"env": {
"browser": true,
"es2021": true
"es2021": true,
"jest/globals": true
},
"extends": "standard-with-typescript",
"parser": "@typescript-eslint/parser",
"extends": [
"standard-with-typescript",
"plugin:jest/recommended"
],
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": "tsconfig.json"
},
"plugins": ["jest"],
"rules": {
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error"
}
}
15 changes: 15 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ jobs:
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

release:
needs: [ build ]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: "✏️ Generate release changelog"
uses: heinrichreimer/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Release
uses: softprops/action-gh-release@v1
with:
body_path: CHANGELOG.md
4 changes: 3 additions & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ jobs:
- name: Install dependencies
run: npm install
- name: Run eslint
run: npm run lint
run: npm run lint
- name: Run tests
run: npm run test
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botcity/botcity-maestro-sdk",
"version": "0.2.2",
"version": "0.2.3",
"description": "Botcity Maestro SDK",
"author": "botcity",
"main": "dist/index.js",
Expand All @@ -9,23 +9,14 @@
"clean": "rimraf dist",
"prebuild": "npm run clean",
"build": "tsc",
"release": "release-it",
"lint": "eslint src/**/*.{js,ts,json}",
"lint:fix": "eslint --fix src/**/*.{js,ts,json}",
"lint": "eslint src/**/*.{js,ts,json} tests/**/*.{js,ts,json}",
"lint:fix": "eslint --fix src/**/*.{js,ts,json} --fix tests/**/*.{js,ts,json}",
"format": "prettier --write src/**/*.{js,ts,md,json} --config ./.prettierrc",
"commit": "git-cz",
"prepare": "husky install"
},
"release-it": {
"npm": {
"publish": false
},
"github": {
"release": true
},
"git": {
"commitMessage": "chore: release ${version}"
}
"prepare": "husky install",
"test": "jest",
"test:watch": "jest --detectOpenHandles --watchAll",
"test:coverage": "jest --detectOpenHandles --coverage"
},
"contributors": [
{
Expand All @@ -47,20 +38,24 @@
"@types/node": "^14.18.26",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "5.36.2",
"axios-mock-adapter": "^1.21.2",
"commitizen": "4.2.5",
"cz-conventional-changelog": "3.3.0",
"eslint": "^8.23.0",
"eslint-config-prettier": "8.5.0",
"eslint-config-standard-with-typescript": "^22.0.0",
"eslint-import-resolver-typescript": "3.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.0.4",
"eslint-plugin-n": "^15.2.5",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-promise": "^6.0.1",
"husky": "8.0.1",
"jest": "^29.0.3",
"jest-mock-axios": "^4.7.0-beta",
"prettier": "2.7.1",
"release-it": "^15.4.1",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.1",
"typescript": "^4.8.2"
},
"dependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/botcity/maestro/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ export class BotMaestroSdk {
const data = { login: this._login, key: this._key }
const response: AxiosResponse = await axios.post(url, data)
this.accessToken = response.data.accessToken
} catch (error) {
console.error(error)
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
throw Error('Login failed to server')
}
throw error
}
}
Expand Down
96 changes: 0 additions & 96 deletions tests/index.ts

This file was deleted.

76 changes: 76 additions & 0 deletions tests/unit/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable jest/valid-expect */
import { describe, it, beforeAll, afterEach, expect } from '@jest/globals'
import MockAdapter from 'axios-mock-adapter'
import axios from 'axios'
import { BotMaestroSdk } from '../../src'

describe('Login module', () => {
let mock: MockAdapter
let server: string = 'https://testing.dev'
let login: string = 'testing'
let key: string = 'testing'
const endpointLogin: string = '/api/v2/workspace/login'

beforeAll(() => {
mock = new MockAdapter(axios)
})

afterEach(() => {
mock.reset()
server = 'https://testing.dev'
login = 'testing'
key = 'testing'
})

it('Login successfully executed.', async () => {
const response = {
accessToken: 'testing'
}

const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key)

mock.onPost(`${server}${endpointLogin}`).reply(200, response)

await maestro.login()

expect(maestro.accessToken).toBe('testing')
})

it('Login executed with "Server is required." error', async () => {
server = ''

const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key)

mock.onPost(`${server}${endpointLogin}`).reply(400, {})

expect(async () => await maestro.login()).rejects.toThrow('Server is required.')
})

it('Login executed with "Login is required." error', async () => {
login = ''

const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key)

mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {})

expect(async () => await maestro.login()).rejects.toThrow('Login is required.')
})

it('Login executed with "Key is required" error', async () => {
key = ''
const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key)

mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {})

expect(async () => await maestro.login()).rejects.toThrow('Key is required.')
})

it('Login executed with error generic', async () => {
const maestro: BotMaestroSdk = new BotMaestroSdk(server, login, key)

mock.onPost(`${server}/api/v2/workspace/login`).reply(400, {})

expect(async () => await maestro.login()).rejects.toThrow('Login failed to server')
})
})
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"esModuleInterop": true,
"experimentalDecorators": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.test.ts"]
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules"]
}