Skip to content

Commit

Permalink
fix(platformServices): sw-625 restore onNavigate
Browse files Browse the repository at this point in the history
* authenticationContext, restore onNavigation callback
* platformActions, restore onNavigation
* redux, platformTypes, restore dispatch type
* services, restore onNavigation
  • Loading branch information
cdcabrera committed Mar 20, 2023
1 parent fe94e57 commit c74f315
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 60 deletions.
4 changes: 4 additions & 0 deletions src/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,14 @@ Initialize an app, and return a combined state store that includes authorization
</tr><tr>
<td>options.hideGlobalFilter</td><td><code>function</code></td>
</tr><tr>
<td>options.onNavigation</td><td><code>function</code></td>
</tr><tr>
<td>options.useChrome</td><td><code>function</code></td>
</tr><tr>
<td>options.useDispatch</td><td><code>function</code></td>
</tr><tr>
<td>options.useNavigate</td><td><code>function</code></td>
</tr><tr>
<td>options.useSelectorsResponse</td><td><code>function</code></td>
</tr> </tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ exports[`Authentication Component should allow being disabled: disabled 1`] = `
<ContextProvider
value={
{
"errorCodes": undefined,
"errorStatus": null,
"locale": undefined,
"authorized": {
"subscriptions": true,
},
"errorCodes": [],
"errorStatus": undefined,
}
}
>
Expand All @@ -27,9 +29,9 @@ exports[`Authentication Component should render a basic component: basic 1`] = `
<ContextProvider
value={
{
"errorCodes": undefined,
"errorStatus": null,
"locale": undefined,
"authorized": {},
"errorCodes": [],
"errorStatus": undefined,
}
}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ exports[`AuthenticationContext should apply a hook for retrieving auth data from
},
],
],
[
[Function],
],
]
`;

Expand Down
27 changes: 25 additions & 2 deletions src/components/authentication/__tests__/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@ import { rhsmConstants } from '../../../services/rhsm/rhsmConstants';

describe('Authentication Component', () => {
it('should render a basic component', async () => {
const props = {
useGetAuthorization: () => ({
error: false,
pending: false,
data: {
authorized: {},
errorCodes: [],
errorStatus: undefined
}
})
};

const component = await shallowHookComponent(
<Authentication>
<Authentication {...props}>
<span className="test">lorem</span>
</Authentication>
);
Expand Down Expand Up @@ -37,7 +49,18 @@ describe('Authentication Component', () => {

it('should allow being disabled', async () => {
const props = {
isDisabled: true
isDisabled: true,
useGetAuthorization: () => ({
error: false,
pending: false,
data: {
authorized: {
[helpers.UI_NAME]: true
},
errorCodes: [],
errorStatus: undefined
}
})
};
const component = await shallowHookComponent(
<Authentication {...props}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('AuthenticationContext', () => {
it('should apply a hook for retrieving auth data from multiple selectors', async () => {
const { result: errorResponse } = shallowHook(() =>
useGetAuthorization({
useNavigate: () => jest.fn(),
useSelectorsResponse: () => ({
error: true,
data: {
Expand All @@ -33,6 +34,7 @@ describe('AuthenticationContext', () => {
const { result: successResponse } = await mountHook(() =>
useGetAuthorization({
useDispatch: () => mockDispatch,
useNavigate: () => jest.fn(),
useSelectorsResponse: () => ({
fulfilled: true,
data: {
Expand All @@ -52,57 +54,66 @@ describe('AuthenticationContext', () => {
expect(mockDispatch.mock.calls).toMatchSnapshot('success dispatch');
expect(successResponse).toMatchSnapshot('success response');

const { result: mockStoreSuccessResponse } = shallowHook(() => useGetAuthorization(), {
state: {
user: {
auth: {
fulfilled: true,
data: [
{ isAdmin: true, isEntitled: true },
{
permissions: [
{
subscriptions: {
all: true,
resources: {
'*': {
'*': [],
loremCustom: [],
read: []
const { result: mockStoreSuccessResponse } = shallowHook(
() => useGetAuthorization({ useNavigate: () => jest.fn() }),
{
state: {
user: {
auth: {
fulfilled: true,
data: [
{ isAdmin: true, isEntitled: true },
{
permissions: [
{
subscriptions: {
all: true,
resources: {
'*': {
'*': [],
loremCustom: [],
read: []
}
}
}
}
],
authorized: {
subscriptions: true
}
],
authorized: {
subscriptions: true
}
}
]
},
locale: { fulfilled: true, data: {} },
errors: {}
]
},
locale: { fulfilled: true, data: {} },
errors: {}
}
}
}
});
);

expect(mockStoreSuccessResponse).toMatchSnapshot('mock store success response');

const { result: mockStoreErrorResponse } = shallowHook(() => useGetAuthorization(), {
state: {
user: {
auth: {
error: true,
data: []
},
locale: { fulfilled: true, data: {} },
errors: {
error: true,
data: ['lorem', 'ipsum']
const { result: mockStoreErrorResponse } = shallowHook(
() =>
useGetAuthorization({
useNavigate: () => jest.fn()
}),
{
state: {
user: {
auth: {
error: true,
data: []
},
locale: { fulfilled: true, data: {} },
errors: {
error: true,
data: ['lorem', 'ipsum']
}
}
}
}
});
);

expect(mockStoreErrorResponse).toMatchSnapshot('mock store error response');
});
Expand Down
17 changes: 14 additions & 3 deletions src/components/authentication/authenticationContext.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext } from 'react';
import { useMount } from 'react-use';
import React, { useContext, useState } from 'react';
import { useMount, useUnmount } from 'react-use';
import { useChrome } from '@redhat-cloud-services/frontend-components/useChrome';
import { reduxActions, storeHooks } from '../../redux';
import { helpers } from '../../common';
import { routerHelpers } from '../router';
import { routerContext, routerHelpers } from '../router';

/**
* @memberof Authentication
Expand Down Expand Up @@ -33,19 +33,25 @@ const useAuthContext = () => useContext(AuthenticationContext);
* @param {string} options.appName
* @param {Function} options.authorizeUser
* @param {Function} options.hideGlobalFilter
* @param {Function} options.onNavigation
* @param {Function} options.useChrome
* @param {Function} options.useDispatch
* @param {Function} options.useNavigate
* @param {Function} options.useSelectorsResponse
* @returns {{data: {errorCodes, errorStatus: *, locale}, pending: boolean, fulfilled: boolean, error: boolean}}
*/
const useGetAuthorization = ({
appName = routerHelpers.appName,
authorizeUser = reduxActions.platform.authorizeUser,
hideGlobalFilter = reduxActions.platform.hideGlobalFilter,
onNavigation = reduxActions.platform.onNavigation,
useChrome: useAliasChrome = useChrome,
useDispatch: useAliasDispatch = storeHooks.reactRedux.useDispatch,
useNavigate: useAliasNavigate = routerContext.useNavigate,
useSelectorsResponse: useAliasSelectorsResponse = storeHooks.reactRedux.useSelectorsResponse
} = {}) => {
const [unregister, setUnregister] = useState(() => helpers.noop);
const navigate = useAliasNavigate();
const dispatch = useAliasDispatch();
const { updateDocumentTitle = helpers.noop } = useAliasChrome();
const { data, error, fulfilled, pending, responses } = useAliasSelectorsResponse([
Expand All @@ -61,6 +67,11 @@ const useGetAuthorization = ({
await dispatch(authorizeUser());
updateDocumentTitle(appName);
dispatch([hideGlobalFilter()]);
setUnregister(() => dispatch(onNavigation(event => navigate(event.navId))));
});

useUnmount(() => {
unregister();
});

const [user = {}, app = {}] = (Array.isArray(data.auth) && data.auth) || [];
Expand Down
19 changes: 19 additions & 0 deletions src/redux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Platform service wrappers for dispatch, state update.
* [~clearNotifications()](#Actions.module_PlatformActions..clearNotifications) ⇒ <code>\*</code>
* [~authorizeUser(appName)](#Actions.module_PlatformActions..authorizeUser) ⇒ <code>function</code>
* [~hideGlobalFilter(isHidden)](#Actions.module_PlatformActions..hideGlobalFilter) ⇒ <code>Object</code>
* [~onNavigation(callback)](#Actions.module_PlatformActions..onNavigation) ⇒ <code>function</code>

<a name="Actions.module_PlatformActions..addNotification"></a>

Expand Down Expand Up @@ -159,6 +160,24 @@ Hide platform global filter.
</tr> </tbody>
</table>

<a name="Actions.module_PlatformActions..onNavigation"></a>

### PlatformActions~onNavigation(callback) ⇒ <code>function</code>
Apply platform method for updating routing history on "navigating" with the left-nav.

**Kind**: inner method of [<code>PlatformActions</code>](#Actions.module_PlatformActions)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>callback</td><td><code>function</code></td>
</tr> </tbody>
</table>

<a name="Actions.module_RhsmActions"></a>

## RhsmActions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ exports[`PlatformActions Should return a dispatch object for the hideGlobalFilte
"type": "PLATFORM_GLOBAL_FILTER_HIDE",
}
`;

exports[`PlatformActions Should return a function for the onNavigation method: expected process 1`] = `"lorem ipsum"`;

exports[`PlatformActions Should return a function for the onNavigation method: function 1`] = `[Function]`;
8 changes: 8 additions & 0 deletions src/redux/actions/__tests__/platformActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ describe('PlatformActions', () => {
it('Should return a dispatch object for the hideGlobalFilter method', () => {
expect(platformActions.hideGlobalFilter()).toMatchSnapshot('dispatch object');
});

it('Should return a function for the onNavigation method', () => {
expect(platformActions.onNavigation()).toMatchSnapshot('function');

window.insights.chrome.on = jest.fn().mockImplementation((id, value) => value('lorem'));
const dispatch = obj => obj;
expect(platformActions.onNavigation(event => `${event} ipsum`)(dispatch)).toMatchSnapshot('expected process');
});
});
19 changes: 17 additions & 2 deletions src/redux/actions/platformActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,26 @@ const hideGlobalFilter = isHidden => ({
payload: platformServices.hideGlobalFilter(isHidden)
});

/**
* Apply platform method for updating routing history on "navigating" with the left-nav.
*
* @param {Function} callback
* @returns {Function}
*/
const onNavigation = callback => dispatch => {
dispatch({
type: platformTypes.PLATFORM_ON_NAV
});
return platformServices.onNavigation(callback);
};

const platformActions = {
addNotification,
removeNotification,
clearNotifications,
authorizeUser,
hideGlobalFilter
hideGlobalFilter,
onNavigation
};

export {
Expand All @@ -74,5 +88,6 @@ export {
removeNotification,
clearNotifications,
authorizeUser,
hideGlobalFilter
hideGlobalFilter,
onNavigation
};
4 changes: 4 additions & 0 deletions src/redux/types/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports[`ReduxTypes should have specific type properties: all redux types 1`] =
"PLATFORM_ADD_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/ADD_NOTIFICATION",
"PLATFORM_CLEAR_NOTIFICATIONS": "@@INSIGHTS-CORE/NOTIFICATIONS/CLEAR_NOTIFICATIONS",
"PLATFORM_GLOBAL_FILTER_HIDE": "PLATFORM_GLOBAL_FILTER_HIDE",
"PLATFORM_ON_NAV": "PLATFORM_ON_NAV",
"PLATFORM_REMOVE_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/REMOVE_NOTIFICATION",
"PLATFORM_USER_AUTH": "PLATFORM_USER_AUTH",
},
Expand Down Expand Up @@ -93,6 +94,7 @@ exports[`ReduxTypes should have specific type properties: all redux types 1`] =
"PLATFORM_ADD_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/ADD_NOTIFICATION",
"PLATFORM_CLEAR_NOTIFICATIONS": "@@INSIGHTS-CORE/NOTIFICATIONS/CLEAR_NOTIFICATIONS",
"PLATFORM_GLOBAL_FILTER_HIDE": "PLATFORM_GLOBAL_FILTER_HIDE",
"PLATFORM_ON_NAV": "PLATFORM_ON_NAV",
"PLATFORM_REMOVE_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/REMOVE_NOTIFICATION",
"PLATFORM_USER_AUTH": "PLATFORM_USER_AUTH",
},
Expand Down Expand Up @@ -146,6 +148,7 @@ exports[`ReduxTypes should have specific type properties: all redux types 1`] =
"PLATFORM_ADD_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/ADD_NOTIFICATION",
"PLATFORM_CLEAR_NOTIFICATIONS": "@@INSIGHTS-CORE/NOTIFICATIONS/CLEAR_NOTIFICATIONS",
"PLATFORM_GLOBAL_FILTER_HIDE": "PLATFORM_GLOBAL_FILTER_HIDE",
"PLATFORM_ON_NAV": "PLATFORM_ON_NAV",
"PLATFORM_REMOVE_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/REMOVE_NOTIFICATION",
"PLATFORM_USER_AUTH": "PLATFORM_USER_AUTH",
},
Expand Down Expand Up @@ -246,6 +249,7 @@ exports[`ReduxTypes should have specific type properties: specific types 1`] = `
"PLATFORM_ADD_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/ADD_NOTIFICATION",
"PLATFORM_CLEAR_NOTIFICATIONS": "@@INSIGHTS-CORE/NOTIFICATIONS/CLEAR_NOTIFICATIONS",
"PLATFORM_GLOBAL_FILTER_HIDE": "PLATFORM_GLOBAL_FILTER_HIDE",
"PLATFORM_ON_NAV": "PLATFORM_ON_NAV",
"PLATFORM_REMOVE_NOTIFICATION": "@@INSIGHTS-CORE/NOTIFICATIONS/REMOVE_NOTIFICATION",
"PLATFORM_USER_AUTH": "PLATFORM_USER_AUTH",
},
Expand Down
Loading

0 comments on commit c74f315

Please sign in to comment.