diff --git a/src/api/__tests__/print.api.spec.js b/src/api/__tests__/print.api.spec.js index bcafd6c9a..7b39e9404 100644 --- a/src/api/__tests__/print.api.spec.js +++ b/src/api/__tests__/print.api.spec.js @@ -2,6 +2,8 @@ import { expect } from 'chai' import { describe, it } from 'vitest' import { PrintLayout, PrintLayoutAttribute } from '@/api/print.api.js' +import { PRINT_DPI_COMPENSATION } from '@/config/print.config' +import { adjustWidth } from '@/utils/styleUtils' describe('Print API unit tests', () => { describe('PrintLayoutAttribute tests', () => { @@ -61,5 +63,22 @@ describe('Print API unit tests', () => { ) expect(scalesInMapAttr.scales).to.eql(scales) }) + it('calculate the width correctly with the "adjustWidth" function', () => { + // invalid values should return 0 + expect(adjustWidth(100, 'invalid value')).to.eql(0) + expect(adjustWidth(100, null)).to.eql(0) + expect(adjustWidth(100, undefined)).to.eql(0) + expect(adjustWidth(null, 254)).to.eql(0) + expect(adjustWidth(undefined, 254)).to.eql(0) + expect(adjustWidth('invalid value', 254)).to.eql(0) + // the dpi parameter should be a positive number + expect(adjustWidth(100, 0)).to.eql(0) + expect(adjustWidth(100, -15)).to.eql(0) + // checking with a slight margin for float rounding errors. + expect(adjustWidth(100, 254)).to.be.closeTo( + (100 * PRINT_DPI_COMPENSATION) / 254, + 1 / 254 + ) + }) }) }) diff --git a/src/utils/styleUtils.js b/src/utils/styleUtils.js index 08cf55661..75b6985df 100644 --- a/src/utils/styleUtils.js +++ b/src/utils/styleUtils.js @@ -1,7 +1,7 @@ import { Circle, Fill, Stroke, Style } from 'ol/style' import CircleStyle from 'ol/style/Circle.js' -import { PRINT_MAGIC_NUMBER } from '@/config/print.config' +import { PRINT_DPI_COMPENSATION } from '@/config/print.config' import variables from '@/scss/variables-admin.module.scss' const { red, mocassin, mocassinToRed1, mocassinToRed2, malibu, black, white } = variables @@ -159,9 +159,9 @@ export const highlightPointStyle = new Style({ // Change a width according to the change of DPI (from the old geoadmin) // Originally introduced here https://github.com/geoadmin/mf-geoadmin3/pull/3280 export function adjustWidth(width, dpi) { - if (!width || isNaN(width || isNaN(dpi) || dpi <= 0)) { + if (!width || isNaN(width) || !dpi || isNaN(dpi) || dpi <= 0) { return 0 } - return (width * PRINT_MAGIC_NUMBER) / dpi + return (width * PRINT_DPI_COMPENSATION) / dpi }