Skip to content

Commit

Permalink
AdminGuesserProps - add admin prop type
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelSuwinski committed May 22, 2024
1 parent 0cdf153 commit 678b33e
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/AdminGuesser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from 'react';
import { AdminUI, AuthContext } from 'react-admin';
import type { AdminProps, AuthProvider } from 'react-admin';
import ReactTestRenderer from 'react-test-renderer/shallow';
import { AdminResourcesGuesser } from './AdminGuesser.js';
import AdminGuesser, { AdminResourcesGuesser } from './AdminGuesser.js';
import ResourceGuesser from './ResourceGuesser.js';
import schemaAnalyzer from './hydra/schemaAnalyzer.js';
import resources from './__fixtures__/resources.js';
import { API_DATA } from './__fixtures__/parsedData.js';
import type {
Expand Down Expand Up @@ -83,4 +86,36 @@ describe('<AdminGuesser />', () => {

expect(renderer.getRenderOutput()).toMatchSnapshot();
});

test('renders with admin element', () => {
const authProvider: AuthProvider = {
getPermissions: () => Promise.resolve(['user']),
getIdentity: () =>
Promise.resolve({
id: '/users/2',
fullName: 'Test User',
avatar: undefined,
}),
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
checkAuth: () => Promise.resolve(),
checkError: () => Promise.resolve(),
};

const AdminEl = (props: AdminProps) => (
<AuthContext.Provider value={authProvider}>
<AdminUI {...props} />
</AuthContext.Provider>
);

renderer.render(
<AdminGuesser
admin={AdminEl}
dataProvider={dataProvider}
schemaAnalyzer={schemaAnalyzer()}
/>,
);

expect(renderer.getRenderOutput()).toMatchSnapshot();
});
});
2 changes: 2 additions & 0 deletions src/AdminGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import useDisplayOverrideCode from './useDisplayOverrideCode.js';
import type { ApiPlatformAdminDataProvider, SchemaAnalyzer } from './types.js';

export interface AdminGuesserProps extends AdminProps {
admin?: ComponentType<AdminProps>;
dataProvider: ApiPlatformAdminDataProvider;
schemaAnalyzer: SchemaAnalyzer;
includeDeprecated?: boolean;
Expand Down Expand Up @@ -214,6 +215,7 @@ AdminGuesser.propTypes = {
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
theme: PropTypes.object,
includeDeprecated: PropTypes.bool,
admin: PropTypes.elementType,
};
/* eslint-enable tree-shaking/no-side-effects-in-initialization */

Expand Down
125 changes: 125 additions & 0 deletions src/__snapshots__/AdminGuesser.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,131 @@

exports[`<AdminGuesser /> renders loading 1`] = `<Loading />`;

exports[`<AdminGuesser /> renders with admin element 1`] = `
<ErrorBoundary
fallbackRender={[Function]}
onError={[Function]}
>
<AdminGuesser
admin={[Function]}
dataProvider={
{
"create": [Function],
"delete": [Function],
"deleteMany": [Function],
"getList": [Function],
"getMany": [Function],
"getManyReference": [Function],
"getOne": [Function],
"introspect": [Function],
"subscribe": [Function],
"unsubscribe": [Function],
"update": [Function],
"updateMany": [Function],
}
}
i18nProvider={
{
"changeLocale": [Function],
"getLocale": [Function],
"getLocales": [Function],
"translate": [Function],
}
}
schemaAnalyzer={
{
"getFieldNameFromSchema": [Function],
"getFieldType": [Function],
"getFiltersParametersFromSchema": [Function],
"getOrderParametersFromSchema": [Function],
"getSubmissionErrors": [Function],
}
}
theme={
{
"components": {
"MuiAutocomplete": {
"variants": [
{
"props": {},
"style": [Function],
},
],
},
"MuiFilledInput": {
"styleOverrides": {
"root": {
"&$disabled": {
"backgroundColor": "rgba(0, 0, 0, 0.04)",
},
"backgroundColor": "rgba(0, 0, 0, 0.04)",
},
},
},
"MuiFormControl": {
"defaultProps": {
"margin": "dense",
"size": "small",
"variant": "filled",
},
},
"MuiTextField": {
"defaultProps": {
"margin": "dense",
"size": "small",
"variant": "filled",
},
"variants": [
{
"props": {},
"style": [Function],
},
],
},
"RaMenuItemLink": {
"styleOverrides": {
"root": {
"&.RaMenuItemLink-active": {
"borderLeft": "3px solid #38a9b4",
},
"borderLeft": "3px solid #fff",
},
},
},
},
"palette": {
"background": {
"default": "#fafafb",
},
"mode": "light",
"primary": {
"contrastText": "#ffffff",
"dark": "#006a75",
"light": "#74dde7",
"main": "#38a9b4",
},
"secondary": {
"contrastText": "#fff",
"dark": "#0069c0",
"light": "#6ec6ff",
"main": "#288690",
},
},
"sidebar": {
"closedWidth": 50,
"width": 240,
},
"typography": {
"h6": {
"fontWeight": 400,
},
},
}
}
/>
</ErrorBoundary>
`;

exports[`<AdminGuesser /> renders with custom resources 1`] = `
<AdminUI
dataProvider={
Expand Down

0 comments on commit 678b33e

Please sign in to comment.