Skip to content

Commit

Permalink
trying to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jan 28, 2020
1 parent 1d31a44 commit 37a9d0c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ const uiState = {
setSilent: jest.fn(),
};

const flushPromises = () => {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 1000);
});
};

const getWrapper = (props?: Partial<VisLegendProps>) =>
mount(
<I18nProvider>
Expand All @@ -120,9 +128,10 @@ describe('VisLegend Component', () => {
});

describe('Legend open', () => {
beforeEach(() => {
beforeEach(async () => {
mockState.set('vis.legendOpen', true);
wrapper = getWrapper();
await flushPromises();
});

it('should match the snapshot', () => {
Expand All @@ -131,36 +140,42 @@ describe('VisLegend Component', () => {
});

describe('Legend closed', () => {
beforeEach(() => {
beforeEach(async () => {
mockState.set('vis.legendOpen', false);
wrapper = getWrapper();
await flushPromises();
});

it('should match the snapshot', () => {
it('should match the snapshot', async () => {
await flushPromises();
expect(wrapper.html()).toMatchSnapshot();
});
});

describe('Highlighting', () => {
beforeEach(() => {
beforeEach(async () => {
wrapper = getWrapper();
await flushPromises();
});

it('should call highlight handler when legend item is focused', () => {
it('should call highlight handler when legend item is focused', async () => {
await flushPromises();
const first = getLegendItems(wrapper).first();
first.simulate('focus');

expect(vislibVis.handler.highlight).toHaveBeenCalledTimes(1);
});

it('should call highlight handler when legend item is hovered', () => {
it('should call highlight handler when legend item is hovered', async () => {
await flushPromises();
const first = getLegendItems(wrapper).first();
first.simulate('mouseEnter');

expect(vislibVis.handler.highlight).toHaveBeenCalledTimes(1);
});

it('should call unHighlight handler when legend item is blurred', () => {
it('should call unHighlight handler when legend item is blurred', async () => {
await flushPromises();
let first = getLegendItems(wrapper).first();
first.simulate('focus');
first = getLegendItems(wrapper).first();
Expand All @@ -169,7 +184,8 @@ describe('VisLegend Component', () => {
expect(vislibVis.handler.unHighlight).toHaveBeenCalledTimes(1);
});

it('should call unHighlight handler when legend item is unhovered', () => {
it('should call unHighlight handler when legend item is unhovered', async () => {
await flushPromises();
const first = getLegendItems(wrapper).first();

first.simulate('mouseEnter');
Expand All @@ -187,8 +203,9 @@ describe('VisLegend Component', () => {
},
};

expect(() => {
expect(async () => {
wrapper = getWrapper({ vis: newVis });
await flushPromises();
const first = getLegendItems(wrapper).first();
first.simulate('focus');
first.simulate('blur');
Expand All @@ -197,8 +214,9 @@ describe('VisLegend Component', () => {
});

describe('Filtering', () => {
beforeEach(() => {
beforeEach(async () => {
wrapper = getWrapper();
await flushPromises();
});

it('should filter out when clicked', () => {
Expand All @@ -223,8 +241,9 @@ describe('VisLegend Component', () => {
});

describe('Toggles details', () => {
beforeEach(() => {
beforeEach(async () => {
wrapper = getWrapper();
await flushPromises();
});

it('should show details when clicked', () => {
Expand All @@ -236,8 +255,9 @@ describe('VisLegend Component', () => {
});

describe('setColor', () => {
beforeEach(() => {
beforeEach(async () => {
wrapper = getWrapper();
await flushPromises();
});

it('sets the color in the UI state', () => {
Expand All @@ -255,18 +275,20 @@ describe('VisLegend Component', () => {
});

describe('toggleLegend function', () => {
it('click should show legend once toggled from hidden', () => {
it('click should show legend once toggled from hidden', async () => {
mockState.set('vis.legendOpen', false);
wrapper = getWrapper();
await flushPromises();
const toggleButton = wrapper.find('.visLegend__toggle').first();
toggleButton.simulate('click');

expect(wrapper.exists('.visLegend__list')).toBe(true);
});

it('click should hide legend once toggled from shown', () => {
it('click should hide legend once toggled from shown', async () => {
mockState.set('vis.legendOpen', true);
wrapper = getWrapper();
await flushPromises();
const toggleButton = wrapper.find('.visLegend__toggle').first();
toggleButton.simulate('click');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ export class VisLegend extends PureComponent<VisLegendProps, VisLegendState> {
this.props.vis.API.events.filter({ data, negate });
};

canFilter = (item: LegendItem): boolean => {
canFilter = async (item: LegendItem): Promise<boolean> => {
if (CUSTOM_LEGEND_VIS_TYPES.includes(this.props.vislibVis.visConfigArgs.type)) {
return false;
}
const filters = createFiltersFromEvent({ data: item.values });
const filters = await createFiltersFromEvent({ data: item.values });
return Boolean(filters.length);
};

Expand All @@ -124,13 +124,28 @@ export class VisLegend extends PureComponent<VisLegendProps, VisLegendState> {
};

// Most of these functions were moved directly from the old Legend class. Not a fan of this.
getLabels = (data: any, type: string) => {
if (!data) return [];
data = data.columns || data.rows || [data];
getLabels = async (data: any, type: string) => {
let labels = [];
if (CUSTOM_LEGEND_VIS_TYPES.includes(type)) {
const legendLabels = this.props.vislibVis.getLegendLabels();
if (legendLabels) {
labels = map(legendLabels, label => {
return { label };
});
}
} else {
if (!data) return [];
data = data.columns || data.rows || [data];

if (type === 'pie') return getPieNames(data);
labels = type === 'pie' ? getPieNames(data) : this.getSeriesLabels(data);
}

return this.getSeriesLabels(data);
return await Promise.all(
labels.map(async label => ({
...label,
canFilter: await this.canFilter(label),
}))
);
};

refresh = () => {
Expand All @@ -155,18 +170,11 @@ export class VisLegend extends PureComponent<VisLegendProps, VisLegendState> {
this.setState({ open: this.props.vis.params.addLegend });
}

if (CUSTOM_LEGEND_VIS_TYPES.includes(vislibVis.visConfigArgs.type)) {
const legendLabels = this.props.vislibVis.getLegendLabels();
if (legendLabels) {
this.setState({
labels: map(legendLabels, label => {
return { label };
}),
});
}
} else {
this.setState({ labels: this.getLabels(this.props.visData, vislibVis.visConfigArgs.type) });
}
(async () => {
this.setState({
labels: await this.getLabels(this.props.visData, vislibVis.visConfigArgs.type),
});
})();

if (vislibVis.visConfig) {
this.getColor = this.props.vislibVis.visConfig.data.getColorFunc();
Expand Down Expand Up @@ -220,7 +228,7 @@ export class VisLegend extends PureComponent<VisLegendProps, VisLegendState> {
key={item.label}
anchorPosition={anchorPosition}
selected={this.state.selectedLabel === item.label}
canFilter={this.canFilter(item)}
canFilter={item.canFilter}
onFilter={this.filter}
onSelect={this.toggleDetails}
legendId={this.legendId}
Expand Down

0 comments on commit 37a9d0c

Please sign in to comment.