Skip to content

Commit

Permalink
ULMS-1442 Changed eslint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Dec 29, 2021
1 parent b1ac8eb commit a031057
Show file tree
Hide file tree
Showing 31 changed files with 20,293 additions and 4,750 deletions.
24 changes: 9 additions & 15 deletions .babelrc.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
{
"env":{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
},
"es": {
"presets": [
["@babel/preset-env", {
"modules": false,
"targets": "last 2 versions"
}]
[
"@babel/preset-env",
{
"modules": false,
"targets": "last 2 versions"
}
]
],
"plugins": [
"babel-plugin-annotate-pure-calls"
]
"plugins": ["babel-plugin-annotate-pure-calls"]
},
"lib": {
"presets": ["@babel/preset-env"]
},
"rollup": {
"presets": [
["@babel/preset-env", {
"modules": false
}]
]
}
}
}
11 changes: 9 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"extends": "standard",
"env": {
"jest": true
}
},
"extends": [
"airbnb-base",
"plugin:promise/recommended",
"plugin:sonarjs/recommended",
"plugin:unicorn/recommended",
"prettier"
],
"plugins": ["promise", "sonarjs", "unicorn"]
}
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: test

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.13.1]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run lint
test:
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.13.1]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test
13 changes: 0 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ node_modules/
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/

