Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdc committed Mar 23, 2024
1 parent 5f07705 commit 58e85fc
Show file tree
Hide file tree
Showing 33 changed files with 1,480 additions and 1,303 deletions.
330 changes: 165 additions & 165 deletions __tests__/unit/hooks/useAdmin.test.tsx
Original file line number Diff line number Diff line change
@@ -1,168 +1,168 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react';

import * as t from 'io-ts';
import App from 'next/app';
import { Button, Col } from 'react-bootstrap';
import useAdmin, from '../../../hooks/useadmin';
import Admin from '../../../pages/admin';

// These are tests to make sure that the base schema resolvers work in conjunction with the form stuff

const fooWithIdSchema = t.type({ id: t.number, foo: t.string });
type FooWithID = t.TypeOf<typeof fooWithIdSchema>;
type FormFields = t.TypeOf<typeof schema>;

const UseAdminTestForm = (): JSX.Element => {
const {
// data,
// setData,
selected,
// setSelected,
showRenameModal,
setShowRenameModal,
error,
setError,
deleteResults,
setDeleteResults,
renameCallback,
nameExists,
form,
formSubmit,
mainField,
deleteButton,
isSuperAdmin,
isValid,
} = useAdmin(
'Foo',
'0',
() => Promise.resolve({ id: 0, foo: 'foo' } as FooWithID),
(f: FormFields, k: string, id: number) => ({ f: f, k: k, id: id }),
{
keyProp: 'foo',
delEndpoint: '',
upsertEndpoint: '',
nameExistsEndpoint: (s: string) => `${s}`,
},
schema,
() => Promise.resolve({} as FormFields),
false,
);

return (
<Admin
type="Taxonomy"
keyField="name"
editName={{ getDefault: () => selected?.foo, renameCallback: renameCallback, nameExistsCallback: nameExists }}
setShowModal={setShowRenameModal}
showModal={showRenameModal}
setError={setError}
error={error}
setDeleteResults={setDeleteResults}
deleteResults={deleteResults}
selected={selected}
>
<form onSubmit={form.handleSubmit(formSubmit)}>
{mainField('name', 'Family', { searchEndpoint: (s) => `/api/taxonomy/family?q=${s}` })}

{selected && (
<Col>
<Button variant="secondary" size="sm" className="button" onClick={() => setShowRenameModal(true)}>
Rename
</Button>
</Col>
)}
<Button variant="primary" type="submit" value="Save Changes" disabled={!selected || !isValid}>
Save Changes
</Button>
{isSuperAdmin
? deleteButton(
'Caution. If there are any species (galls or hosts) assigned to this Family they too will be PERMANENTLY deleted.',
)
: 'If you need to delete a Family please contact Adam or Jeff on Slack/Discord.'}
</form>
</Admin>
);
};

// Tests begin...
test('base schema validation works', () => {
expect(true).toBeTruthy();
});

// it('should display required error when value is invalid', async () => {
// render(<UseAdminTestForm />);

// fireEvent.submit(screen.getByRole('button'));

// expect(await screen.findAllByRole('alert')).toHaveLength(2);
// expect(mockLogin).not.toBeCalled();
// });

// it('should display matching error when email is invalid', async () => {
// render(<App login={mockLogin} />);

// fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// target: {
// value: 'test',
// },
// });

// fireEvent.input(screen.getByLabelText('password'), {
// target: {
// value: 'password',
// },
// });

// fireEvent.submit(screen.getByRole('button'));

// expect(await screen.findAllByRole('alert')).toHaveLength(1);
// expect(mockLogin).not.toBeCalled();
// expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('test');
// expect(screen.getByLabelText('password')).toHaveValue('password');
// });

// it('should display min length error when password is invalid', async () => {
// render(<App login={mockLogin} />);

// fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// target: {
// value: '[email protected]',
// import { fireEvent, render, screen, waitFor } from '@testing-library/react';

// import * as t from 'io-ts';
// import App from 'next/app';
// import { Button, Col } from 'react-bootstrap';
// import useAdmin, from '../../../hooks/useadmin';
// import Admin from '../../../pages/admin';

// // These are tests to make sure that the base schema resolvers work in conjunction with the form stuff

// const fooWithIdSchema = t.type({ id: t.number, foo: t.string });
// type FooWithID = t.TypeOf<typeof fooWithIdSchema>;
// type FormFields = t.TypeOf<typeof schema>;

// const UseAdminTestForm = (): JSX.Element => {
// const {
// // data,
// // setData,
// selected,
// // setSelected,
// showRenameModal,
// setShowRenameModal,
// error,
// setError,
// deleteResults,
// setDeleteResults,
// renameCallback,
// nameExists,
// form,
// formSubmit,
// mainField,
// deleteButton,
// isSuperAdmin,
// isValid,
// } = useAdmin(
// 'Foo',
// '0',
// () => Promise.resolve({ id: 0, foo: 'foo' } as FooWithID),
// (f: FormFields, k: string, id: number) => ({ f: f, k: k, id: id }),
// {
// keyProp: 'foo',
// delEndpoint: '',
// upsertEndpoint: '',
// nameExistsEndpoint: (s: string) => `${s}`,
// },
// });

// fireEvent.input(screen.getByLabelText('password'), {
// target: {
// value: 'pass',
// },
// });

// fireEvent.submit(screen.getByRole('button'));

// expect(await screen.findAllByRole('alert')).toHaveLength(1);
// expect(mockLogin).not.toBeCalled();
// expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('[email protected]');
// expect(screen.getByLabelText('password')).toHaveValue('pass');
// schema,
// () => Promise.resolve({} as FormFields),
// false,
// );

