|
1 | | -import { describe, it, expect, beforeEach } from 'vitest' |
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
2 | 2 | import { suppressKeyboardEvent, isEqual } from '../eventHandlers' |
3 | 3 |
|
4 | 4 | // Helper to build a basic ag-cell DOM structure |
@@ -55,6 +55,86 @@ describe('suppressKeyboardEvent', () => { |
55 | 55 | event = { code: 'Tab', srcElement: input1, shiftKey: false, key: 'Tab' } |
56 | 56 | expect(suppressKeyboardEvent({ event })).toBe(true) |
57 | 57 | }) |
| 58 | + |
| 59 | + it('triggers row navigation when Enter is pressed and keyboard navigation is enabled', () => { |
| 60 | + const cell = createCell() |
| 61 | + const event = { code: 'Enter', srcElement: cell } |
| 62 | + const mockOnRowClicked = vi.fn() |
| 63 | + const mockParams = { |
| 64 | + event, |
| 65 | + node: { id: 'test-row' }, |
| 66 | + data: { id: 1 } |
| 67 | + } |
| 68 | + |
| 69 | + const result = suppressKeyboardEvent(mockParams, { |
| 70 | + enableKeyboardRowNavigation: true, |
| 71 | + onRowClicked: mockOnRowClicked |
| 72 | + }) |
| 73 | + |
| 74 | + expect(result).toBe(true) // Should suppress default behavior |
| 75 | + expect(mockOnRowClicked).toHaveBeenCalledWith({ |
| 76 | + ...mockParams, |
| 77 | + event: expect.objectContaining({ |
| 78 | + target: cell, |
| 79 | + currentTarget: cell |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('does not trigger row navigation when Enter is pressed but keyboard navigation is disabled', () => { |
| 85 | + const cell = createCell() |
| 86 | + const event = { code: 'Enter', srcElement: cell } |
| 87 | + const mockOnRowClicked = vi.fn() |
| 88 | + const mockParams = { |
| 89 | + event, |
| 90 | + node: { id: 'test-row' }, |
| 91 | + data: { id: 1 } |
| 92 | + } |
| 93 | + |
| 94 | + const result = suppressKeyboardEvent(mockParams, { |
| 95 | + enableKeyboardRowNavigation: false, |
| 96 | + onRowClicked: mockOnRowClicked |
| 97 | + }) |
| 98 | + |
| 99 | + expect(result).toBe(false) // Should allow default behavior |
| 100 | + expect(mockOnRowClicked).not.toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it('does not trigger row navigation when Enter is pressed but no onRowClicked handler is provided', () => { |
| 104 | + const cell = createCell() |
| 105 | + const event = { code: 'Enter', srcElement: cell } |
| 106 | + const mockParams = { |
| 107 | + event, |
| 108 | + node: { id: 'test-row' }, |
| 109 | + data: { id: 1 } |
| 110 | + } |
| 111 | + |
| 112 | + const result = suppressKeyboardEvent(mockParams, { |
| 113 | + enableKeyboardRowNavigation: true, |
| 114 | + onRowClicked: null |
| 115 | + }) |
| 116 | + |
| 117 | + expect(result).toBe(false) // Should allow default behavior |
| 118 | + }) |
| 119 | + |
| 120 | + it('does not trigger row navigation when Enter is pressed but no node is provided', () => { |
| 121 | + const cell = createCell() |
| 122 | + const event = { code: 'Enter', srcElement: cell } |
| 123 | + const mockOnRowClicked = vi.fn() |
| 124 | + const mockParams = { |
| 125 | + event, |
| 126 | + node: null, |
| 127 | + data: { id: 1 } |
| 128 | + } |
| 129 | + |
| 130 | + const result = suppressKeyboardEvent(mockParams, { |
| 131 | + enableKeyboardRowNavigation: true, |
| 132 | + onRowClicked: mockOnRowClicked |
| 133 | + }) |
| 134 | + |
| 135 | + expect(result).toBe(false) // Should allow default behavior |
| 136 | + expect(mockOnRowClicked).not.toHaveBeenCalled() |
| 137 | + }) |
58 | 138 | }) |
59 | 139 |
|
60 | 140 | describe('isEqual utility', () => { |
|
0 commit comments