Skip to content

Commit

Permalink
PB-818: adding unit tests for adjust width
Browse files Browse the repository at this point in the history
 - We also added some safeguards against division by 0, and corrected the DPI compensation constant name across the code where we forgot to do it.
  • Loading branch information
ltkum authored and pakb committed Sep 11, 2024
1 parent 57788d4 commit bad40b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/api/__tests__/print.api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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
)
})
})
})
6 changes: 3 additions & 3 deletions src/utils/styleUtils.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit bad40b0

Please sign in to comment.