import type { Emitter } from '../events' import { createEmitter } from '../events' import type { Store } from '../state/createStore' import { createStore } from '../state/createStore' import type { GridScrollApi } from './scroll' import { createScrollApi } from './scroll' import type { GridState } from '../state' import { INITIAL_STATE } from '../state' const getStateMock = (state: Partial = {}): GridState => ({ ...INITIAL_STATE, columns: { ...INITIAL_STATE.columns, ids: ['First', 'Second'], }, rows: { ...INITIAL_STATE.rows, }, ...state, }) describe('scroll api', () => { let store: Store, api: GridScrollApi, events: Emitter beforeEach(() => { store = createStore() store.getState = jest.fn() store.dispatch = jest.fn() jest.spyOn(store.selectors, 'selectRowIds').mockReturnValue([ '101', '102', '103', ]) events = createEmitter() events.emit = jest.fn() api = createScrollApi(store, events) }) it('scrollToTop should dispatch event on call', () => { jest.mocked(store.getState).mockReturnValue( getStateMock({ currentFocus: { ...INITIAL_STATE.currentFocus, focus: { area: 'body', columnId: 'Second', rowId: '102', subFocus: 'first', }, }, }) ) api.scrollToTop() expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 0, null, 'always' ) }) it('scrollToTop should dispatch event on call (with column id)', () => { jest.mocked(store.getState).mockReturnValue( getStateMock({ currentFocus: { ...INITIAL_STATE.currentFocus, focus: { area: 'body', columnId: 'Second', rowId: '102', subFocus: 'first', }, }, }) ) api.scrollToTop('First') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 0, 0, 'always' ) }) it('scrollToTop should NOT dispatch event on call if no rows', () => { jest.mocked(store.getState).mockReturnValue(INITIAL_STATE) jest.mocked(store.selectors.selectRowIds).mockReturnValue([]) api.scrollToTop() expect(events.emit).toHaveBeenCalledTimes(0) }) it('scrollToBottom should dispatch event on call', () => { jest.mocked(store.getState).mockReturnValue( getStateMock({ currentFocus: { ...INITIAL_STATE.currentFocus, focus: { area: 'body', columnId: 'Second', rowId: '102', subFocus: 'first', }, }, }) ) api.scrollToBottom() expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 2, null, 'always' ) }) it('scrollToBottom should dispatch event on call (with column id)', () => { jest.mocked(store.getState).mockReturnValue( getStateMock({ currentFocus: { ...INITIAL_STATE.currentFocus, focus: { area: 'body', columnId: 'Second', rowId: '102', subFocus: 'first', }, }, }) ) api.scrollToBottom('First') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 2, 0, 'always' ) }) it('scrollToBottom should NOT dispatch event on call if no rows', () => { jest.mocked(store.getState).mockReturnValue(INITIAL_STATE) jest.mocked(store.selectors.selectRowIds).mockReturnValue([]) api.scrollToBottom() expect(events.emit).toHaveBeenCalledTimes(0) }) it('scrollTo should dispatch event on call (row only)', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo('102') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 1, null, 'always' ) }) it('scrollTo should dispatch event on call (column only)', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo(null, 'First') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', null, 0, 'always' ) }) it('scrollTo should not dispatch event on call of both rowId and columnId are null', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo(null, null) expect(events.emit).not.toHaveBeenCalled() }) it('scrollTo should dispatch event on call (with column id)', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo('102', 'First') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 1, 0, 'always' ) }) it('scrollTo should fallback to first column if not found', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo('102', 'not-there') expect(events.emit).toHaveBeenCalledWith( 'scrollToIndex', 1, 0, 'always' ) }) it('scrollTo should NOT dispatch event on call if index is not found', () => { jest.mocked(store.getState).mockReturnValue(getStateMock()) api.scrollTo('10245') expect(events.emit).toHaveBeenCalledTimes(0) }) it('scrollTo should NOT dispatch event on call if no rows', () => { jest.mocked(store.getState).mockReturnValue( getStateMock({ rows: { ...INITIAL_STATE.rows, }, }) ) api.scrollTo('10245') expect(events.emit).toHaveBeenCalledTimes(0) }) })