Files
shopdb-flask/plugins/tools/frontend/labelVars.spec.js

77 lines
3.2 KiB
JavaScript

// The 176x0 label: a cleared number field emitted `NaNin`, the browser dropped
// the declaration, and the code box fell back to auto height, which is zero for
// an image sized at 100% of it. Every case below is a value a number input can
// actually hold.
import { describe, it, expect } from 'vitest'
import { labelVars, LABEL_VAR_NAMES, VAR_FALLBACKS } from './labelVars'
const EMPTY_INPUT = '' // what v-model.number leaves behind on a cleared field
describe('labelVars', () => {
it('emits every property the stylesheet reads', () => {
const vars = labelVars({})
for (const name of LABEL_VAR_NAMES) expect(vars[name]).toBeTruthy()
})
it('never emits NaN or an empty length, whatever it is handed', () => {
for (const junk of [EMPTY_INPUT, NaN, null, undefined, 'abc', Infinity]) {
const vars = labelVars({
labelwidth: junk, labelheight: junk, barheight: junk, codesize: junk,
quiet: junk, gap: junk, padding: junk, labelfont: junk, picturesize: junk,
contentx: junk, contenty: junk, nudgex: junk, nudgey: junk,
pictureopacity: junk,
})
for (const name of LABEL_VAR_NAMES) {
expect(vars[name]).not.toMatch(/NaN|Infinity|undefined|null/)
expect(vars[name]).not.toBe('in')
}
}
})
it('falls back to a printable bar height rather than a zero-height box', () => {
expect(labelVars({ barheight: EMPTY_INPUT })['--tool-barh'])
.toBe(VAR_FALLBACKS.barheight + 'in')
expect(labelVars({ barheight: 0 })['--tool-barh'])
.toBe(VAR_FALLBACKS.barheight + 'in')
expect(labelVars({ barheight: -2 })['--tool-barh'])
.toBe(VAR_FALLBACKS.barheight + 'in')
})
it('keeps a real measurement', () => {
const vars = labelVars({ labelwidth: 2.13, labelheight: 3.38, barheight: 0.5 })
expect(vars['--tool-label-w']).toBe('2.13in')
expect(vars['--tool-label-h']).toBe('3.38in')
expect(vars['--tool-barh']).toBe('0.5in')
})
it('lets padding, quiet zone and gap be zero, because zero is a real answer', () => {
const vars = labelVars({ padding: 0, quiet: 0, gap: 0 })
expect(vars['--tool-pad']).toBe('0in')
expect(vars['--tool-quiet']).toBe('0in')
expect(vars['--tool-gap']).toBe('0in')
})
it('keeps offsets signed - a negative nudge is the whole point of a nudge', () => {
const vars = labelVars({ contentx: -0.05, nudgey: -0.1, contenty: 0.02 })
expect(vars['--tool-content-x']).toBe('-0.05in')
expect(vars['--tool-nudge-y']).toBe('-0.1in')
expect(vars['--tool-content-y']).toBe('0.02in')
})
it('refuses an alignment it does not recognise', () => {
expect(labelVars({ align: 'flex-end' })['--tool-align']).toBe('flex-end')
expect(labelVars({ align: 'sideways' })['--tool-align']).toBe('center')
expect(labelVars({ valign: EMPTY_INPUT })['--tool-valign']).toBe('center')
})
it('clamps watermark opacity into a range that still prints', () => {
expect(labelVars({ pictureopacity: 0.4 })['--tool-pic-opacity']).toBe('0.4')
expect(labelVars({ pictureopacity: 0 })['--tool-pic-opacity'])
.toBe(String(VAR_FALLBACKS.pictureopacity))
expect(labelVars({ pictureopacity: 5 })['--tool-pic-opacity'])
.toBe(String(VAR_FALLBACKS.pictureopacity))
})
})