import React from 'react';
import {fireEvent, render, waitFor} from '@testing-library/react';
import {DCFCell} from 'paddy-react-edit-list';
//import {schema, data, onLoad} from './data';
describe('base', () => {
it('basic use', async () => {
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
//expect(onLoadFn).toBeCalledTimes(1);
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
/*
keeping this existing code here for examples
it('basic use', async () => {
const onLoadFn = jest.fn(onLoad);
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(1);
expect(r.container.innerHTML).toMatchSnapshot();
r.rerender(
product
}}
thClassName={{
product: 'col-4',
type: 'col-3',
price: 'col-2',
stock: 'col-2',
buttons: 'col-1'
}}
tdClassName={{
product: 'product',
type: 'type',
price: 'price',
stock: 'stock',
buttons: 'buttons'
}}
btnValidateClassName='btn btn-success p-0 m-0'
btnDeleteClassName='btn btn-danger py-0 px-1 m-0 mx-1'
btnCancelClassName='btn btn-secondary py-0 px-1 m-0 mx-1'
/>
);
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(2);
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('single custom classes', async () => {
const onLoadFn = jest.fn(onLoad);
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(1);
expect(r.container.innerHTML).toMatchSnapshot();
r.rerender(
);
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(2);
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('sync loader', async () => {
const onLoadFn = jest.fn(() => data);
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(1);
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('no headers', async () => {
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('custom button elements', async () => {
const r = render(
YES!}
btnCancelElement={}
btnDeleteElement={}
filler={...}
/>
);
await waitFor(() => expect(r.getByText('Desk')));
expect(r.container.innerHTML).toMatchSnapshot();
fireEvent((await r.findAllByText('...'))[0], new MouseEvent('click', {bubbles: true}));
await waitFor(() => expect(r.container.querySelectorAll('input').length).toBe(3));
expect(r.container.innerHTML).toMatchSnapshot();
fireEvent(await r.findByText('YES!'), new MouseEvent('click', {bubbles: true}));
await waitFor(() => expect(r.container.querySelectorAll('input').length).toBe(0));
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('custom grid elements', async () => {
const r = render(
);
await waitFor(() => expect(r.getByText('Desk')));
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
it('reference forwarding', async () => {
const onLoadFn = jest.fn(onLoad);
const ref = React.createRef();
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(onLoadFn).toBeCalledTimes(1);
expect(r.container.innerHTML).toMatchSnapshot();
expect(ref.current?.tagName.toLowerCase()).toBe('table');
ref.current?.dispatchEvent(
new KeyboardEvent('keydown', {key: 'R', altKey: true, bubbles: true})
);
await waitFor(() => expect(onLoadFn).toBeCalledTimes(2));
r.unmount();
});
it('externally triggered refresh', async () => {
const onLoadFn = jest.fn(() => data);
const r = render();
await waitFor(() => expect(r.getByText('Desk')));
expect(r.container.innerHTML).toMatchSnapshot();
expect(onLoadFn).toBeCalledTimes(1);
await fireEvent.keyDown(r.container.querySelector('table')!, {
key: 'R',
altKey: true
});
await waitFor(() => expect(onLoadFn).toBeCalledTimes(2));
expect(r.getByText('Desk')).toBeInTheDocument();
expect(r.container.innerHTML).toMatchSnapshot();
r.unmount();
});
describe('errors', () => {
let err;
beforeEach(() => {
// eslint-disable-next-line no-console
err = console.error;
// eslint-disable-next-line no-console
console.error = () => undefined;
});
afterEach(() => {
// eslint-disable-next-line no-console
console.error = err;
});
it('no schema', () => {
expect(() =>
render(
)
).toThrowError('schema prop is missing');
});
it('no onLoad', () => {
expect(() => {
render( []} />);
}).toThrowError('onLoad callback is missing');
});
it('custom type w/o formatter', async () => {
expect(() => {
const customSchema = [...schema];
const field = customSchema.findIndex((e) => e.name === 'product');
customSchema[field] = {...customSchema[field]};
customSchema[field].type = 'custom';
const r = render();
}).toThrowError('Field product:custom has no formatter defined');
});
});
*/
});