Skip to content
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 35 additions & 43 deletions test/methods/dataframe/display/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DataFrame } from '../../../../src/core/dataframe/DataFrame.js';
import { display } from '../../../../src/methods/dataframe/display/display.js';

import {
testWithBothStorageTypes,
createDataFrameWithStorage,
} from '../../../utils/storageTestUtils.js';

// Mock the module
vi.mock('../../../../src/display/web/html.js', () => ({
display: vi.fn(),
Expand All @@ -21,50 +16,47 @@ describe('DataFrame display method', () => {
mockWebDisplay.mockReset();
});

// Run tests with both storage types
testWithBothStorageTypes((storageType) => {
describe(`with ${storageType} storage`, () => {
// Create test data frame with people data for better readability in tests
const testData = [
{ name: 'Alice', age: 25, city: 'New York' },
{ name: 'Bob', age: 30, city: 'Boston' },
{ name: 'Charlie', age: 35, city: 'Chicago' },
];

// Create DataFrame with the specified storage type
const df = createDataFrameWithStorage(DataFrame, testData, storageType);
describe('with standard storage', () => {
// Create test data frame with people data for better readability in tests
const testData = [
{ name: 'Alice', age: 25, city: 'New York' },
{ name: 'Bob', age: 30, city: 'Boston' },
{ name: 'Charlie', age: 35, city: 'Chicago' },
];

it('should call the web display function with the frame', () => {
// Call display function directly
const displayFn = display();
displayFn(df);
// Create DataFrame using fromRows
const df = DataFrame.fromRows(testData);

// Check that the web display function was called with the frame
expect(mockWebDisplay).toHaveBeenCalledWith(df, expect.any(Object));
});
it('should call the web display function with the frame', () => {
// Call display function directly
const displayFn = display();
displayFn(df);

it('should return the frame for method chaining', () => {
// Call display function and check the return value
const displayFn = display();
const result = displayFn(df);
// Check that the web display function was called with the frame
expect(mockWebDisplay).toHaveBeenCalledWith(df, expect.any(Object));
});

// Check that the function returns the frame
expect(result).toBe(df);
});
it('should return the frame for method chaining', () => {
// Call display function and check the return value
const displayFn = display();
const result = displayFn(df);

it('should pass options to the web display function', () => {
// Call display function with options
const displayFn = display();
const options = {
maxRows: 5,
maxCols: 2,
className: 'custom-table',
};
displayFn(df, options);
// Check that the function returns the frame
expect(result).toBe(df);
});

// Check that the web display function was called with the options
expect(mockWebDisplay).toHaveBeenCalledWith(df, options);
});
it('should pass options to the web display function', () => {
// Call display function with options
const displayFn = display();
const options = {
maxRows: 5,
maxCols: 2,
className: 'custom-table',
};
displayFn(df, options);

// Check that the web display function was called with the options
expect(mockWebDisplay).toHaveBeenCalledWith(df, options);
});
});
});
246 changes: 113 additions & 133 deletions test/methods/dataframe/display/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { describe, it, expect, vi } from 'vitest';
import { DataFrame } from '../../../../src/core/dataframe/DataFrame.js';
import { print } from '../../../../src/methods/dataframe/display/print.js';

import {
testWithBothStorageTypes,
createDataFrameWithStorage,
} from '../../../utils/storageTestUtils.js';

// Test data to be used in all tests
const sampleData = [
{ value: 10, category: 'A', mixed: '20' },
Expand All @@ -17,134 +12,119 @@ const sampleData = [
];

describe('DataFrame print method', () => {
// Run tests with both storage types
testWithBothStorageTypes((storageType) => {
describe(`with ${storageType} storage`, () => {
// Create test data frame with people data for better readability in tests
const testData = [
{ name: 'Alice', age: 25, city: 'New York' },
{ name: 'Bob', age: 30, city: 'Boston' },
{ name: 'Charlie', age: 35, city: 'Chicago' },
{ name: 'David', age: 40, city: 'Denver' },
{ name: 'Eve', age: 45, city: 'El Paso' },
];

// Create DataFrame with the specified storage type
const df = createDataFrameWithStorage(DataFrame, testData, storageType);

it('should format data as a table string', () => {
// Mock console.log to check output
const consoleSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {});

// Call print function directly
const printFn = print();
printFn(df);

// Check that console.log was called
expect(consoleSpy).toHaveBeenCalled();

// Get the argument passed to console.log
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains column headers
expect(output).toContain('name');
expect(output).toContain('age');
expect(output).toContain('city');

// Check that the output contains data
expect(output).toContain('Alice');
expect(output).toContain('25');
expect(output).toContain('New York');

// Restore console.log
consoleSpy.mockRestore();
});

it('should return the frame for method chaining', () => {
// Mock console.log to prevent output
const consoleSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {});

// Call print function and check the return value
const printFn = print();
const result = printFn(df);

// Check that the function returns the frame
expect(result).toBe(df);

// Restore console.log
consoleSpy.mockRestore();
});

it('should respect rows limit', () => {
// Create a frame with many rows
const largeData = Array.from({ length: 20 }, (_, i) => ({
id: i,
value: i * 10,
}));

const largeDf = createDataFrameWithStorage(
DataFrame,
largeData,
storageType,
);

// Mock console.log
const consoleSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {});

// Call print function with row limit
const printFn = print();
printFn(largeDf, 5);

// Get the output
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains message about additional rows
expect(output).toContain('more rows');

// Restore console.log
consoleSpy.mockRestore();
});

it('should respect cols limit', () => {
// Create a frame with many columns
const wideData = {
col1: [1, 2, 3],
col2: [4, 5, 6],
col3: [7, 8, 9],
col4: [10, 11, 12],
col5: [13, 14, 15],
};

const wideDf = createDataFrameWithStorage(
DataFrame,
wideData,
storageType,
);

// Mock console.log
const consoleSpy = vi
.spyOn(console, 'log')
.mockImplementation(() => {});

// Call print function with column limit
const printFn = print();
printFn(wideDf, Infinity, 2);

// Get the output
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains message about additional columns
expect(output).toContain('more columns');

// Restore console.log
consoleSpy.mockRestore();
});
describe('with standard storage', () => {
// Create test data frame with people data for better readability in tests
const testData = [
{ name: 'Alice', age: 25, city: 'New York' },
{ name: 'Bob', age: 30, city: 'Boston' },
{ name: 'Charlie', age: 35, city: 'Chicago' },
{ name: 'David', age: 40, city: 'Denver' },
{ name: 'Eve', age: 45, city: 'El Paso' },
];

// Create DataFrame using fromRows
const df = DataFrame.fromRows(testData);

it('should format data as a table string', () => {
// Mock console.log to check output
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// Call print function directly
const printFn = print();
printFn(df);

// Check that console.log was called
expect(consoleSpy).toHaveBeenCalled();

// Get the argument passed to console.log
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains column headers
expect(output).toContain('name');
expect(output).toContain('age');
expect(output).toContain('city');

// Check that the output contains data
expect(output).toContain('Alice');
expect(output).toContain('25');
expect(output).toContain('New York');

// Restore console.log
consoleSpy.mockRestore();
});

it('should return the frame for method chaining', () => {
// Mock console.log to prevent output
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// Call print function and check the return value
const printFn = print();
const result = printFn(df);

// Check that the function returns the frame
expect(result).toBe(df);

// Restore console.log
consoleSpy.mockRestore();
});

it('should respect rows limit', () => {
// Create a frame with many rows
const largeData = Array.from({ length: 20 }, (_, i) => ({
id: i,
value: i * 10,
}));

const largeDf = DataFrame.fromRows(largeData);

// Mock console.log
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// Call print function with row limit
const printFn = print();
printFn(largeDf, 5);

// Get the output
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains message about additional rows
expect(output).toContain('more rows');

// Restore console.log
consoleSpy.mockRestore();
});

it('should respect cols limit', () => {
// Create a frame with many columns
const wideData = {
col1: [1, 2, 3],
col2: [4, 5, 6],
col3: [7, 8, 9],
col4: [10, 11, 12],
col5: [13, 14, 15],
};

const wideDf = DataFrame.fromRows([
{ col1: 1, col2: 4, col3: 7, col4: 10, col5: 13 },
{ col1: 2, col2: 5, col3: 8, col4: 11, col5: 14 },
{ col1: 3, col2: 6, col3: 9, col4: 12, col5: 15 },
]);

// Mock console.log
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});

// Call print function with column limit
const printFn = print();
printFn(wideDf, Infinity, 2);

// Get the output
const output = consoleSpy.mock.calls[0][0];

// Check that the output contains message about additional columns
expect(output).toContain('more columns');

// Restore console.log
consoleSpy.mockRestore();
});
});
});
Loading
Loading