import type { Store } from '../state/createStore' import { createStore } from '../state/createStore' import type { Emitter } from '../events' import { createEmitter } from '../events' import type { GridEditApi } from './edit' import { createEditApi } from './edit' import { findNextEditable } from '../utils/find-next-editable' jest.mock('../utils/find-next-editable') describe('edit api', () => { let store: Store, events: Emitter, api: GridEditApi beforeEach(() => { store = createStore() store.dispatch = jest.fn() events = createEmitter() events.emit = jest.fn() api = createEditApi(store, events) }) it('should dispatch updateEdit when calling start', () => { api.start({ rowId: '1', columnId: 'firstName' }) expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: { rowId: '1', columnId: 'firstName' }, }) }) it('should dispatch updateEdit when calling disable', () => { api.stop() expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: null, }) }) describe('next', () => { describe('when it cannot find another cell to edit', () => { it('should stop editing', () => { jest.mocked(findNextEditable).mockReturnValue(null) api.next() expect(findNextEditable).toHaveBeenCalledWith(store, true) expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: null, }) }) }) describe('when it finds another cell to edit', () => { beforeEach(() => { jest.mocked(findNextEditable).mockReturnValue({ columnId: 'firstName', rowId: '1', }) api.next() }) it('should update the edit', () => { expect(findNextEditable).toHaveBeenCalledWith(store, true) expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: { rowId: '1', columnId: 'firstName' }, }) }) }) }) describe('previous', () => { describe('when it cannot find another cell to edit', () => { it('should stop editing', () => { jest.mocked(findNextEditable).mockReturnValue(null) api.previous() expect(findNextEditable).toHaveBeenCalledWith(store, false) expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: null, }) }) }) describe('when it finds another cell to edit', () => { beforeEach(() => { jest.mocked(findNextEditable).mockReturnValue({ columnId: 'firstName', rowId: '1', }) api.previous() }) it('should update the edit', () => { expect(findNextEditable).toHaveBeenCalledWith(store, false) expect(store.dispatch).toHaveBeenCalledWith({ type: 'updateEdit', payload: { rowId: '1', columnId: 'firstName' }, }) }) }) }) })