test: added remaining utils tests

This commit is contained in:
Roberto Tonino 2021-06-02 17:51:56 +02:00
parent 21b7629bdc
commit 15e279a2a5
4 changed files with 78 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,6 +2,7 @@
* Climbs the DOM until the root is reached, storing every node passed. * Climbs the DOM until the root is reached, storing every node passed.
* @param {HTMLElement} el * @param {HTMLElement} el
* @return {Array} Contains all the nodes between el and the root * @return {Array} Contains all the nodes between el and the root
* @since 0.0.0
*/ */
export function generatePath(el) { export function generatePath(el) {
if (!el) { if (!el) {
@ -17,6 +18,11 @@ export function generatePath(el) {
return path return path
} }
/**
* @param {string} text
* @returns {boolean}
* @since 0.0.0
*/
export function isValidURL(text) { export function isValidURL(text) {
const lowerCaseText = text.toLowerCase() const lowerCaseText = text.toLowerCase()
@ -35,6 +41,11 @@ export function isValidURL(text) {
return false return false
} }
/**
* @param {number} duration
* @returns {string}
* @since 0.0.0
*/
export function convertDuration(duration) { export function convertDuration(duration) {
// Convert from seconds only to mm:ss format // Convert from seconds only to mm:ss format
let mm, ss let mm, ss
@ -47,6 +58,11 @@ export function convertDuration(duration) {
return mm + ':' + ss return mm + ':' + ss
} }
/**
* @param {number} duration
* @returns {[number, number, number]}
* @since 0.0.0
*/
export function convertDurationSeparated(duration) { export function convertDurationSeparated(duration) {
let hh, mm, ss let hh, mm, ss
mm = Math.floor(duration / 60) mm = Math.floor(duration / 60)
@ -56,9 +72,13 @@ export function convertDurationSeparated(duration) {
return [hh, mm, ss] return [hh, mm, ss]
} }
export function numberWithDots(x) { /**
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.') * @param {number} x
} * @returns {string}
* @since 0.0.0
* @deprecated
*/
export const numberWithDots = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
// On scroll event, returns currentTarget = null // On scroll event, returns currentTarget = null
// Probably on other events too // Probably on other events too
@ -83,6 +103,8 @@ export function debounce(func, wait, immediate) {
* ghost input and copying the passed String * ghost input and copying the passed String
* *
* @param {string} text Text to copy * @param {string} text Text to copy
* @returns void
* @since 0.0.0
*/ */
export function copyToClipboard(text) { export function copyToClipboard(text) {
const ghostInput = document.createElement('input') const ghostInput = document.createElement('input')
@ -100,6 +122,7 @@ export function copyToClipboard(text) {
* @param {object|array} obj * @param {object|array} obj
* @param {...any} props * @param {...any} props
* @returns {any|null} property requested * @returns {any|null} property requested
* @since 0.0.0
*/ */
export function getPropertyWithFallback(obj, ...props) { export function getPropertyWithFallback(obj, ...props) {
for (const prop of props) { for (const prop of props) {

View File

@ -0,0 +1,17 @@
import { upperCaseFirstLowerCaseRest } from '../../../src/utils/texts'
describe('texts utils', () => {
describe('upperCaseFirstLowerCaseRest', () => {
it('converts a full uppercase string', () => {
expect(upperCaseFirstLowerCaseRest('TEST STRING')).toBe('Test string')
})
it('converts a full lowercase string', () => {
expect(upperCaseFirstLowerCaseRest('test string')).toBe('Test string')
})
it('converts a mixed string', () => {
expect(upperCaseFirstLowerCaseRest("i wOn'T woRK")).toBe("I won't work")
})
})
})

View File

@ -0,0 +1,33 @@
import { isValidURL, convertDuration, convertDurationSeparated } from '../../../src/utils/utils'
describe('utils utils (needs refactor)', () => {
describe('isValidURL', () => {
it('returns a positive result with all supported URLs', () => {
expect(isValidURL('https://www.deezer.com')).toBe(true)
expect(isValidURL('https://deezer.page.link')).toBe(true)
expect(isValidURL('https://open.spotify.com')).toBe(true)
expect(isValidURL('https://link.tospotify.com')).toBe(true)
expect(isValidURL('spotify:something')).toBe(true)
})
it('returns a negative result with a not supported URL', () => {
expect(isValidURL('https://www.google.com')).toBe(false)
})
})
describe('convertDuration', () => {
it('converts seconds in the correct format', () => {
expect(convertDuration(120)).toBe('2:00')
expect(convertDuration(60)).toBe('1:00')
expect(convertDuration(30)).toBe('0:30')
})
})
describe('convertDurationSeparated', () => {
it('converts seconds in the correct format', () => {
expect(convertDurationSeparated(120)).toStrictEqual([0, 2, 0])
expect(convertDurationSeparated(60)).toStrictEqual([0, 1, 0])
expect(convertDurationSeparated(30)).toStrictEqual([0, 0, 30])
})
})
})