diff --git a/README.md b/README.md index 732fc5b..439cb23 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/test/tableControl.test.ts b/test/tableControl.test.ts new file mode 100644 index 0000000..662b2cd --- /dev/null +++ b/test/tableControl.test.ts @@ -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', + }) + }) +})