Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jul 29, 2021
1 parent 392d815 commit 43ea0b8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 25 deletions.
2 changes: 1 addition & 1 deletion ui/src/app/admin/hoc/RoleProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRole } from '../hooks';

export function RoleProvider({ id, children }) {

const [role, setRole] = React.useState();
const [role, setRole] = React.useState({id: 'foo'});
const { get, response } = useRole(id);

async function loadRole() {
Expand Down
35 changes: 35 additions & 0 deletions ui/src/app/admin/hoc/RoleProvider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { render, screen } from "@testing-library/react";
import { act } from 'react-dom/test-utils';
import React from 'react';
import {RoleProvider} from './RoleProvider';

import {useRole} from '../hooks';

jest.mock('../hooks');

describe('RoleProvider component', () => {

beforeEach(() => {
useRole.mockImplementation(() => {
return {
get: jest.fn().mockResolvedValue({ id: 'foo' }),
response: {
ok: false,
status: 200
}
};
});
})

test('should provide the role context', async () => {
act(() => {
render(<RoleProvider id={'foo'}>
{(role) => <div>{role?.id}</div>}
</RoleProvider>);
});

expect(useRole).toHaveBeenCalled();
expect(screen.getByText('foo')).toBeInTheDocument();
});

})
43 changes: 43 additions & 0 deletions ui/src/app/admin/hoc/RolesProvider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { render, screen } from "@testing-library/react";
import { act } from 'react-dom/test-utils';
import React from 'react';
import { RolesProvider } from './RolesProvider';

import { useRoles } from '../hooks';
import { useNotificationDispatcher } from "../../notifications/hoc/Notifications";

jest.mock('../hooks');

jest.mock('../../notifications/hoc/Notifications');

describe('RolesProvider component', () => {

beforeEach(() => {

useNotificationDispatcher.mockImplementation(() => {
return {};
});

useRoles.mockImplementation(() => {
return {
get: jest.fn().mockResolvedValue([]),
response: {
ok: false,
status: 200
}
};
});
})

test('should provide the roles context', async () => {
act(() => {
render(<RolesProvider>
{(roles) => <div>{roles.length}</div>}
</RolesProvider>);
});

expect(useRoles).toHaveBeenCalled();
expect(screen.getByText('0')).toBeInTheDocument();
});

})
6 changes: 1 addition & 5 deletions ui/src/app/admin/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,5 @@ export function useRole(id) {
}

export function useRoleUiSchema() {
return {
description: {
'ui:widget': 'textarea'
}
};
return {};
}
55 changes: 55 additions & 0 deletions ui/src/app/admin/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
useRoles,
useRole,
} from './hooks';

import useFetch from 'use-http';
import API_BASE_PATH from '../App.constant';

jest.mock('use-http');

describe('api hooks', () => {

let mockPut;
let mockGet;

beforeEach(() => {

mockPut = jest.fn().mockResolvedValue({ response: { ok: true } });
mockGet = jest.fn().mockResolvedValue({ response: { ok: true } });

useFetch.mockImplementation(() => {
return {
request: {
ok: true
},
put: mockPut,
get: mockGet,
error: null,
response: {
status: 409
}
};
});
})

describe('useRoles', () => {
it('should call useFetch', () => {
const opts = {};
const roles = useRoles(opts);

expect(useFetch).toHaveBeenCalledWith(`${API_BASE_PATH}/admin/roles`, opts)
});
});

describe('useRole', () => {
it('should call useFetch', () => {
const opts = {
cachePolicy: 'no-cache'
};
const role = useRole('foo');

expect(useFetch).toHaveBeenCalledWith(`${API_BASE_PATH}/admin/roles/foo`, opts)
});
});
});
17 changes: 1 addition & 16 deletions ui/src/app/metadata/hooks/schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { useIsAdmin } from '../../core/user/UserContext';

export const fillInRootProperties = (keys, ui) => keys.reduce((sch, key, idx) => {
if (!sch.hasOwnProperty(key)) {
Expand Down Expand Up @@ -31,21 +30,7 @@ export function useUiSchema(definition, schema, current, locked = true) {
}
), [mapped, step.locked, locked]);

const isAdmin = useIsAdmin();

const hideEnableFromNonAdmins = React.useMemo(() => {
if (!isAdmin) {
return {
...isLocked,
serviceEnabled: {
'ui:widget': 'hidden'
}
};
}
return isLocked;
}, [isAdmin, isLocked]);

return { uiSchema: hideEnableFromNonAdmins, step};
return { uiSchema: isLocked, step};
}


Expand Down
2 changes: 1 addition & 1 deletion ui/src/testing/sourceSchema.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions ui/src/testing/uiSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const schema = {
"fields": [
"serviceProviderName",
"entityId",
"serviceEnabled",
"organization"
]
},
Expand Down Expand Up @@ -208,7 +207,6 @@ const schema = {
},
"serviceProviderName": {},
"entityId": {},
"serviceEnabled": {},
"organization": {},
"ui:disabled": false
};
Expand Down

0 comments on commit 43ea0b8

Please sign in to comment.