test(utils): increase coverage (part 1) (#2795)

* close test(utils): more test for Collection.ts #2793

* fix: after each not defined

* close test(utils): more test for base64.ts #2796

* close test(utils): more test for casing.ts #2797

* test(utils): remove dev code

* close #2799

* fix #2800

* close #2823

* test(utils): fix site move

* close #2826

* refactor(utils): remove redundant if

* close #2827

* close #2828
This commit is contained in:
Jonathan Ho
2023-03-06 14:48:16 -08:00
committed by GitHub
parent 147327b239
commit 918cee2514
11 changed files with 511 additions and 137 deletions

View File

@@ -107,7 +107,7 @@ export function nextRefill(bucket: LeakyBucket): number {
// Since this bucket is lazy update the tokens before calculating the next refill.
updateTokens(bucket)
return performance.now() - bucket.lastRefill + bucket.refillInterval
return bucket.refillInterval - performance.now() + bucket.lastRefill
}
export async function acquire(bucket: LeakyBucket, amount: number, highPriority = false): Promise<void> {

View File

@@ -9,7 +9,7 @@ export function camelize <T>(object: T): Camelize<T> {
const obj = {} as Camelize<T>
;(Object.keys(object) as Array<keyof T>).forEach((key) => {
// @ts-expect-error js hack
;(obj[typeof key === 'string' ? snakeToCamelCase(key) : key] as Camelize<(T & object)[keyof T]>) = camelize(object[key])
;(obj[snakeToCamelCase(key)] as Camelize<(T & object)[keyof T]>) = camelize(object[key])
})
return obj
}
@@ -25,8 +25,7 @@ export function snakelize <T>(object: T): Snakelize<T> {
const obj = {} as Snakelize<T>
;(Object.keys(object) as Array<keyof T>).forEach((key) => {
// @ts-expect-error js hack
;(obj[typeof key === 'string' ? camelToSnakeCase(key) : key] as Snakelize<(T & object)[keyof T]>) = snakelize(object[key])
;(obj[camelToSnakeCase(key)] as Snakelize<(T & object)[keyof T]>) = snakelize(object[key])
})
return obj
}

View File

@@ -37,7 +37,7 @@ export function avatarUrl(
): string {
return options?.avatar
? formatImageUrl(
`https://cdn.discordapp.com/avatars/${userId}/${typeof options?.avatar === 'string' ? options.avatar : iconBigintToHash(options?.avatar)}`,
`https://cdn.discordapp.com/avatars/${userId}/${typeof options.avatar === 'string' ? options.avatar : iconBigintToHash(options.avatar)}`,
options?.size ?? 128,
options?.format,
)
@@ -125,7 +125,7 @@ export function guildSplashUrl(
* @returns The link to the resource.
*/
export function getWidgetImageUrl(guildId: BigString, options?: GetGuildWidgetImageQuery): string {
let url = `https://cdn.discordapp.com/guilds/${guildId}/widget.png`
let url = `https://discordapp.com/api/guilds/${guildId}/widget.png`
if (options?.style) {
url += `?style=${options.style}`

View File

@@ -10,15 +10,28 @@ describe('base64.ts', () => {
})
it('can encode Uint8Array to base64', () => {
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173, 162]))).to.be.equal('TWFuINCB8KStog==')
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173]))).to.be.equal('TWFuINCB8KSt')
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173, 162, 63]))).to.be.equal('TWFuINCB8KStoj8=')
})
it('can encode Buffer to base64', () => {
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173, 162]))).to.be.equal('TWFuINCB8KStog==')
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173]))).to.be.equal('TWFuINCB8KSt')
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173, 162, 63]))).to.be.equal('TWFuINCB8KStoj8=')
})
})
describe('decode', () => {
it('can dencode string to Uint8Array', () => {
expect(new TextDecoder().decode(decode('TWFuINCB8KStog=='))).to.be.equal('Man Ё𤭢')
expect(new TextDecoder().decode(decode('TWFuINCB8KSt'))).to.be.equal('Man Ё\ufffd')
expect(new TextDecoder().decode(decode('TWFuINCB8KStoj8='))).to.be.equal('Man Ё𤭢?')
})
it('will throw an error with invalid string', () => {
expect(() => decode('=adw')).to.throw('Unable to parse base64 string.')
expect(() => decode('a')).to.throw('Unable to parse base64 string.')
expect(() => decode('$avs')).to.throw('Unable to parse base64 string.')
expect(() => decode('~daw')).to.throw('Unable to parse base64 string.')
})
})

