-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement subtitle and title logic for single value chart
- Loading branch information
1 parent
f39093a
commit b0caa6e
Showing
7 changed files
with
173 additions
and
1 deletion.
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
64 changes: 64 additions & 0 deletions
64
src/visualizations/config/adapters/dhis_highcharts/subtitle/__tests__/singleValue.spec.js
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,64 @@ | ||
import getSingleValueSubtitle from '../singleValue.js' | ||
|
||
jest.mock( | ||
'../../../../../util/getFilterText', | ||
() => () => 'The default filter text' | ||
) | ||
|
||
describe('getSingleValueSubtitle', () => { | ||
it('returns empty subtitle when flag hideSubtitle exists', () => { | ||
expect(getSingleValueSubtitle({ hideSubtitle: true })).toEqual('') | ||
}) | ||
|
||
it('returns the subtitle provided in the layout', () => { | ||
const subtitle = 'The subtitle was already set' | ||
expect(getSingleValueSubtitle({ subtitle })).toEqual(subtitle) | ||
}) | ||
|
||
it('returns an empty string when layout does not have filters', () => { | ||
expect(getSingleValueSubtitle({})).toEqual('') | ||
}) | ||
|
||
it('returns the filter text', () => { | ||
expect(getSingleValueSubtitle({ filters: [] })).toEqual( | ||
'The default filter text' | ||
) | ||
}) | ||
|
||
describe('not dashboard', () => { | ||
describe('layout does not include title', () => { | ||
it('returns empty subtitle', () => { | ||
expect( | ||
getSingleValueSubtitle({ filters: undefined }, {}, false) | ||
).toEqual('') | ||
}) | ||
}) | ||
|
||
/* All these tests have been moved and adjusted from here: | ||
* src/visualizations/config/adapters/dhis_dhis/title/__tests__` | ||
* The test below asserted the default subtitle behaviour, for | ||
* visualization types other than SingleValue. It expected that | ||
* the title was being used as subtitle. It fails now, and I | ||
* believe that this behaviour does not make sense. So instead | ||
* of fixing it, I disabled it. */ | ||
// describe('layout includes title', () => { | ||
// it('returns filter title as subtitle', () => { | ||
// expect( | ||
// getSingleValueSubtitle( | ||
// { filters: undefined, title: 'Chart title' }, | ||
// {}, | ||
// false | ||
// ) | ||
// ).toEqual('The default filter text') | ||
// }) | ||
// }) | ||
}) | ||
|
||
describe('dashboard', () => { | ||
it('returns filter title as subtitle', () => { | ||
expect(getSingleValueSubtitle({ filters: {} }, {}, true)).toEqual( | ||
'The default filter text' | ||
) | ||
}) | ||
}) | ||
}) |
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
17 changes: 17 additions & 0 deletions
17
src/visualizations/config/adapters/dhis_highcharts/subtitle/singleValue.js
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,17 @@ | ||
import getFilterText from '../../../../util/getFilterText.js' | ||
|
||
export default function getSingleValueSubtitle(layout, metaData) { | ||
if (layout.hideSubtitle) { | ||
return '' | ||
} | ||
|
||
if (typeof layout.subtitle === 'string' && layout.subtitle.length) { | ||
return layout.subtitle | ||
} | ||
|
||
if (layout.filters) { | ||
return getFilterText(layout.filters, metaData) | ||
} | ||
|
||
return '' | ||
} |
56 changes: 56 additions & 0 deletions
56
src/visualizations/config/adapters/dhis_highcharts/title/__tests__/singleValue.spec.js
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,56 @@ | ||
import { VIS_TYPE_SINGLE_VALUE } from '../../../../../../modules/visTypes.js' | ||
import getSingleValueTitle from '../singleValue.js' | ||
|
||
jest.mock('../../../../../util/getFilterText', () => () => 'The filter text') | ||
|
||
describe('getSingleValueTitle', () => { | ||
it('returns empty title when flag hideTitle exists', () => { | ||
expect(getSingleValueTitle({ hideTitle: true })).toEqual('') | ||
}) | ||
|
||
it('returns the title provided in the layout', () => { | ||
const title = 'The title was already set' | ||
expect(getSingleValueTitle({ title })).toEqual(title) | ||
}) | ||
|
||
it('returns null when layout does not have columns', () => { | ||
expect(getSingleValueTitle({})).toEqual('') | ||
}) | ||
|
||
it('returns the filter text based on column items', () => { | ||
expect( | ||
getSingleValueTitle({ | ||
columns: [ | ||
{ | ||
items: [{}], | ||
}, | ||
], | ||
}) | ||
).toEqual('The filter text') | ||
}) | ||
|
||
describe('not dashboard', () => { | ||
it('returns filter text as title', () => { | ||
expect( | ||
getSingleValueTitle( | ||
{ | ||
columns: [ | ||
{ | ||
items: [{}], | ||
}, | ||
], | ||
filters: [], | ||
}, | ||
{}, | ||
false | ||
) | ||
).toEqual('The filter text') | ||
}) | ||
}) | ||
|
||
describe('dashboard', () => { | ||
it('returns empty string', () => { | ||
expect(getSingleValueTitle({ filters: {} }, {}, true)).toEqual('') | ||
}) | ||
}) | ||
}) |
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
22 changes: 22 additions & 0 deletions
22
src/visualizations/config/adapters/dhis_highcharts/title/singleValue.js
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,22 @@ | ||
import getFilterText from '../../../../util/getFilterText.js' | ||
|
||
export default function (layout, metaData, dashboard) { | ||
if (layout.hideTitle) { | ||
return '' | ||
} | ||
|
||
if (typeof layout.title === 'string' && layout.title.length) { | ||
return layout.title | ||
} | ||
|
||
if (layout.columns) { | ||
const firstItem = layout.columns[0].items[0] | ||
|
||
const column = Object.assign({}, layout.columns[0], { | ||
items: [firstItem], | ||
}) | ||
|
||
return getFilterText([column], metaData) | ||
} | ||
return '' | ||
} |