Skip to content

Commit

Permalink
fix: legend default config for multiple chart instances (#110)
Browse files Browse the repository at this point in the history
* fix: init legend default config

* test: add tests for default config instances
  • Loading branch information
victorgz authored May 24, 2022
1 parent 30e47ce commit 77917a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
17 changes: 12 additions & 5 deletions __tests__/unit/chartConfigService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import ChartConfigService from 'c/chartConfigService';
import { ATTRIBUTE_CARTESIAN_AXES } from 'c/constants';

describe('c-chart-config-service', () => {
test(`Get new instance of default configuration`, () => {
const firstInstance = ChartConfigService.defaultConfiguration();
const secondInstance = ChartConfigService.defaultConfiguration();

expect(firstInstance).toStrictEqual(secondInstance); // Objects should have the same type and structure
expect(firstInstance).not.toBe(secondInstance); // Objects should not be a shallow copy but a new instance
});
test(`Default configuration`, () => {
const chartConfigService = new ChartConfigService();
expect(chartConfigService.getConfig()).toEqual(
ChartConfigService.DEFAULT_CONFIGURATION
ChartConfigService.defaultConfiguration()
);
});
test(`Update config with null option`, () => {
Expand Down Expand Up @@ -74,7 +81,7 @@ describe('c-chart-config-service', () => {
chartConfigService.updateConfig(testSubject, ATTRIBUTE_CARTESIAN_AXES);
chartConfigService.removeConfig(testSubject, ATTRIBUTE_CARTESIAN_AXES);
expect(chartConfigService.getConfig()).toEqual(
ChartConfigService.DEFAULT_CONFIGURATION
ChartConfigService.defaultConfiguration()
);
});
test(`Remove configuration`, () => {
Expand All @@ -83,14 +90,14 @@ describe('c-chart-config-service', () => {
chartConfigService.updateConfig(testSubject, 'test');
chartConfigService.removeConfig(testSubject, 'test');
expect(chartConfigService.getConfig()).toEqual(
ChartConfigService.DEFAULT_CONFIGURATION
ChartConfigService.defaultConfiguration()
);
});

test(`Clean basic object`, () => {
expect(
ChartConfigService.cleanObject(ChartConfigService.DEFAULT_CONFIGURATION)
).toEqual(ChartConfigService.DEFAULT_CONFIGURATION);
ChartConfigService.cleanObject(ChartConfigService.defaultConfiguration())
).toEqual(ChartConfigService.defaultConfiguration());
});
test(`Clean object with array`, () => {
const testSubject = { array: [10, 20, 30] };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ATTRIBUTE_CARTESIAN_AXES } from 'c/constants';

export default class ChartConfigService {
static DEFAULT_CONFIGURATION = {
legend: {
display: false
}
};
static defaultConfiguration() {
return {
legend: {
display: false
}
};
}

constructor() {
this._config = { ...ChartConfigService.DEFAULT_CONFIGURATION }; // clone default configuration
this._config = ChartConfigService.defaultConfiguration(); // clone default configuration
this._scales = {
xAxes: {},
yAxes: {}
Expand Down

0 comments on commit 77917a9

Please sign in to comment.