View File

@@ -1,6 +1,6 @@
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { camelize, snakeToCamelCase } from '../src/casing.js'
import { camelize, snakelize, snakeToCamelCase } from '../src/casing.js'
describe('casting.ts', () => {
describe('camelize function', () => {
@@ -44,4 +44,46 @@ describe('casting.ts', () => {
})
})
})
describe('snakelize function', () => {
it('will convert snake case object to camel case object', () => {
expect(
snakelize({
testAxByCz: 'dummy_dx_ey_fz',
testgxhyiz: 'dummyjxkylz',
32: 'adw_dw',
}),
).to.deep.equal({
test_ax_by_cz: 'dummy_dx_ey_fz',
testgxhyiz: 'dummyjxkylz',
32: 'adw_dw',
})
})
it('will convert array of snake case object to camel case object', () => {
expect(
snakelize([
{
testAxByCz: 'dummy_dx_ey_fz',
},
{
testGxHyIz: 'dummy_jx_ky_lz',
},
]),
).to.deep.equal([
{
test_ax_by_cz: 'dummy_dx_ey_fz',
},
{
test_gx_hy_iz: 'dummy_jx_ky_lz',
},
])
})
describe('snakeToCamelCase function', () => {
it('will convert string snake case to camel case', () => {
expect(snakeToCamelCase('sd_sd')).to.equal('sdSd')
})
})
})
})

View File

