Skip to content

Commit

Permalink
fix tests for cell types module
Browse files Browse the repository at this point in the history
  • Loading branch information
bhushankhope committed Oct 16, 2024
1 parent 9ae7670 commit c7eef8a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<cde-visualization-header
(resetView)="visualization.resetView()"
(resetAll)="cellTypesTable.resetSort(); visualization.resetView(); histogram.resetAllCellsColor(); resetCellTypes()"
(downloadNodes)="downloadNodes()"
(downloadEdges)="downloadEdges()"
[homeLink]="homeLink()"
></cde-visualization-header>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { TestBed } from '@angular/core/testing';
import { RenderComponentOptions, render, screen } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { RenderComponentOptions, render } from '@testing-library/angular';
import { mockDeep } from 'jest-mock-extended';
import embed, { Result } from 'vega-embed';

import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatMenuHarness } from '@angular/material/menu/testing';
import { rgbToHex } from '@hra-ui/design-system/color-picker';
import { provideScrolling } from '@hra-ui/design-system/scrolling';
import { ColorMapEntry, DEFAULT_COLOR_MAP_KEY, DEFAULT_COLOR_MAP_VALUE_KEY } from '../models/color-map';
import { EdgeEntry } from '../models/edge';
import { DEFAULT_NODE_TARGET_KEY, DEFAULT_NODE_TARGET_VALUE, NodeEntry } from '../models/node';
import { FileSaverService } from '../services/file-saver/file-saver.service';
import { CdeVisualizationComponent } from './cde-visualization.component';

jest.mock('hra-node-dist-vis/docs/hra-node-dist-vis.wc.js', () => ({}));
Expand Down Expand Up @@ -109,66 +103,6 @@ describe('CdeVisualizationComponent', () => {
embedResult.view.signal.mockReturnThis();
});

it('should update nodes when downloadNodes is called', async () => {
const {
fixture: { componentInstance: instance },
} = await setup({
componentInputs: {
...sampleData,
nodes: sampleNodes,
},
});

const fileSaver = TestBed.inject(FileSaverService);
const fileSaveSpy = jest.spyOn(fileSaver, 'saveCsv').mockReturnValue(undefined);

const downloadNodesButton = screen.getByText('Nodes');
await userEvent.click(downloadNodesButton);
expect(fileSaveSpy).toHaveBeenCalledWith(instance.loadedNodes(), 'nodes.csv');
});

it('should update edges when downloadEdges is called', async () => {
const {
fixture: { componentInstance: instance },
} = await setup({
componentInputs: {
...sampleData,
edges: sampleEdges,
},
});

const fileSaver = TestBed.inject(FileSaverService);
const fileSaveSpy = jest.spyOn(fileSaver, 'saveCsv').mockReturnValue(undefined);

const downloadEdgesButton = screen.getByText('Edges');
await userEvent.click(downloadEdgesButton);
expect(fileSaveSpy).toHaveBeenCalledWith(instance.loadedEdges(), 'edges.csv');
});

it('should update color map when downloadColorMap is called', async () => {
const { fixture } = await setup({
componentInputs: {
...sampleData,
nodes: sampleNodes,
colorMap: sampleColorMap,
},
});

const instance = fixture.componentInstance;
const processedColorMap = instance
.cellTypesAsColorMap()
.map((entry) => ({ ...entry, [instance.colorMapValueKey()]: rgbToHex(entry[instance.colorMapValueKey()]) }));

const fileSaver = TestBed.inject(FileSaverService);
const fileSaveSpy = jest.spyOn(fileSaver, 'saveCsv').mockReturnValue(undefined);

const loader = TestbedHarnessEnvironment.loader(fixture);
const menu = await loader.getHarness(MatMenuHarness);

await menu.clickItem({ text: /Download/ }, { text: /Cell Color Map CSV/ });
expect(fileSaveSpy).toHaveBeenCalledWith(processedColorMap, 'color-map.csv');
});

it('should reset cell types and increase reset counter', async () => {
const {
fixture: { componentInstance: instance },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatCheckboxHarness } from '@angular/material/checkbox/testing';
import { MatMenuHarness } from '@angular/material/menu/testing';
import { provideScrolling } from '@hra-ui/design-system/scrolling';
import { RenderComponentOptions, render, screen } from '@testing-library/angular';
import { CellTypeEntry } from '../../models/cell-type';
Expand All @@ -20,6 +21,53 @@ describe('CellTypesComponent', () => {
});
}

it('should update nodes when downloadNodes is called', async () => {
const emitFn = jest.fn();
const { fixture } = await setup({
inputs: { cellTypes, cellTypesSelection },
on: {
downloadNodes: emitFn,
},
});
const loader = TestbedHarnessEnvironment.loader(fixture);
const menu = await loader.getHarness(MatMenuHarness);

await menu.clickItem({ text: /Download/ }, { text: /Cells CSV/ });
expect(emitFn).toHaveBeenCalled();
});

it('should update edges when downloadEdges is called', async () => {
const emitFn = jest.fn();
const { fixture } = await setup({
inputs: { cellTypes, cellTypesSelection },
on: {
downloadEdges: emitFn,
},
});

const loader = TestbedHarnessEnvironment.loader(fixture);
const menu = await loader.getHarness(MatMenuHarness);

await menu.clickItem({ text: /Download/ }, { text: /Cell Links CSV/ });
expect(emitFn).toHaveBeenCalled();
});

it('should update color map when downloadColorMap is called', async () => {
const emitFn = jest.fn();
const { fixture } = await setup({
inputs: { cellTypes, cellTypesSelection },
on: {
downloadColorMap: emitFn,
},
});

const loader = TestbedHarnessEnvironment.loader(fixture);
const menu = await loader.getHarness(MatMenuHarness);

await menu.clickItem({ text: /Download/ }, { text: /Cell Color Map CSV/ });
expect(emitFn).toHaveBeenCalled();
});

it('should render the component', async () => {
await setup({
componentInputs: { cellTypes, cellTypesSelection },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ export class VisualizationHeaderComponent {
/** Defines an output event for resetting all settings */
readonly resetAll = output();

/** Defines an output event for downloading nodes */
readonly downloadNodes = output();
/** Defines an output event for downloading edges */
readonly downloadEdges = output();

/** Flag to check if reset info tooltip is open */
resetInfoOpen = false;
/** Flag to check if embed info tooltip is open */
Expand Down

0 comments on commit c7eef8a

Please sign in to comment.