-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure modules/ui.js and add tests for the various parts
- Loading branch information
1 parent
4f5bf68
commit 3e4b56c
Showing
13 changed files
with
340 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
LEGEND_DISPLAY_STRATEGY_BY_DATA_ITEM, | ||
LEGEND_DISPLAY_STYLE_FILL, | ||
} from '@dhis2/analytics' | ||
import { | ||
OPTION_LEGEND_DISPLAY_STRATEGY, | ||
OPTION_LEGEND_DISPLAY_STYLE, | ||
OPTION_LEGEND_SET, | ||
OPTION_SHOW_LEGEND_KEY, | ||
getOptionsFromVisualization, | ||
} from '../options.js' | ||
|
||
describe('getOptionsFromVisualization', () => { | ||
it('should return default options if visualization is empty', () => { | ||
const visualization = {} | ||
const result = getOptionsFromVisualization(visualization) | ||
expect(result).toEqual({ | ||
digitGroupSeparator: 'SPACE', | ||
displayDensity: 'NORMAL', | ||
fontSize: 'NORMAL', | ||
showHierarchy: false, | ||
skipRounding: false, | ||
}) | ||
}) | ||
|
||
it('should return options from visualization', () => { | ||
const visualization = { | ||
digitGroupSeparator: 'COMMA', | ||
displayDensity: 'COMPACT', | ||
fontSize: 'SMALL', | ||
showHierarchy: true, | ||
skipRounding: true, | ||
} | ||
|
||
const result = getOptionsFromVisualization(visualization) | ||
expect(result).toEqual({ | ||
digitGroupSeparator: 'COMMA', | ||
displayDensity: 'COMPACT', | ||
fontSize: 'SMALL', | ||
showHierarchy: true, | ||
skipRounding: true, | ||
}) | ||
}) | ||
|
||
it('should include legend options if present in visualization', () => { | ||
const visualization = { | ||
legend: { | ||
strategy: LEGEND_DISPLAY_STRATEGY_BY_DATA_ITEM, | ||
style: LEGEND_DISPLAY_STYLE_FILL, | ||
set: 'legendSet1', | ||
showKey: true, | ||
}, | ||
} | ||
const result = getOptionsFromVisualization(visualization) | ||
expect(result).toEqual({ | ||
digitGroupSeparator: 'SPACE', | ||
displayDensity: 'NORMAL', | ||
fontSize: 'NORMAL', | ||
showHierarchy: false, | ||
skipRounding: false, | ||
[OPTION_LEGEND_DISPLAY_STRATEGY]: | ||
LEGEND_DISPLAY_STRATEGY_BY_DATA_ITEM, | ||
[OPTION_LEGEND_DISPLAY_STYLE]: LEGEND_DISPLAY_STYLE_FILL, | ||
[OPTION_LEGEND_SET]: 'legendSet1', | ||
[OPTION_SHOW_LEGEND_KEY]: true, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { getParentGraphMapFromVisualization } from '../parentGraphMap.js' | ||
|
||
describe('getParentGraphMapFromVisualization', () => { | ||
it('returns an empty object when no org unit axis exists', () => { | ||
const visualization = { | ||
columns: [{ dimension: 'dx' }], | ||
filters: [], | ||
} | ||
const parentGraphMap = getParentGraphMapFromVisualization(visualization) | ||
expect(parentGraphMap).toEqual({}) | ||
}) | ||
|
||
it('returns parent graph map correctly for org units', () => { | ||
const visualization = { | ||
columns: [ | ||
{ dimension: 'dx' }, | ||
{ | ||
dimension: 'ou', | ||
items: [ | ||
{ id: 'orgUnit1', path: '/orgUnit1' }, | ||
{ id: 'orgUnit2', path: '/orgUnit1/orgUnit2' }, | ||
{ id: 'orgUnit3', path: '/orgUnit1/orgUnit3' }, | ||
], | ||
}, | ||
], | ||
} | ||
const parentGraphMap = getParentGraphMapFromVisualization(visualization) | ||
expect(parentGraphMap).toEqual({ | ||
orgUnit1: '', | ||
orgUnit2: 'orgUnit1', | ||
orgUnit3: 'orgUnit1', | ||
}) | ||
}) | ||
|
||
it('handles root org unit case correctly', () => { | ||
const visualization = { | ||
columns: [ | ||
{ dimension: 'dx' }, | ||
{ | ||
dimension: 'ou', | ||
items: [{ id: 'rootOrgUnit', path: '/rootOrgUnit' }], | ||
}, | ||
], | ||
} | ||
const parentGraphMap = getParentGraphMapFromVisualization(visualization) | ||
expect(parentGraphMap).toEqual({ rootOrgUnit: '' }) | ||
}) | ||
|
||
it('handles org unit paths with trailing slashes correctly', () => { | ||
const visualization = { | ||
columns: [ | ||
{ dimension: 'dx' }, | ||
{ | ||
dimension: 'ou', | ||
items: [{ id: 'orgUnit', path: '/orgUnit/' }], | ||
}, | ||
], | ||
} | ||
const parentGraphMap = getParentGraphMapFromVisualization(visualization) | ||
expect(parentGraphMap).toEqual({ orgUnit: 'orgUnit' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
DIMENSION_ID_ORGUNIT, | ||
layoutGetAxisIdDimensionIdsObject, | ||
} from '@dhis2/analytics' | ||
import { getInverseLayout } from './layout.js' | ||
import { removeLastPathSegment } from './orgUnit.js' | ||
|
||
export const getParentGraphMapFromVisualization = (vis) => { | ||
const dimensionIdsByAxis = layoutGetAxisIdDimensionIdsObject(vis) | ||
const inverseLayout = getInverseLayout(dimensionIdsByAxis) | ||
const ouAxis = inverseLayout[DIMENSION_ID_ORGUNIT] | ||
|
||
if (!ouAxis) { | ||
return {} | ||
} | ||
|
||
const parentGraphMap = {} | ||
const ouDimension = vis[ouAxis].find( | ||
(dimension) => dimension.dimension === DIMENSION_ID_ORGUNIT | ||
) | ||
|
||
ouDimension.items | ||
.filter((orgUnit) => orgUnit.path) | ||
.forEach((orgUnit) => { | ||
if ('/' + orgUnit.id === orgUnit.path) { | ||
// root org unit case | ||
parentGraphMap[orgUnit.id] = '' | ||
} else { | ||
const path = removeLastPathSegment(orgUnit.path) | ||
parentGraphMap[orgUnit.id] = | ||
path[0] === '/' ? path.substr(1) : path | ||
} | ||
}) | ||
|
||
return parentGraphMap | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.