badgen/test/badgen.spec.js

111 wiersze
3.3 KiB
JavaScript

2022-04-30 09:05:10 +00:00
const tap = require('tap')
const { badgen } = require('../dist')
const icons = require('./assets/icon-data-uri.js')
2018-06-22 16:35:24 +00:00
const originalMath = global.Math
const mockMath = Object.create(global.Math)
mockMath.random = () => 0.5
tap.beforeEach(async t => { global.Math = mockMath })
tap.afterEach(async t => { global.Math = originalMath })
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status }', t => {
const svg = badgen({ label: 'npm', status: 'v1.0.0' })
2018-07-01 08:19:07 +00:00
t.ok(typeof svg === 'string', 'successfully generated')
2018-07-19 05:46:08 +00:00
t.matchSnapshot(svg, 'snapshot')
2018-06-22 16:35:24 +00:00
t.end()
})
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status, color }', t => {
const svg = badgen({ label: 'npm', status: 'v1.0.0', color: 'ADF' })
2018-07-01 08:19:07 +00:00
t.ok(typeof svg === 'string', 'successfully generated')
2018-07-19 05:46:08 +00:00
t.matchSnapshot(svg, 'snapshot')
2018-06-22 16:35:24 +00:00
t.end()
})
2018-07-15 09:25:25 +00:00
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status, style }', t => {
const svg = badgen({ label: 'npm', status: 'v1.0.0', style: 'flat' })
2018-07-15 09:25:25 +00:00
t.ok(typeof svg === 'string', 'successfully generated')
2018-07-19 05:46:08 +00:00
t.matchSnapshot(svg, 'snapshot')
2018-07-15 09:25:25 +00:00
t.end()
})
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status, color, style }', t => {
const svg = badgen({ label: 'npm', status: 'v1.0.0', color: 'ADF', style: 'flat' })
2018-07-15 09:25:25 +00:00
t.ok(typeof svg === 'string', 'successfully generated')
2018-07-19 05:46:08 +00:00
t.matchSnapshot(svg, 'snapshot')
2018-07-15 09:25:25 +00:00
t.end()
})
2018-07-20 03:15:46 +00:00
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status, icon }', t => {
const svg = badgen({ label: 'docker', status: 'icon', icon: icons.chrome })
2018-07-20 03:15:46 +00:00
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2018-08-06 07:25:01 +00:00
tap.test('generate badge with { status, icon }', t => {
2019-07-06 07:34:14 +00:00
const svg = badgen({ label: '', status: 'icon', icon: icons.chrome })
2018-08-06 07:25:01 +00:00
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2018-08-24 05:26:08 +00:00
tap.test('generate badge with { status, icon, iconWidth }', t => {
2019-07-06 07:34:14 +00:00
const svg = badgen({ label: '', status: 'icon', icon: icons.lgtm, iconWidth: 19 })
2018-08-24 05:26:08 +00:00
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2019-07-06 07:34:14 +00:00
tap.test('generate badge with { label, status, icon, style }', t => {
const svg = badgen({ label: 'docker', status: 'icon', style: 'flat', icon: icons.lgtm })
2018-07-20 03:15:46 +00:00
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2018-07-25 03:30:00 +00:00
tap.test('ensure badgen() correctly escapes string inputs', t => {
const svg = badgen({
label: '<escape me>',
status: '<escape me>',
color: '<escape me>',
icon: '<escape me>',
labelColor: '<escape me>',
})
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2019-01-29 12:53:41 +00:00
tap.test('generate bare badge with { status }', t => {
const svg = badgen({ status: 'v1.0.0' })
t.ok(typeof svg === 'string', 'successfully generated')
t.matchSnapshot(svg, 'snapshot')
t.end()
})
tap.test('generate bare badge with { status, color }', t => {
const svg = badgen({ status: 'v1.0.0', color: 'ADF' })
t.ok(typeof svg === 'string', 'successfully generated')
t.matchSnapshot(svg, 'snapshot')
t.end()
})
tap.test('generate bare badge with { status, style }', t => {
const svg = badgen({ status: 'v1.0.0', style: 'flat' })
t.ok(typeof svg === 'string', 'successfully generated')
t.matchSnapshot(svg, 'snapshot')
t.end()
})
tap.test('ensure bare() correctly escapes string inputs', t => {
const svg = badgen({
status: '<escape me>',
color: '<escape me>',
})
t.matchSnapshot(svg, 'snapshot')
t.end()
})
2018-07-25 03:30:00 +00:00
tap.test('type checking', t => {
2019-10-04 02:24:19 +00:00
// @ts-ignore
2019-01-29 12:53:41 +00:00
t.throws(() => badgen({}), TypeError, 'throw if status is non-string')
2018-07-25 03:30:00 +00:00
t.end()
})