|
| 1 | +/* eslint-disable spellcheck/spell-checker */ |
| 2 | +import { |
| 3 | + afterEach, beforeEach, describe, expect, it, jest, |
| 4 | +} from '@jest/globals'; |
| 5 | +import $ from '@js/core/renderer'; |
| 6 | +import CardView from '@ts/grids/new/card_view/widget'; |
| 7 | +import type { Options as GridCoreOptions } from '@ts/grids/new/grid_core/options'; |
| 8 | +import { throwError } from '@ts/grids/new/grid_core/options_validation/utils'; |
| 9 | +import { rerender } from 'inferno'; |
| 10 | + |
| 11 | +const SELECTORS = { |
| 12 | + cardView: '.dx-cardview', |
| 13 | + addButton: '[aria-label="add"]', |
| 14 | + editButton: '[aria-label="edit"]', |
| 15 | + deleteButton: '[aria-label="remove"]', |
| 16 | +}; |
| 17 | + |
| 18 | +const setup = (options: GridCoreOptions = {}): CardView => { |
| 19 | + const container = document.createElement('div'); |
| 20 | + const { body } = document; |
| 21 | + body.append(container); |
| 22 | + |
| 23 | + const cardView = new CardView(container, options); |
| 24 | + rerender(); |
| 25 | + return cardView; |
| 26 | +}; |
| 27 | + |
| 28 | +const getAddButton = (): Element | null => document.querySelector(SELECTORS.addButton); |
| 29 | + |
| 30 | +const getEditButton = (): Element | null => document.querySelector(SELECTORS.editButton); |
| 31 | + |
| 32 | +const getDeleteButton = (): Element | null => document.querySelector(SELECTORS.deleteButton); |
| 33 | + |
| 34 | +const checkError = (): void => expect(throwError).toHaveBeenCalledWith('E1042', 'CardView'); |
| 35 | + |
| 36 | +jest.mock('@ts/grids/new/grid_core/options_validation/utils', () => ({ |
| 37 | + throwError: jest.fn().mockImplementation(() => ({})), |
| 38 | +})); |
| 39 | + |
| 40 | +describe('editing validation', () => { |
| 41 | + afterEach(() => { |
| 42 | + const cardView = document.querySelector(SELECTORS.cardView); |
| 43 | + // @ts-expect-error bad typed renderer |
| 44 | + $(cardView ?? undefined as any)?.dxCardView('dispose'); |
| 45 | + document.body.innerHTML = ''; |
| 46 | + }); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + jest.clearAllMocks(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw error when no keyExpr and clicking on add', () => { |
| 53 | + setup({ |
| 54 | + dataSource: [{ value: 'test1' }], |
| 55 | + editing: { allowAdding: true }, |
| 56 | + }); |
| 57 | + |
| 58 | + const addButton = getAddButton(); |
| 59 | + addButton?.dispatchEvent(new MouseEvent('click')); |
| 60 | + |
| 61 | + checkError(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should throw error when no keyExpr and clicking on edit', () => { |
| 65 | + setup({ |
| 66 | + dataSource: [{ value: 'test1' }], |
| 67 | + editing: { allowUpdating: true }, |
| 68 | + }); |
| 69 | + const editButton = getEditButton(); |
| 70 | + editButton?.dispatchEvent(new MouseEvent('click')); |
| 71 | + |
| 72 | + checkError(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw error when no keyExpr and clicking on delete', () => { |
| 76 | + setup({ |
| 77 | + dataSource: [{ value: 'test1' }], |
| 78 | + editing: { allowDeleting: true }, |
| 79 | + }); |
| 80 | + const deleteButton = getDeleteButton(); |
| 81 | + deleteButton?.dispatchEvent(new MouseEvent('click')); |
| 82 | + |
| 83 | + checkError(); |
| 84 | + }); |
| 85 | +}); |
0 commit comments