Skip to content

Commit

Permalink
feat(mespapiers): Add a function to get the default selected version
Browse files Browse the repository at this point in the history
of SelectPaperVersion component state
  • Loading branch information
Merkur39 committed Dec 14, 2023
1 parent f141944 commit e5e2a99
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Radio from 'cozy-ui/transpiled/react/Radios'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { findPaperVersion } from '../../../helpers/findAttributes'
import { makeReferenceRulesByOcrAttributes } from '../../../helpers/makeReferenceRulesByOcrAttributes'
import CompositeHeader from '../../CompositeHeader/CompositeHeader'
import CompositeHeaderImage from '../../CompositeHeader/CompositeHeaderImage'
import { useFormData } from '../../Hooks/useFormData'
Expand All @@ -21,15 +22,7 @@ import { getAttributesFromOcr, makeMetadataFromOcr } from '../helpers'
const getDefaultSelectedVersion = ({ ocrFromFlagship, ocrAttributes }) => {
const allReferenceRules = ocrAttributes.map(attrs => ({
version: attrs.version,
referenceRules: Object.keys(attrs)
.filter(attr => /(front|back)/.test(attr))
.flatMap(
side =>
attrs[side].referenceRules?.map(ref => ({
...ref,
side
})) || []
)
referenceRules: makeReferenceRulesByOcrAttributes(attrs)
}))

const { version } = findPaperVersion(ocrFromFlagship, allReferenceRules)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Returns reference rules from ocrAttributes with their side
* @param {object} ocrAttributes
* @returns {object[]} reference rules
*/
export const makeReferenceRulesByOcrAttributes = ocrAttributes => {
return Object.keys(ocrAttributes)
.filter(attr => /(front|back)/.test(attr))
.flatMap(
side =>
ocrAttributes[side].referenceRules?.map(ref => ({
...ref,
side
})) || []
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { makeReferenceRulesByOcrAttributes } from './makeReferenceRulesByOcrAttributes'

describe('getReferenceRulesByOcrAttributes', () => {
it('should return reference rules with side attribute', () => {
const ocrAttributes = {
front: {
referenceRules: [{ regex: 'rule1' }, { regex: 'rule2' }]
},
back: {
referenceRules: [{ regex: 'rule3' }, { regex: 'rule4' }]
}
}

const expected = [
{ regex: 'rule1', side: 'front' },
{ regex: 'rule2', side: 'front' },
{ regex: 'rule3', side: 'back' },
{ regex: 'rule4', side: 'back' }
]

expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual(expected)
})

it('should return an empty array if no front or back attributes', () => {
const ocrAttributes = {
other: {
referenceRules: [{ regex: 'rule1' }, { regex: 'rule2' }]
}
}

expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual([])
})

it('should return an empty array if referenceRules is not found', () => {
const ocrAttributes = {
front: {
otherAttribute: 'other value'
}
}

expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual([])
})
})

0 comments on commit e5e2a99

Please sign in to comment.