Skip to content

Commit

Permalink
test: add tooltip test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Jun 2, 2024
1 parent 2949aeb commit c87325b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
50 changes: 50 additions & 0 deletions ui/src/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {fireEvent, render} from '@testing-library/react';
import {Tooltip} from './Tooltip.tsx';
import {assert, expect, test} from 'vitest';
import {Entry, EntryChildren, type EntryType} from "./tool/entry.ts";

function getTestNode(): Entry {
return {
getChildren(): EntryChildren[EntryType] {
return [];
},
getType(): EntryType {
return "unknown";
}, getID(): number {
return 1;
}, getName(): string {
return "test";
}, getSize(): number {
return 12345;
}, toString(): string {
return "test content";
}
}
}

test('Tooltip should render correctly when visible', () => {
const {getByText} = render(<Tooltip visible={true}
node={getTestNode()}/>);
expect(getByText('test')).toBeInTheDocument();
expect(getByText('test content')).toBeInTheDocument();
});

test('Tooltip should not render when not visible', () => {
const r = render(<Tooltip visible={false}
node={getTestNode()}/>);
// should have tooltip-hidden class
expect(r.container.querySelector('.tooltip-hidden')).not.toBeNull();
});

test('Tooltip should update position on mouse move', () => {
const { getByText } = render(<Tooltip visible={true} node={getTestNode()} />);
fireEvent.mouseMove(document, { clientX: 100, clientY: 100 });
const tooltip = getByText('test').parentElement;
if (!tooltip) {
assert.isNotNull(tooltip);
return;
}

expect(tooltip.style.left).toBe('110px');
expect(tooltip.style.top).toBe('130px');
});
7 changes: 3 additions & 4 deletions ui/src/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, {useCallback, useEffect, useMemo, useRef, useState} from "react";
import {HierarchyRectangularNode} from "d3-hierarchy";
import {Entry} from "./tool/entry.ts";

const Tooltip_marginX = 10;
const Tooltip_marginY = 30;

export interface TooltipProps {
node?: HierarchyRectangularNode<Entry>;
node?: Entry;
visible: boolean;
}

Expand All @@ -20,11 +19,11 @@ export const Tooltip: React.FC<TooltipProps> =

const path = useMemo(() => {
if (!node) return "";
return node.data.getName();
return node.getName();
}, [node])

const content = useMemo(() => {
return node?.data.toString() ?? "";
return node?.toString() ?? "";
}, [node]);

const updatePosition = useCallback((mouseCoords: { x: number; y: number }) => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/TreeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function TreeMap({entry}: TreeMapProps) {

return (
<>
<Tooltip visible={showTooltip} node={tooltipNode}/>
<Tooltip visible={showTooltip} node={tooltipNode?.data}/>
<svg xmlns="http://www.w3.org/2000/svg" viewBox={`0 0 ${width} ${height}`} ref={svgRef}>
{nodes}
</svg>
Expand Down
23 changes: 18 additions & 5 deletions ui/src/explorer/file_selector.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @vitest-environment jsdom

import {fireEvent, render} from '@testing-library/react';
import {FileSelector} from './file_selector.tsx';
import {assert, expect, test, vi} from 'vitest';
import {expect, test, vi} from 'vitest';

test('FileSelector should render correctly', () => {
const mockHandler = vi.fn();
Expand All @@ -18,11 +16,17 @@ test('FileSelector should call handler when file size is within limit', () => {
expect(mockHandler).toHaveBeenCalledWith(file);
});

test('FileSelector should not call handler when no file is selected', () => {
const mockHandler = vi.fn();
const {getByLabelText} = render(<FileSelector handler={mockHandler}/>);
fireEvent.change(getByLabelText('Select file'), {target: {files: []}});
expect(mockHandler).not.toHaveBeenCalled();
});

test('FileSelector should not call handler when file size exceeds limit', () => {
const mockHandler = vi.fn();
const {getByLabelText} = render(<FileSelector handler={mockHandler}/>);
const file = new File(["0".repeat(1024 * 1024 * 31)], 'dummy.txt', {type: 'text/plain'});
assert(file.size > 1024 * 1024 * 30);

fireEvent.change(getByLabelText('Select file'), {target: {files: [file]}});
expect(mockHandler).not.toHaveBeenCalled();
Expand All @@ -32,9 +36,18 @@ test('FileSelector should call handler when file size exceeds limit and user cho
const mockHandler = vi.fn();
const {getByLabelText, getByText} = render(<FileSelector handler={mockHandler}/>);
const file = new File(["0".repeat(1024 * 1024 * 31)], 'dummy.txt', {type: 'text/plain'});
assert(file.size > 1024 * 1024 * 30);

fireEvent.change(getByLabelText('Select file'), {target: {files: [file]}});
fireEvent.click(getByText('Continue'));
expect(mockHandler).toHaveBeenCalledWith(file);
});

test('FileSelector should not call handler when file size exceeds limit and user chooses to cancel', () => {
const mockHandler = vi.fn();
const {getByLabelText, getByText} = render(<FileSelector handler={mockHandler}/>);
const file = new File(["0".repeat(1024 * 1024 * 31)], 'dummy.txt', {type: 'text/plain'});

fireEvent.change(getByLabelText('Select file'), {target: {files: [file]}});
fireEvent.click(getByText('Cancel'));
expect(mockHandler).not.toHaveBeenCalled();
});

0 comments on commit c87325b

Please sign in to comment.