Skip to content

Commit

Permalink
Table control tests
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Dec 23, 2024
1 parent 51de4fb commit 7d3670b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![minzipped](https://img.shields.io/bundlephobia/minzip/hightable)](https://www.npmjs.com/package/hightable)
[![workflow status](https://github.com/hyparam/hightable/actions/workflows/ci.yml/badge.svg)](https://github.com/hyparam/hightable/actions)
[![mit license](https://img.shields.io/badge/License-MIT-orange.svg)](https://opensource.org/licenses/MIT)
![coverage](https://img.shields.io/badge/Coverage-88-darkred)
![coverage](https://img.shields.io/badge/Coverage-93-darkred)
[![dependencies](https://img.shields.io/badge/Dependencies-0-blueviolet)](https://www.npmjs.com/package/hightable?activeTab=dependencies)

HighTable is a virtualized table component for React, designed to efficiently display large datasets in the browser. It loads and renders only the rows necessary for the current viewport, enabling smooth scrolling and performance even with millions of rows. HighTable supports asynchronous data fetching, dynamic loading, and optional column sorting.
Expand Down
55 changes: 55 additions & 0 deletions test/tableControl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it, vi } from 'vitest'
import { createTableControl } from '../src/HighTable.js'

describe('createTableControl', () => {
it('returns an object with a publisher and setOrderBy function', () => {
const tableControl = createTableControl()
const subscriber = vi.fn()
expect(tableControl).toHaveProperty('publisher')
expect(tableControl).toHaveProperty('setOrderBy')
expect(typeof tableControl.publisher.subscribe).toBe('function')
expect(typeof tableControl.publisher.unsubscribe).toBe('function')
expect(typeof tableControl.setOrderBy).toBe('function')
})

it('publishes SET_ORDER action with correct orderBy when setOrderBy is called', () => {
const tableControl = createTableControl()
const subscriber = vi.fn()
tableControl.publisher.subscribe(subscriber)
tableControl.setOrderBy('name')

expect(subscriber).toHaveBeenCalledTimes(1)
expect(subscriber).toHaveBeenCalledWith({
type: 'SET_ORDER',
orderBy: 'name',
})
})

it('does not call unsubscribed subscriber', () => {
const tableControl = createTableControl()
const subscriber = vi.fn()
tableControl.publisher.subscribe(subscriber)
tableControl.publisher.unsubscribe(subscriber)
tableControl.setOrderBy('price')

expect(subscriber).not.toHaveBeenCalled()
})

it('allows multiple subscribers to receive the published action', () => {
const tableControl = createTableControl()
const subscriber = vi.fn()
const anotherSubscriber = vi.fn()
tableControl.publisher.subscribe(subscriber)
tableControl.publisher.subscribe(anotherSubscriber)
tableControl.setOrderBy('quantity')

expect(subscriber).toHaveBeenCalledWith({
type: 'SET_ORDER',
orderBy: 'quantity',
})
expect(anotherSubscriber).toHaveBeenCalledWith({
type: 'SET_ORDER',
orderBy: 'quantity',
})
})
})

0 comments on commit 7d3670b

Please sign in to comment.