@@ -1,142 +1,220 @@
import { expect } from 'chai'
import { beforeEach, describe, it } from 'mocha'
import { afterEach, beforeEach, describe, it } from 'mocha'
import sinon from 'sinon'
import { Collection } from '../src/Collection.js'
describe('collection.ts', () => {
let collection: Collection<any, any>
beforeEach(() => {
collection = new Collection()
afterEach(() => {
sinon.restore()
})
it('[collection] collection values to array', () => {
const testCollection = new Collection([
['best', 'tri'],
['proficient', 'yui'],
])
expect(testCollection.array()).to.be.deep.equal(['tri', 'yui'])
})
describe('Collection class', () => {
let collection: Collection<any, any>
it('[collection] get a random value', () => {
const testCollection = new Collection([['best', 'tri']])
expect(testCollection.random() ?? '').to.be.oneOf(['best', 'tri'])
expect(collection.random()).to.be.undefined
})
describe('', () => {
beforeEach(() => {
collection.set('best developer', 'triformine')
collection = new Collection([
['best', 'tri'],
['proficient', 'yui'],
])
})
it('[collection] Set a value without maxSize', () => {
expect(collection.size).to.be.equal(1)
expect(collection.get('best developer')).to.be.equal('triformine')
describe('.array() method', () => {
it('will return values as array', () => {
expect(collection.array()).to.be.deep.equal(['tri', 'yui'])
})
})
describe('', () => {
describe('.random() method', () => {
it('will get a random value', () => {
expect(collection.random() ?? '').to.be.oneOf(['tri', 'yui'])
expect(new Collection().random()).to.be.undefined
})
})
describe('.set() method', () => {
describe('without maxSize', () => {
it('will set a value', () => {
collection.set('best developer', 'triformine')
expect(collection.size).to.be.equal(3)
expect(collection.get('best developer')).to.be.equal('triformine')
})
})
describe('with maxSize', () => {
const maxSize = 2
beforeEach(() => {
collection = new Collection([], {
maxSize,
})
})
it('will set a value when not over max size', () => {
collection.set('foo', 'bar')
collection.set('me', 'you')
expect(collection.size).to.be.equal(2)
})
it('will not set a value when over max size', () => {
collection.set('foo', 'bar')
collection.set('me', 'you')
expect(collection.size).to.be.equal(2)
collection.set('this', 'not')
expect(collection.size).to.be.equal(2)
})
})
})
describe('.forceSet() method', () => {
const maxSize = 2
beforeEach(() => {
collection.set('deno', 'yes')
})
it('[collection] get the value of the first element', () => {
expect(collection.first()).to.be.equal('triformine')
collection = new Collection(
[
['foo', 'bar'],
['me', 'you'],
],
{ maxSize },
)
})
it('[collection] get the value of the last element', () => {
expect(collection.last()).to.be.equal('yes')
it('will ignore maxSize and set a value ', () => {
collection.forceSet('this', 'not')
expect(collection.size).to.be.equal(3)
})
})
})
describe('[collection] Create a collection with maxSize', () => {
const maxSize = 2
const maxCollection = new Collection([], {
maxSize,
})
expect(maxCollection).to.exist
expect(maxCollection.maxSize).to.exist
expect(maxCollection.maxSize).to.be.equal(maxSize)
describe('[collection] Test if maxSize works properly', () => {
maxCollection.set('foo', 'bar')
maxCollection.set('me', 'you')
expect(maxCollection.size).to.be.equal(2)
maxCollection.set('this', 'not')
expect(maxCollection.size).to.be.equal(2)
it('[collection] Test if forceSet ignore maxSize', () => {
maxCollection.forceSet('this', 'not')
expect(maxCollection.size).to.be.equal(3)
describe('.first() method', () => {
it('will get the value of the first element', () => {
expect(collection.first()).to.be.equal('tri')
})
})
})
const testCollection = new Collection([
['a', 1],
['b', 2],
['c', 3],
])
describe('.last() method', () => {
it('get the value of the last element', () => {
expect(collection.last()).to.be.equal('yui')
})
})
it('[collection] find by key or value', () => {
expect(testCollection.find((v, k) => v === 2)).to.be.equal(2)
expect(testCollection.find((v, k) => k === 'b')).to.be.equal(2)
})
const testCollection = new Collection([
['a', 1],
['b', 2],
['c', 3],
])
it('[collection] filter by key or value', () => {
expect(testCollection.filter((v, k) => v === 3).size).to.be.equal(1)
expect(testCollection.filter((v, k) => k === 'd').size).to.be.equal(0)
})
describe('.find() method', () => {
it('will find value by value', () => {
expect(collection.find((v, k) => v === 'tri')).to.be.equal('tri')
expect(collection.find((v, k) => v === 'skillz')).to.be.undefined
})
it('[collection] map', () => {
expect(testCollection.map((k, v) => `${v}${k}`)).to.be.deep.equal(['a1', 'b2', 'c3'])
})
it('will find value by key', () => {
expect(collection.find((v, k) => k === 'proficient')).to.be.equal('yui')
expect(collection.find((v, k) => k === 'skillz')).to.be.undefined
})
})
it('[collection] some', () => {
expect(testCollection.some((v, _) => v === 1)).to.be.equal(true)
expect(testCollection.some((v, _) => v === 4)).to.be.equal(false)
})
describe('.filter() method', () => {
it('will filter by key', () => {
expect(collection.filter((v, k) => v === 'yui').array()).to.deep.equal(['yui'])
expect(collection.filter((v, k) => v === 'skillz').array()).to.deep.equal([])
})
it('will filter by key', () => {
expect(collection.filter((v, k) => k === 'best').array()).to.deep.equal(['tri'])
expect(collection.filter((v, k) => k === 'skillz').array()).to.deep.equal([])
})
})
it('[collection] every', () => {
expect(testCollection.every((v, _) => v !== 0)).to.be.equal(true)
expect(testCollection.every((v, _) => v === 1)).to.be.equal(false)
})
it('map', () => {
expect(testCollection.map((k, v) => `${v}${k}`)).to.be.deep.equal(['a1', 'b2', 'c3'])
})
it('[collection] reduce', () => {
expect(testCollection.reduce((acc, val) => acc + val, 0)).to.be.equal(6)
})
it('some', () => {
expect(testCollection.some((v, _) => v === 1)).to.be.equal(true)
expect(testCollection.some((v, _) => v === 4)).to.be.equal(false)
})
it('[collection] start sweeper', async () => {
const clock = sinon.useFakeTimers()
const sweeperCollection = new Collection(
[
['a', 1],
['b', 2],
],
{
sweeper: {
filter: (v, _) => v === 1,
interval: 50,
},
},
)
it('every', () => {
expect(testCollection.every((v, _) => v !== 0)).to.be.equal(true)
expect(testCollection.every((v, _) => v === 1)).to.be.equal(false)
})
try {
await clock.tickAsync(49)
expect(sweeperCollection.size).to.be.equal(2)
await clock.tickAsync(1)
expect(sweeperCollection.size).to.be.equal(1)
} catch (err) {
sweeperCollection.stopSweeper()
it('reduce', () => {
expect(testCollection.reduce((acc, val) => acc + val, 0)).to.be.equal(6)
})
throw err
}
describe('sweeper', () => {
let clock: sinon.SinonFakeTimers
sweeperCollection.stopSweeper()
clock.restore()
beforeEach(() => {
clock = sinon.useFakeTimers()
})
afterEach(() => {
clock.restore()
})
it('start sweeper', async () => {
const sweeperCollection = new Collection(
[
['a', 1],
['b', 2],
],
{
sweeper: {
filter: (v, _) => v === 1,
interval: 50,
},
},
)
try {
await clock.tickAsync(49)
expect(sweeperCollection.size).to.be.equal(2)
await clock.tickAsync(1)
expect(sweeperCollection.size).to.be.equal(1)
} catch (err) {
sweeperCollection.stopSweeper()
throw err
}
sweeperCollection.stopSweeper()
})
describe('.changeSweeperInterval() method', () => {
it('will call startSweeper with new interval', () => {
collection.startSweeper({ filter: () => false, interval: 1000 })
collection.changeSweeperInterval(20000)
expect(collection.sweeper?.interval).to.equal(20000)
})
it('will not startsweeper if not started', () => {
collection.changeSweeperInterval(20000)
expect(collection.sweeper).to.undefined
})
})
describe('.changeSweeperFilter() method', () => {
it('will call startSweeper with new interval', () => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const newFilter = () => true
collection.startSweeper({ filter: () => false, interval: 1000 })
collection.changeSweeperFilter(newFilter)
expect(collection.sweeper?.filter).to.equal(newFilter)
})
it('will not startsweeper if not started', () => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const newFilter = () => true
collection.changeSweeperFilter(newFilter)
expect(collection.sweeper).to.undefined
})
})
})
})
})

View File

@@ -0,0 +1,175 @@
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { avatarUrl, emojiUrl, formatImageUrl, getWidgetImageUrl, guildBannerUrl, guildIconUrl, guildSplashUrl } from '../src/images.js'
describe('images.ts', () => {
describe('formatImageUrl function', () => {
it('will return formated url with default size 128 and jpg', () => {
expect(formatImageUrl('https://skillz.is.pro/image')).to.be.equal('https://skillz.is.pro/image.jpg?size=128')
})
it('will return formated url with given size', () => {
expect(formatImageUrl('https://skillz.is.pro/image', 1024)).to.be.equal('https://skillz.is.pro/image.jpg?size=1024')
})
it('will return formated url with given size and format', () => {
expect(formatImageUrl('https://skillz.is.pro/image', 1024, 'gif')).to.be.equal('https://skillz.is.pro/image.gif?size=1024')
})
it('will return formated url with default size and format', () => {
expect(formatImageUrl('https://skillz.is.pro/image', undefined, 'gif')).to.be.equal('https://skillz.is.pro/image.gif?size=128')
})
describe('without format', () => {
it('will use gif if a_ is found', () => {
expect(formatImageUrl('https://cdn.discordapp.com/avatars/568505543511259840/a_482491d6dcf12e12746ccd3148f0c646')).to.be.equal(
'https://cdn.discordapp.com/avatars/568505543511259840/a_482491d6dcf12e12746ccd3148f0c646.gif?size=128',
)
})
it('will use jpg if no a_ is found', () => {
expect(formatImageUrl('https://cdn.discordapp.com/avatars/568505543511259840/482491d6dcf12e12746ccd3148f0c646')).to.be.equal(
'https://cdn.discordapp.com/avatars/568505543511259840/482491d6dcf12e12746ccd3148f0c646.jpg?size=128',
)
})
})
})
describe('emojiUrl function', () => {
it('can format emoji url with png as default ext', () => {
expect(emojiUrl('1079823706743918622')).to.equal('https://cdn.discordapp.com/emojis/1079823706743918622.png')
})
it('can format emoji url with gif as ext', () => {
expect(emojiUrl('1079584570661404724', true)).to.equal('https://cdn.discordapp.com/emojis/1079584570661404724.gif')
})
})
describe('avatarUrl function', () => {
it('will return the url for given avatar icon hash', () => {
expect(
avatarUrl('207324334904049664', '9130', {
avatar: 'db26a6fb924c985f66b79364cf5797b7',
}),
).to.equal('https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.jpg?size=128')
})
it('will return the url for given avatar icon bigint', () => {
expect(
avatarUrl('207324334904049664', '9130', {
avatar: 4034407661299384404326332419647968090039n,
}),
).to.equal('https://cdn.discordapp.com/avatars/207324334904049664/db26a6fb924c985f66b79364cf5797b7.jpg?size=128')
})
it('will return the url for default avatar', () => {
expect(
avatarUrl('207324334904049664', '9130', {
avatar: undefined,
}),
).to.equal('https://cdn.discordapp.com/embed/avatars/0.png')
})
})
describe('guildBannerUrl function', () => {
it("will return the url for given guild's banner's icon hash", () => {
expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d' })).to.equal(
'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.jpg?size=128',
)
})
it("will return the url for given guild's banner's icon big int", () => {
expect(guildBannerUrl('785384884197392384', { banner: 3806581668328291509506503737571885116189n })).to.equal(
'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.jpg?size=128',
)
})
it("will return the url for given guild's banner with format", () => {
expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d', format: 'webp' })).to.equal(
'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.webp?size=128',
)
})
it("will return the url for given guild's banner with size", () => {
expect(guildBannerUrl('785384884197392384', { banner: '2fc0f64acd7a326e0c93c123db02eb1d', size: 256 })).to.equal(
'https://cdn.discordapp.com/banners/785384884197392384/2fc0f64acd7a326e0c93c123db02eb1d.jpg?size=256',
)
})
it('will return undefined without given banner', () => {
expect(guildBannerUrl('785384884197392384', {})).to.equal(undefined)
})
})
describe('guildIconUrl function', () => {
it("will return the url for given guild's icon's icon hash", () => {
expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1')).to.equal(
'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.jpg?size=128',
)
})
it("will return the url for given guild's icon's icon big int", () => {
expect(guildIconUrl('785384884197392384', 3908877832746069276949504774836813649329n)).to.equal(
'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.jpg?size=128',
)
})
it("will return the url for given guild's icon with format", () => {
expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1', { format: 'webp' })).to.equal(
'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.webp?size=128',
)
})
it("will return the url for given guild's icon with size", () => {
expect(guildIconUrl('785384884197392384', '7cb67c989d54d824239b2bb4270955b1', { size: 256 })).to.equal(
'https://cdn.discordapp.com/icons/785384884197392384/7cb67c989d54d824239b2bb4270955b1.jpg?size=256',
)
})
it('will return undefined without given icon', () => {
expect(guildIconUrl('785384884197392384', undefined)).to.equal(undefined)
})
})
describe('guildSplashUrl function', () => {
it("will return the url for given guild's splash's icon big hash", () => {
expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c')).to.equal(
'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.jpg?size=128',
)
})
it("will return the url for given guild's splash's icon big int", () => {
expect(guildSplashUrl('785384884197392384', 3786271587545740215322752847582515594636n)).to.equal(
'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.jpg?size=128',
)
})
it("will return the url for given guild's splash with format", () => {
expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c', { format: 'webp' })).to.equal(
'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.webp?size=128',
)
})
it("will return the url for given guild's splash with size", () => {
expect(guildSplashUrl('785384884197392384', '207961ff6c41f119874e10efc602858c', { size: 2048 })).to.equal(
'https://cdn.discordapp.com/splashes/785384884197392384/207961ff6c41f119874e10efc602858c.jpg?size=2048',
)
})
it('will return undefined without given icon', () => {
expect(guildSplashUrl('785384884197392384', undefined)).to.equal(undefined)
})
})
describe('getWidgetImageUrl function', () => {
it("will return the url for given guild's widget", () => {
expect(getWidgetImageUrl('785384884197392384')).to.equal('https://discordapp.com/api/guilds/785384884197392384/widget.png')
})
it("will return the url for given guild's widget with the style", () => {
expect(getWidgetImageUrl('785384884197392384', { style: 'banner2' })).to.equal(
'https://discordapp.com/api/guilds/785384884197392384/widget.png?style=banner2',
)
})
})
})

View File

@@ -14,7 +14,7 @@ describe('token.ts', () => {
})
it('Will throw when token is undefined.', () => {
expect(() => removeTokenPrefix(undefined)).to.throw
expect(() => removeTokenPrefix(undefined)).to.throw()
})
})

View File

@@ -0,0 +1,61 @@
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit } from '../src/typeguards.js'
describe('typeguard.ts', () => {
describe('isGetMessagesAfter function', () => {
it('will return true if has after', () => {
expect(
isGetMessagesAfter({
after: '684146387468463',
}),
).equal(true)
})
it("will return false if don't has after", () => {
expect(isGetMessagesAfter({})).equal(false)
})
})
describe('isGetMessagesBefore function', () => {
it('will return true if has after', () => {
expect(
isGetMessagesBefore({
before: '684146387468463',
}),
).equal(true)
})
it("will return false if don't has after", () => {
expect(isGetMessagesBefore({})).equal(false)
})
})
describe('isGetMessagesAround function', () => {
it('will return true if has after', () => {
expect(
isGetMessagesAround({
around: '684146387468463',
}),
).equal(true)
})
it("will return false if don't has after", () => {
expect(isGetMessagesAround({})).equal(false)
})
})
describe('isGetMessagesAfter function', () => {
it('will return true if has after', () => {
expect(
isGetMessagesLimit({
limit: 54,
}),
).equal(true)
})
it("will return false if don't has after", () => {
expect(isGetMessagesLimit({})).equal(false)
})
})
})

File diff suppressed because one or more lines are too long

View File

@@ -1,8 +1,7 @@
import { expect } from 'chai'
import { afterEach, beforeEach, describe, it } from 'mocha'
import sinon from 'sinon'
import { delay } from '../src/utils.js'
import { formatImageUrl } from '../src/images.js'
import { delay, hasProperty } from '../src/utils.js'
describe('utils.ts', () => {
let clock: sinon.SinonFakeTimers
@@ -16,22 +15,29 @@ describe('utils.ts', () => {
clock.restore()
})
it('will', async () => {
let delayEnded = false
delay(31).then(() => {
delayEnded = true
describe('delay function', () => {
it('will delay/sleep for given time', async () => {
let delayEnded = false
delay(31).then(() => {
delayEnded = true
})
expect(delayEnded).to.be.false
await clock.tickAsync(30)
expect(delayEnded).to.be.false
await clock.tickAsync(31)
expect(delayEnded).to.be.true
})
})
describe('hasProperty funciton', async () => {
const obj = { prop: 'lts372005' }
it('will return true if it does have property', () => {
expect(hasProperty(obj, 'prop')).equal(true)
})
it('will return false if it does not have property', () => {
expect(hasProperty(obj, 'lts372005')).equal(false)
})
expect(delayEnded).to.be.false
await clock.tickAsync(30)
expect(delayEnded).to.be.false
await clock.tickAsync(31)
expect(delayEnded).to.be.true
})
})
it('[utils] format image url', () => {
expect(formatImageUrl('https://skillz.is.pro')).to.be.equal('https://skillz.is.pro.jpg?size=128')
expect(formatImageUrl('https://skillz.is.pro', 1024)).to.be.equal('https://skillz.is.pro.jpg?size=1024')
expect(formatImageUrl('https://skillz.is.pro', 1024, 'gif')).to.be.equal('https://skillz.is.pro.gif?size=1024')
expect(formatImageUrl('https://skillz.is.pro', undefined, 'gif')).to.be.equal('https://skillz.is.pro.gif?size=128')
})