# IDE specific files
.idea/*

Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.13.1
11 changes: 11 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/es/
/lib/

# Node modules directory
/node_modules/

# Tests output
/coverage/

# Optional eslint cache
.eslintcache
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "semi": false, "singleQuote": true }
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# ulms-api-clients-js

JavaScript API clients for ULMS platform
123 changes: 77 additions & 46 deletions __tests__/basic-client.test.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,132 @@
import { BasicClient } from '../src/basic-client'
import BasicClient from '../src/basic-client'

const baseUrl = 'https://test.url'
const httpClient = {
get: jest.fn(),
put: jest.fn(),
post: jest.fn(),
patch: jest.fn(),
delete: jest.fn()
delete: jest.fn(),
}
const tokenProvider = {
getToken: jest.fn().mockImplementation(() => 'token')
getToken: jest.fn().mockImplementation(() => 'token'),
}

const headersWithoutLabels = { authorization: 'Bearer token', 'content-type': 'application/json' }
const headersWithoutLabels = {
authorization: 'Bearer token',
'content-type': 'application/json',
}

const Client = new BasicClient(baseUrl, httpClient, tokenProvider)

const labels = {
app_audience: 'audience',
app_label: 'label',
app_version: 'version',
scope: 'scope'
scope: 'scope',
}

const ulmsLabels = {
'ulms-app-audience': 'audience',
'ulms-app-label': 'label',
'ulms-app-version': 'version',
'ulms-scope': 'scope'
'ulms-scope': 'scope',
}

const urlGeneratorCases = [
['objectParams', { a: 0, b: 1, c: 2 }, '?a=0&b=1&c=2'],
['advancedObjectParams', { a: 'a', b: [1, 2, 3], c: { cc: 3, dd: 4 } }, '?a=a&b[]=1&b[]=2&b[]=3&c[cc]=3&c[dd]=4'],
[
'advancedObjectParams',
{ a: 'a', b: [1, 2, 3], c: { cc: 3, dd: 4 } },
'?a=a&b[]=1&b[]=2&b[]=3&c[cc]=3&c[dd]=4',
],
['objectParamsUndefined', { a: undefined }, ''],
['arrayParams', ['a', 'b', 'c'], '?0=a&1=b&2=c'],
['advancedArrayParams', { alphabet: ['a', 'b', 'c'] }, '?alphabet[]=a&alphabet[]=b&alphabet[]=c'],
['withoutParams', undefined, '']
[
'advancedArrayParams',
{ alphabet: ['a', 'b', 'c'] },
'?alphabet[]=a&alphabet[]=b&alphabet[]=c',
],
['withoutParams', undefined, ''],
]

const methodsWithoutDataCases = [
['get'],
['delete']
]
const methodsWithoutDataCases = [['get'], ['delete']]

const methodsWithDataCases = [
['post', 'data'],
['put', 'data'],
['patch', 'data']
['patch', 'data'],
]
describe('Basic client suite', () => {
it.each(urlGeneratorCases)('URL generator should work with: endpoint — %s and %j as params', (endpoint, params, expectedParams) => {
const resultUrl = Client._url(`/${endpoint}`, params)
expect(resultUrl).toBe(`${baseUrl}/${endpoint}${expectedParams}`)
})
it.each(urlGeneratorCases)(
'URL generator should work with: endpoint — %s and %j as params',
(endpoint, parameters, expectedParameters) => {
const resultUrl = Client.url(`/${endpoint}`, parameters)
expect(resultUrl).toBe(`${baseUrl}/${endpoint}${expectedParameters}`)
}
)

it('Headers should compare with labels', () => {
const resultHeaders = BasicClient._headers('token', { label: 'a' })
const resultHeaders = BasicClient.headers('token', { label: 'a' })
expect(resultHeaders.authorization).toBe('Bearer token')
expect(resultHeaders['content-type']).toBe('application/json')
expect(resultHeaders.label).toBe('a')
})

it('Headers should compare without labels', () => {
const resultHeaders = BasicClient._headers('token')
const resultHeaders = BasicClient.headers('token')
expect(resultHeaders).toEqual(headersWithoutLabels)
})

it('Labes should set and destroy', () => {
it('Labels should set and destroy', () => {
Client.setLabels(labels)
expect(Client._labels).toEqual(ulmsLabels)

Client.clearLabels()
expect(Client._labels).toEqual({})
})

it.each(methodsWithoutDataCases)('%s method should work (without labels)', async (method) => {
await Client['_' + method](baseUrl)
expect(httpClient[method]).toBeCalledWith(baseUrl, { headers: headersWithoutLabels })
})
expect(Client.labels).toEqual(ulmsLabels)

it.each(methodsWithoutDataCases)('%s method should work (with labels)', async (method) => {
Client.setLabels(labels)
await Client['_' + method](baseUrl)
Client.clearLabels()
expect(httpClient[method]).toBeCalledWith(baseUrl, { headers: { ...headersWithoutLabels, ...ulmsLabels } })
})

it.each(methodsWithDataCases)('%s method should work (without labels) with payload: %s', async (method, data) => {
await Client['_' + method](baseUrl, data)
expect(httpClient[method]).toBeCalledWith(baseUrl, data, { headers: headersWithoutLabels })
expect(Client.labels).toEqual({})
})

it.each(methodsWithDataCases)('%s method should work (with labels) with payload: %s', async (method, data) => {
Client.setLabels(labels)
await Client['_' + method](baseUrl, data)
Client.clearLabels()
expect(httpClient[method]).toBeCalledWith(baseUrl, data, { headers: { ...headersWithoutLabels, ...ulmsLabels } })
})
it.each(methodsWithoutDataCases)(
'%s method should work (without labels)',
async (method) => {
await Client[method](baseUrl)
expect(httpClient[method]).toBeCalledWith(baseUrl, {
headers: headersWithoutLabels,
})
}
)

it.each(methodsWithoutDataCases)(
'%s method should work (with labels)',
async (method) => {
Client.setLabels(labels)
await Client[method](baseUrl)
Client.clearLabels()
expect(httpClient[method]).toBeCalledWith(baseUrl, {
headers: { ...headersWithoutLabels, ...ulmsLabels },
})
}
)

it.each(methodsWithDataCases)(
'%s method should work (without labels) with payload: %s',
async (method, data) => {
await Client[method](baseUrl, data)
expect(httpClient[method]).toBeCalledWith(baseUrl, data, {
headers: headersWithoutLabels,
})
}
)

it.each(methodsWithDataCases)(
'%s method should work (with labels) with payload: %s',
async (method, data) => {
Client.setLabels(labels)
await Client[method](baseUrl, data)
Client.clearLabels()
expect(httpClient[method]).toBeCalledWith(baseUrl, data, {
headers: { ...headersWithoutLabels, ...ulmsLabels },
})
}
)
})
10 changes: 4 additions & 6 deletions __tests__/codec.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Codec } from '../src/codec'
import Codec from '../src/codec'

const encoder = (data) => JSON.stringify(data)
const decoder = (data) => {
let payload

try {
payload = JSON.parse(data.toString())
} catch (error) {
} catch {
payload = {}
}

Expand All @@ -18,11 +18,9 @@ const payload = {
data: {
a: 'a',
b: 'b',
c: 'c'
c: 'c',
},
another: [
'a', 'b', 'c'
]
another: ['a', 'b', 'c'],
}

describe('Codec works correctly', () => {
Expand Down
12 changes: 8 additions & 4 deletions __tests__/error.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// import { describe, it, expect} from 'jest'

import { MQTTClientError, MQTTRPCServiceError, TimeoutError } from '../src/error'
import {
MQTTClientError,
MQTTRPCServiceError,
TimeoutError,
} from '../src/error'

const clientError = new MQTTClientError('Message')
const serviceError = new MQTTRPCServiceError('Message')
Expand All @@ -14,7 +16,9 @@ describe('Custom errors have right titles', () => {
expect(clientError.message).toEqual('Message')
})
it('MQTTClientError message is valid (from error)', () => {
expect(MQTTClientError.fromError(new Error('Message')).message).toEqual('Message')
expect(MQTTClientError.fromError(new Error('Message')).message).toEqual(
'Message'
)
})
it('MQTTRPCServiceError name is valid', () => {
expect(serviceError.name).toEqual('MQTTRPCServiceError')
Expand Down
Loading

0 comments on commit a031057

Please sign in to comment.