Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STCOR-864 correctly evaluate typeof stripes.okapi #1498

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Fix 404 error page when logging in after changing password in Eureka. Refs STCOR-845.
* Always retrieve `clientId` and `tenant` values from `config.tenantOptions` in stripes.config.js. Retires `okapi.tenant`, `okapi.clientId`, and `config.isSingleTenant`. Refs STCOR-787.
* List UI apps in "Applications/modules/interfaces" column. STCOR-773
* Correctly evaluate `stripes.okapi` before rendering `<RootWithIntl>`. Refs STCOR-864.

## [10.1.1](https://github.com/folio-org/stripes-core/tree/v10.1.1) (2024-03-25)
[Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.1.0...v10.1.1)
Expand Down
2 changes: 1 addition & 1 deletion src/RootWithIntl.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const RootWithIntl = ({ stripes, token = '', isAuthenticated = false, disableAut
event={events.LOGIN}
stripes={connectedStripes}
/>
{ (connectedStripes.okapi !== 'object' || connectedStripes.discovery.isFinished) && (
{ (typeof connectedStripes.okapi !== 'object' || connectedStripes.discovery.isFinished) && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZOMG!

<ModuleContainer id="content">
<OverlayContainer />
{connectedStripes.config.useSecureTokens && <SessionEventContainer history={history} />}
Expand Down
126 changes: 66 additions & 60 deletions src/RootWithIntl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@
/* eslint-disable no-unused-vars */

import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
import { Router as DefaultRouter } from 'react-router-dom';
import { createMemoryHistory } from 'history';

import { Redirect as InternalRedirect } from 'react-router-dom';
import Redirect from './components/Redirect';
import { Login } from './components';
import PreLoginLanding from './components/PreLoginLanding';
import AuthnLogin from './components/AuthnLogin';
import MainNav from './components/MainNav';
import MainContainer from './components/MainContainer';
import ModuleContainer from './components/ModuleContainer';
import RootWithIntl from './RootWithIntl';
import Stripes from './Stripes';

// import {
// renderLogoutComponent
// } from './RootWithIntl';
jest.mock('./components/AuthnLogin', () => () => '<AuthnLogin>');
jest.mock('./components/MainNav', () => () => '<MainNav>');
jest.mock('./components/ModuleContainer', () => () => '<ModuleContainer>');
jest.mock('./components/MainContainer', () => ({ children }) => children);

import AuthnLogin from './components/AuthnLogin';
const defaultHistory = createMemoryHistory();

jest.mock('react-router-dom', () => ({
Redirect: () => '<internalredirect>',
withRouter: (Component) => Component,
}));
jest.mock('./components/Redirect', () => () => '<redirect>');
jest.mock('./components/Login', () => () => '<login>');
jest.mock('./components/PreLoginLanding', () => () => '<preloginlanding>');
const Harness = ({
Router = DefaultRouter,
children,
history = defaultHistory,
}) => {
return (
<Router history={history}>
{children}
</Router>
);
};

const store = {
getState: () => ({
Expand All @@ -34,56 +43,53 @@ const store = {
};

describe('RootWithIntl', () => {
describe('AuthnLogin', () => {
it('handles legacy login', () => {
const stripes = { okapi: {}, config: {}, store };
render(<AuthnLogin stripes={stripes} />);
it('renders login without one of (isAuthenticated, token, disableAuth)', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, discovery: { isFinished: false } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} isAuthenticated={false} /></Harness>);

expect(screen.getByText(/<AuthnLogin>/)).toBeInTheDocument();
expect(screen.queryByText(/<MainNav>/)).toBeNull();
});

describe('renders MainNav', () => {
it('given isAuthenticated', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, discovery: { isFinished: false } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} isAuthenticated /></Harness>);

expect(screen.getByText(/<login>/)).toBeInTheDocument();
expect(screen.queryByText(/<AuthnLogin>/)).toBeNull();
expect(screen.queryByText(/<MainNav>/)).toBeInTheDocument();
});

describe('handles third-party login', () => {
it('handles single-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://barbie.com' },
config: {
isSingleTenant: true,
tenantOptions: {
diku: { name: 'diku', clientId: 'diku-application' }
}
},
store
};
render(<AuthnLogin stripes={stripes} />);

expect(screen.getByText(/<redirect>/)).toBeInTheDocument();
});

it('handles multi-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://oppie.com' },
config: {
isSingleTenant: false,
tenantOptions: {
diku: { name: 'diku', clientId: 'diku-application' },
diku2: { name: 'diku2', clientId: 'diku2-application' }
}
},
store
};
render(<AuthnLogin stripes={stripes} />);

expect(screen.getByText(/<preloginlanding>/)).toBeInTheDocument();
});
it('given token', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, discovery: { isFinished: false } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} token /></Harness>);

expect(screen.queryByText(/<AuthnLogin>/)).toBeNull();
expect(screen.queryByText(/<MainNav>/)).toBeInTheDocument();
});

it('given disableAuth', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, discovery: { isFinished: false } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} disableAuth /></Harness>);

expect(screen.queryByText(/<AuthnLogin>/)).toBeNull();
expect(screen.queryByText(/<MainNav>/)).toBeInTheDocument();
});
});