// return (
// <Admin
// type="Taxonomy"
// keyField="name"
// editName={{ getDefault: () => selected?.foo, renameCallback: renameCallback, nameExistsCallback: nameExists }}
// setShowModal={setShowRenameModal}
// showModal={showRenameModal}
// setError={setError}
// error={error}
// setDeleteResults={setDeleteResults}
// deleteResults={deleteResults}
// selected={selected}
// >
// <form onSubmit={form.handleSubmit(formSubmit)}>
// {mainField('name', 'Family', { searchEndpoint: (s) => `/api/taxonomy/family?q=${s}` })}

// {selected && (
// <Col>
// <Button variant="secondary" size="sm" className="button" onClick={() => setShowRenameModal(true)}>
// Rename
// </Button>
// </Col>
// )}
// <Button variant="primary" type="submit" value="Save Changes" disabled={!selected || !isValid}>
// Save Changes
// </Button>
// {isSuperAdmin
// ? deleteButton(
// 'Caution. If there are any species (galls or hosts) assigned to this Family they too will be PERMANENTLY deleted.',
// )
// : 'If you need to delete a Family please contact Adam or Jeff on Slack/Discord.'}
// </form>
// </Admin>
// );
// };

// // Tests begin...
// test('base schema validation works', () => {
// expect(true).toBeTruthy();
// });

// it('should not display error when value is valid', async () => {
// render(<App login={mockLogin} />);

// fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// target: {
// value: '[email protected]',
// },
// });

// fireEvent.input(screen.getByLabelText('password'), {
// target: {
// value: 'password',
// },
// });

// fireEvent.submit(screen.getByRole('button'));

// await waitFor(() => expect(screen.queryAllByRole('alert')).toHaveLength(0));
// expect(mockLogin).toBeCalledWith('[email protected]', 'password');
// expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('');
// expect(screen.getByLabelText('password')).toHaveValue('');
// });
// // it('should display required error when value is invalid', async () => {
// // render(<UseAdminTestForm />);

// // fireEvent.submit(screen.getByRole('button'));

// // expect(await screen.findAllByRole('alert')).toHaveLength(2);
// // expect(mockLogin).not.toBeCalled();
// // });

// // it('should display matching error when email is invalid', async () => {
// // render(<App login={mockLogin} />);

// // fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// // target: {
// // value: 'test',
// // },
// // });

// // fireEvent.input(screen.getByLabelText('password'), {
// // target: {
// // value: 'password',
// // },
// // });

// // fireEvent.submit(screen.getByRole('button'));

// // expect(await screen.findAllByRole('alert')).toHaveLength(1);
// // expect(mockLogin).not.toBeCalled();
// // expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('test');
// // expect(screen.getByLabelText('password')).toHaveValue('password');
// // });

// // it('should display min length error when password is invalid', async () => {
// // render(<App login={mockLogin} />);

// // fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// // target: {
// // value: '[email protected]',
// // },
// // });

// // fireEvent.input(screen.getByLabelText('password'), {
// // target: {
// // value: 'pass',
// // },
// // });

// // fireEvent.submit(screen.getByRole('button'));

// // expect(await screen.findAllByRole('alert')).toHaveLength(1);
// // expect(mockLogin).not.toBeCalled();
// // expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('[email protected]');
// // expect(screen.getByLabelText('password')).toHaveValue('pass');
// // });

// // it('should not display error when value is valid', async () => {
// // render(<App login={mockLogin} />);

// // fireEvent.input(screen.getByRole('textbox', { name: /email/i }), {
// // target: {
// // value: '[email protected]',
// // },
// // });

// // fireEvent.input(screen.getByLabelText('password'), {
// // target: {
// // value: 'password',
// // },
// // });

// // fireEvent.submit(screen.getByRole('button'));

// // await waitFor(() => expect(screen.queryAllByRole('alert')).toHaveLength(0));
// // expect(mockLogin).toBeCalledWith('[email protected]', 'password');
// // expect(screen.getByRole('textbox', { name: /email/i })).toHaveValue('');
// // expect(screen.getByLabelText('password')).toHaveValue('');
// // });
26 changes: 0 additions & 26 deletions __tests__/unit/utils/io-ts.test.ts

This file was deleted.

32 changes: 17 additions & 15 deletions components/EditableDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,23 @@ const EditableTable = <T extends WithID>(props: EditableTableProps<T>): JSX.Elem

const confirm = useConfirmation();

const deleteSelected = () => {
return confirm({
variant: 'danger',
catchOnCancel: true,
title: 'Are you sure want to delete?',
message:
props.deleteConfirmation ?? `This will delete the current row and all associated data. Do you want to continue?`,
})
.then(() => {
const d = props.data.filter((a) => !selected.has(a.id));
setSelected(new Set());
setToggleCleared(!toggleCleared);
props.update(d);
})
.catch(() => Promise.resolve());
const deleteSelected = async () => {
try {
await confirm({
variant: 'danger',
catchOnCancel: true,
title: 'Are you sure want to delete?',
message:
props.deleteConfirmation ??
`This will delete the current row and all associated data. Do you want to continue?`,
});
const d = props.data.filter((a) => !selected.has(a.id));
setSelected(new Set());
setToggleCleared(!toggleCleared);
props.update(d);
} catch {
return await Promise.resolve();
}
};

const add = () => {
Expand Down
Loading

0 comments on commit 58e85fc

Please sign in to comment.