Skip to content

Commit

Permalink
added jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikMatiasko committed Apr 22, 2024
1 parent 095aa46 commit 0d34385
Show file tree
Hide file tree
Showing 51 changed files with 1,390 additions and 67 deletions.
2 changes: 1 addition & 1 deletion config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
modulePaths: ['<rootDir>'],
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
resetMocks: true,
setupFiles: ['react-app-polyfill/jsdom'],
setupFiles: ['react-app-polyfill/jsdom', '<rootDir>/config/setup-tests.js'],
setupFilesAfterEnv: ['<rootDir>/config/jest.setup.ts'],
snapshotSerializers: ['@emotion/jest/serializer'],
testMatch: ['<rootDir>/src/**/*.test.{js,jsx,ts,tsx}'],
Expand Down
3 changes: 3 additions & 0 deletions config/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import '@testing-library/jest-dom'
import { matchers } from '@emotion/jest'
import ReactDOM from 'react-dom'

import 'regenerator-runtime/runtime'
import { cleanup } from '@testing-library/react'
expect.extend(matchers)

const ResizeObserverOrigin = window.ResizeObserver
Expand Down Expand Up @@ -30,4 +32,5 @@ beforeEach(() => {
afterEach(() => {
window.ResizeObserver = ResizeObserverOrigin
jest.resetAllMocks()
cleanup()
})
3 changes: 3 additions & 0 deletions config/setup-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { configure } from '@testing-library/react'

configure({ testIdAttribute: 'data-test-id' })
29 changes: 29 additions & 0 deletions src/common/context/FormContext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getFormContextDefault } from './FormContext'
import { inputAligns, inputSizes } from '../../components/Atomic/FormInput'

describe('getFormContextDefault', () => {
it('returns default form context with provided i18nDefault', () => {
const i18nDefault = 'test'
const context = getFormContextDefault(i18nDefault)

expect(context.commonTimeoutControlProps.i18n.default).toBe(i18nDefault)
expect(context.updateData).toBeInstanceOf(Function)
expect(context.commonFormGroupProps).toEqual({
errorTooltip: true,
fullSize: true,
marginBottom: false,
})
expect(context.commonInputProps).toEqual({
align: inputAligns.RIGHT,
inlineStyle: true,
size: inputSizes.SMALL,
})
expect(context.compactFormComponentsView).toBe(false)
})

it('returns default form context with empty i18nDefault when no argument is provided', () => {
const context = getFormContextDefault()

expect(context.commonTimeoutControlProps.i18n.default).toBe('')
})
})
1 change: 0 additions & 1 deletion src/common/shapes/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/common/shapes/prop-type-ref.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const copyToClipboard = (text, certFormat = false) => {
export const copyToClipboard = (text: string, certFormat = false) => {
if (document.execCommand) {
const textField = document.createElement('textarea')
textField.innerText = certFormat ? JSON.stringify(text.replace(/<br>/g, '\n')) : text
Expand Down
6 changes: 2 additions & 4 deletions src/common/utils/devices.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// @ts-ignore
import * as converter from 'units-converter/dist/es/index'
import { DEVICES_RESOURCE_UPDATE_WS_KEY, knownInterfaces, commandTimeoutUnits, MINIMAL_TTL_VALUE_MS } from '../constants'
import { compareIgnoreCase } from '../../components/Atomic/Table/Utils'

const time = converter.time
import time from './time'

const { INFINITE, NS, MS, S, M, H } = commandTimeoutUnits

Expand Down Expand Up @@ -85,6 +82,7 @@ export const createNestedResourceData = (data: any) => {
return compareIgnoreCase(a.href, b.href)
})
}

const deDensisfy = (objectToDeDensify: any) => {
const { href, ...rest } = objectToDeDensify

Expand Down
2 changes: 0 additions & 2 deletions src/common/utils/get-api-error-message.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/common/utils/get-api-error-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type ErrorType = {
response?: {
data?: {
err?: string
message?: string
}
}
message?: string
}

// Return a message from the error response of an API
export const getApiErrorMessage = (error: ErrorType | string): string | undefined =>
typeof error === 'string' ? error : error?.response?.data?.err || error?.response?.data?.message || error?.message || undefined
File renamed without changes.
1 change: 0 additions & 1 deletion src/common/utils/get-value.js

This file was deleted.

1 change: 1 addition & 0 deletions src/common/utils/get-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getValue = (value: any, emptyPlaceholder = '-') => (value !== undefined && value !== null ? value : emptyPlaceholder)
10 changes: 5 additions & 5 deletions src/common/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from './get-value'
export * from './cert-decoder.mjs'
export * from './copy-to-clipboard'
export * from './devices'
export * from './get-api-error-message'
export * from './get-app-mode'
export * from './get-value'
export * from './is-browser-tab-active'
// export * from './play-random-fart-sound'
export * from './parse-streamed-data'
export * from './copy-to-clipboard'
export * from './is-valid-guid'
export * from './devices'
export * from './parse-streamed-data'
export * from './time'
File renamed without changes.
1 change: 0 additions & 1 deletion src/common/utils/is-valid-guid.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/common/utils/is-valid-guid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isValidGuid = (guid: string) =>
/^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$/i.test(guid) &&
((guid.charAt(0) === '{' && guid.charAt(guid.length - 1) === '}') || guid.charAt(0) !== '{')
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Return an array of objects which came in as a text stream of results
export const parseStreamedData = (stream) => {
export const parseStreamedData = (stream: string) => {
try {
return stream
? stream
Expand All @@ -14,4 +14,4 @@ export const parseStreamedData = (stream) => {
}

// Convert Uint8Array to text
export const binArrayToJson = (binArray) => new TextDecoder().decode(binArray)
export const binArrayToJson = (binArray: any) => new TextDecoder().decode(binArray)
20 changes: 0 additions & 20 deletions src/common/utils/play-random-fart-sound.js

This file was deleted.

136 changes: 136 additions & 0 deletions src/common/utils/time-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// @ts-nocheck
const Converter = function (numerator, definitions) {
this.definitions = definitions
this.val = numerator
}

Converter.prototype.from = function (from) {
if (this.destination) {
throw new Error('.from must be called before .to')
}

this.origin = this.getUnit(from)

if (!this.origin) {
this.throwUnsupportedUnitError(from)
}

return this
}

Converter.prototype.to = function (to) {
if (!this.origin) {
throw new Error('.to must be called after .from')
}

this.destination = this.getUnit(to)

let result

if (!this.destination) {
this.throwUnsupportedUnitError(to)
}

if (this.origin.abbr === this.destination.abbr) {
return Object.assign({ value: this.val }, this.describe(this.destination.abbr))
}

result = this.val * this.origin.unit.to_anchor

if (this.origin.unit.anchor_shift) {
result -= this.origin.unit.anchor_shift
}

if (this.origin.system !== this.destination.system) {
result = this.definitions[this.origin.system].transform(result)
}

if (this.destination.unit.anchor_shift !== undefined) {
result += this.destination.unit.anchor_shift
}

return Object.assign({ value: result / this.destination.unit.to_anchor }, this.describe(this.destination.abbr))
}

Converter.prototype.toBest = function (options) {
if (!this.origin) {
throw new Error('.toBest must be called after .from')
}

options = Object.assign(
{
exclude: [],
cutOffNumber: 1,
},
options
)

return this.list()
.filter((item) => !options.exclude.includes(item.unit) && this.describe(item.unit).system === this.origin.system)
.reduce((acc, item) => {
const result = this.to(item.unit)
if (!acc || (result.value >= options.cutOffNumber && result.value < acc.value)) {
return result
} else {
return acc
}
}, undefined)
}

Converter.prototype.getUnit = function (abbr) {
const systemNames = Object.keys(this.definitions)
const found = systemNames
.map((systemName) => {
if (this.definitions[systemName][abbr]) {
return {
abbr: abbr,
system: systemName,
unit: this.definitions[systemName][abbr],
}
} else {
return undefined
}
})
.filter((item) => item !== undefined)

return Array.isArray(found) ? found[0] : undefined
}

Converter.prototype.list = function () {
return this.possibilities().map((abbr) => this.describe(abbr))
}

Converter.prototype.throwUnsupportedUnitError = function (what) {
throw new Error('Unsupported unit ' + what)
}

Converter.prototype.describe = function (abbr) {
if (!abbr) {
throw new Error('You must select a unit')
}

const unit = this.getUnit(abbr)

return {
unit: unit.abbr,
system: unit.system,
singular: unit.unit.name.singular,
plural: unit.unit.name.plural,
}
}

Converter.prototype.possibilities = function () {
return Array.prototype.concat(
...Object.keys(this.definitions).map((systemName) => {
return Object.keys(this.definitions[systemName]).splice(2)
})
)
}

function converter(definitions) {
return (val) => {
return new Converter(val, definitions)
}
}

export default converter
88 changes: 88 additions & 0 deletions src/common/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,92 @@
import utils from './time-utils'

export function getMinutesBetweenDates(startDate: Date, endDate: Date) {
const diff = endDate.getTime() - startDate.getTime()
return diff / 60000
}

const RATIO = 1
const daysInYear = 365.25

const time = {
metric: {
baseUnit: 's',
transform: (val: any) => {
return val * RATIO
},
ns: {
name: {
singular: 'Nanosecond',
plural: 'Nanoseconds',
},
to_anchor: 1 / 1000000000,
},
mu: {
name: {
singular: 'Microsecond',
plural: 'Microseconds',
},
to_anchor: 1 / 1000000,
},
ms: {
name: {
singular: 'Millisecond',
plural: 'Milliseconds',
},
to_anchor: 1 / 1000,
},
s: {
name: {
singular: 'Second',
plural: 'Seconds',
},
to_anchor: 1,
},
min: {
name: {
singular: 'Minute',
plural: 'Minutes',
},
to_anchor: 60,
},
h: {
name: {
singular: 'Hour',
plural: 'Hours',
},
to_anchor: 60 * 60,
},
d: {
name: {
singular: 'Day',
plural: 'Days',
},
to_anchor: 60 * 60 * 24,
},
week: {
name: {
singular: 'Week',
plural: 'Weeks',
},
to_anchor: 60 * 60 * 24 * 7,
},
month: {
name: {
singular: 'Month',
plural: 'Months',
},
to_anchor: (60 * 60 * 24 * daysInYear) / 12,
},
year: {
name: {
singular: 'Year',
plural: 'Years',
},
to_anchor: 60 * 60 * 24 * daysInYear,
},
},
}

const time$1 = utils(time)

export default time$1
Loading

0 comments on commit 0d34385

Please sign in to comment.