// describe('renderLogoutComponent', () => {
// it('handles legacy logout', () => {
// const stripes = { okapi: {}, config: {} };
// render(renderLogoutComponent(stripes));
describe('renders ModuleContainer', () => {
it('if config.okapi is not an object', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, discovery: { isFinished: true } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} isAuthenticated /></Harness>);

expect(screen.getByText(/<ModuleContainer>/)).toBeInTheDocument();
});

// expect(screen.getByText(/<internalredirect>/)).toBeInTheDocument();
// });
// });
it('if discovery is finished', async () => {
const stripes = new Stripes({ epics: {}, logger: {}, bindings: {}, config: {}, store, okapi: {}, discovery: { isFinished: true } });
await render(<Harness><RootWithIntl stripes={stripes} history={defaultHistory} isAuthenticated /></Harness>);

expect(screen.getByText(/<ModuleContainer>/)).toBeInTheDocument();
});
});
});
76 changes: 76 additions & 0 deletions src/components/AuthnLogin/AuthnLogin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* shhhh, eslint, it's ok. we need "unused" imports for mocks */
/* eslint-disable no-unused-vars */

import { render, screen } from '@folio/jest-config-stripes/testing-library/react';

import { Redirect as InternalRedirect } from 'react-router-dom';
import Redirect from '../Redirect';
import Login from '../Login';
import PreLoginLanding from '../PreLoginLanding';

import AuthnLogin from './AuthnLogin';

jest.mock('react-router-dom', () => ({
Redirect: () => '<internalredirect>',
withRouter: (Component) => Component,
}));
jest.mock('../Redirect', () => () => '<redirect>');
jest.mock('../Login', () => () => '<login>');
jest.mock('../PreLoginLanding', () => () => '<preloginlanding>');

const store = {
getState: () => ({
okapi: {
token: '123',
},
}),
dispatch: () => {},
subscribe: () => {},
replaceReducer: () => {},
};

describe('RootWithIntl', () => {
describe('AuthnLogin', () => {
it('handles legacy login', () => {
const stripes = { okapi: {}, config: {}, store };
render(<AuthnLogin stripes={stripes} />);

expect(screen.getByText(/<login>/)).toBeInTheDocument();
});

describe('handles third-party login', () => {
it('handles single-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://barbie.com' },
config: {
isSingleTenant: true,
tenantOptions: {
diku: { name: 'diku', clientId: 'diku-application' }
}
},
store
};
render(<AuthnLogin stripes={stripes} />);

expect(screen.getByText(/<redirect>/)).toBeInTheDocument();
});

it('handles multi-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://oppie.com' },
config: {
isSingleTenant: false,
tenantOptions: {
diku: { name: 'diku', clientId: 'diku-application' },
diku2: { name: 'diku2', clientId: 'diku2-application' }
}
},
store
};
render(<AuthnLogin stripes={stripes} />);

expect(screen.getByText(/<preloginlanding>/)).toBeInTheDocument();
});
});
});
});
3 changes: 3 additions & 0 deletions test/bigtest/helpers/setup-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default function setupApplication({
currentPerms: permissions,
isAuthenticated: true,
};
initialState.discovery = {
isFinished: true,
};
} else {
initialState.okapi = {
ssoEnabled: true,
Expand Down
1 change: 1 addition & 0 deletions test/jest/__mock__/stripesComponents.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jest.mock('@folio/stripes-components', () => ({
<span>{children}</span>
)),
Headline: jest.fn(({ children }) => <div>{ children }</div>),
HotKeys: jest.fn(({ children }) => <>{ children }</>),
Icon: jest.fn((props) => (props && props.children ? props.children : <span />)),
IconButton: jest.fn(({
buttonProps,
Expand Down
Loading