Skip to content

Commit

Permalink
schema summary: sort node definitions in the same order as in the for…
Browse files Browse the repository at this point in the history
…m designer (#3546)

* schema summary: sort node definitions in the same order as in the form designer

* fixing test

* fixing tests

---------

Co-authored-by: Stefano Ricci <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 7d6544c commit 3effd09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
3 changes: 2 additions & 1 deletion core/survey/_survey/surveyNodeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ export const traverseHierarchyItemSync = (nodeDefItem, visitorFn, depth = 0) =>
}

export const visitDescendantsAndSelf =
({ nodeDef = null, visitorFn, traverseMethod = TraverseMethod.bfs }) =>
({ visitorFn, nodeDef = null, cycle = null, traverseMethod = TraverseMethod.bfs }) =>
(survey) => {
const nodeDefToVisit = nodeDef ?? getNodeDefRoot(survey)
return Surveys.visitDescendantsAndSelfNodeDef({
survey,
cycle,
nodeDef: nodeDefToVisit,
visitor: visitorFn,
traverseMethod,
Expand Down
31 changes: 13 additions & 18 deletions server/modules/survey/service/schemaSummary/schemaSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,18 @@ const getValidationsSummary = ({ nodeDef }) => {

export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => {
const survey = await SurveyManager.fetchSurveyAndNodeDefsBySurveyId({ surveyId, draft: true, advanced: true })
const nodeDefs = Survey.getNodeDefsArray(survey).filter(
(nodeDef) =>
// exclude "weight" node def created by the processing chain
!(NodeDef.isAnalysis(nodeDef) && SamplingNodeDefs.isWeightNodeDef(nodeDef))
)
const pathByNodeDefUuid = nodeDefs.reduce(
(paths, nodeDef) => ({ ...paths, [nodeDef.uuid]: getNodeDefPath({ survey, nodeDef }) }),
{}
)
// sort node defs by path
nodeDefs.sort((nodeDef1, nodeDef2) => {
const path1 = pathByNodeDefUuid[nodeDef1.uuid]
const path2 = pathByNodeDefUuid[nodeDef2.uuid]
if (path1 > path2) return 1
if (path2 > path1) return -1
return 0
})
const nodeDefs = []
Survey.visitDescendantsAndSelf({
cycle,
visitorFn: (nodeDef) => {
if (
// exclude "weight" node def created by the processing chain
!(NodeDef.isAnalysis(nodeDef) && SamplingNodeDefs.isWeightNodeDef(nodeDef))
) {
nodeDefs.push(nodeDef)
}
},
})(survey)

const getCategoryName = (nodeDef) => {
if (!NodeDef.isCode(nodeDef)) return ''
Expand Down Expand Up @@ -114,7 +109,7 @@ export const generateSchemaSummaryItems = async ({ surveyId, cycle }) => {
return {
uuid,
name: NodeDef.getName(nodeDef),
path: pathByNodeDefUuid[uuid],
path: getNodeDefPath({ survey, nodeDef }),
parentEntity: NodeDef.getName(Survey.getNodeDefParent(nodeDef)(survey)),
// labels
...languages.reduce(
Expand Down
19 changes: 6 additions & 13 deletions test/e2e/tests/surveySchemaSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { gotoFormDesigner } from './_navigation'
import { cluster } from '../mock/nodeDefs'
import { parseCsvAsync } from '../../utils/csvUtils'

const getTestNodeDefsOrderedByPath = () => {
const getTestNodeDefs = () => {
const items = []

const stack = [{ parentPath: null, nodeDef: cluster }]
Expand All @@ -27,14 +27,7 @@ const getTestNodeDefsOrderedByPath = () => {
)
}
}
// sort items by path
return items.sort((item1, item2) => {
const path1 = item1.path
const path2 = item2.path
if (path1 > path2) return 1
if (path2 > path1) return -1
return 0
})
return items
}

export default () =>
Expand All @@ -58,21 +51,21 @@ export default () =>
await expect(fs.existsSync(filePath)).toBeTruthy()
})

const nodeDefsOrderedByPath = getTestNodeDefsOrderedByPath()
const nodeDefs = getTestNodeDefs()

test(`Check generated schema summary`, async () => {
await expect(fs.existsSync(filePath)).toBeTruthy()

data = await parseCsvAsync(filePath)

await expect(data.length).toBe(nodeDefsOrderedByPath.length)
await expect(data.length).toBe(nodeDefs.length)
})

nodeDefsOrderedByPath.forEach((nodeDefItem, index) => {
nodeDefs.forEach((nodeDefItem) => {
const { nodeDef, path: nodeDefPath } = nodeDefItem
test(`Check ${nodeDef.name} def`, async () => {
await expect(data).toBeDefined()
const nodeDefData = data[index]
const nodeDefData = data.find((dataItem) => dataItem.name === nodeDef.name)
await expect(Object.keys(nodeDefData)).toEqual([
'uuid',
'name',
Expand Down

0 comments on commit 3effd09

Please sign in to comment.