Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
- [#275](https://github.com/okta/okta-react/pull/275)
- Adds new component `<SecureOutlet>` for integration with `react-router 6.x`. It should be imported from `@okta/okta-react/react-router-6`

# 6.9.0

### Bug Fixes

-[#284](https://github.com/okta/okta-react/pull/284) fix: passes the return value of `restoreOriginalUri()`, so promises will be awaited

# 6.8.0

### Bug Fixes
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okta/okta-react",
"version": "6.8.0",
"version": "6.10.0",
"description": "React support for Okta",
"private": true,
"scripts": {
Expand Down Expand Up @@ -62,7 +62,8 @@
"//": "set-value@2.0.1 has a vuln, see OKTA-473553",
"**/set-value": "^4.1.0",
"ejs": "^3.1.7",
"axios": "^0.27.2"
"axios": "^0.27.2",
"**/glob": "^9.3.5"
},
"dependencies": {
"@babel/runtime": "^7.11.2",
Expand All @@ -85,7 +86,7 @@
"@babel/plugin-transform-runtime": "^7.19.1",
"@babel/preset-env": "^7.19.3",
"@babel/preset-react": "^7.18.6",
"@okta/okta-auth-js": "^7.0.0",
"@okta/okta-auth-js": "^7.7.0",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-replace": "^2.3.4",
"@testing-library/jest-dom": "^5.16.2",
Expand Down Expand Up @@ -160,4 +161,4 @@
"**/@types/react-router-dom"
]
}
}
}
2 changes: 1 addition & 1 deletion src/Security.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const Security: React.FC<SecurityProps & React.HTMLAttributes<HTMLDivElement>> =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
oktaAuth.options.restoreOriginalUri = (async (oktaAuth: unknown, originalUri: string) => {
restoreOriginalUri(oktaAuth as OktaAuth, originalUri);
return restoreOriginalUri(oktaAuth as OktaAuth, originalUri);
}) as ((oktaAuth: OktaAuth, originalUri?: string) => Promise<void>);
restoreOriginalUriOverridden = true;
}, []); // empty array, only check on component mount
Expand Down
82 changes: 55 additions & 27 deletions test/jest/security.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ describe('<Security />', () => {
let Security: React.FC<SecurityProps>;
let useOktaAuth: () => IOktaContext;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const restoreOriginalUri = async (_: OktaAuth, url: string) => {
location.href = url;
// leaving empty, doesn't affect tests, was causing jsdom error (location.href is not supported)
// location.href = url;
};
beforeEach(async () => {
jest.clearAllMocks();
Expand Down Expand Up @@ -150,14 +152,58 @@ describe('<Security />', () => {
});
});

it('should set default restoreOriginalUri callback in oktaAuth.options', () => {
oktaAuth.options = {};
const mockProps = {
oktaAuth,
restoreOriginalUri
};
mount(<Security {...mockProps} />);
expect(oktaAuth.options.restoreOriginalUri).toBeDefined();
describe('restoreOriginalUri', () => {
it('should set default restoreOriginalUri callback in oktaAuth.options', () => {
oktaAuth.options = {};
const mockProps = {
oktaAuth,
restoreOriginalUri
};
mount(<Security {...mockProps} />);
expect(oktaAuth.options.restoreOriginalUri).toBeDefined();
});

it('should only log warning of restoreOriginalUri option once', () => {
oktaAuth.options = {
restoreOriginalUri
};
const mockProps = {
oktaAuth,
restoreOriginalUri
};
const warning = 'Two custom restoreOriginalUri callbacks are detected. The one from the OktaAuth configuration will be overridden by the provided restoreOriginalUri prop from the Security component.';
const spy = jest.spyOn(console, 'warn');
const wrapper = mount(<Security {...mockProps} />);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(warning);
spy.mockClear();
wrapper.setProps({restoreOriginalUri: 'foo'}); // forces rerender
expect(spy).toBeCalledTimes(0);
});

it('should await the resulting Promise when a fn returning a Promise is provided', async () => {
oktaAuth.options = {};

let hasResolved = false;
const restoreSpy = jest.fn().mockImplementation(() => {
return new Promise(resolve => {
// adds small sleep so non-awaited promises will "fallthrough"
// and the test will fail, unless it awaits for the sleep duration
// (meaning the resulting promise was awaited)
setTimeout(() => {
hasResolved = true;
resolve('foo');
}, 500);
});
});

mount(<Security oktaAuth={oktaAuth} restoreOriginalUri={restoreSpy} />);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await oktaAuth.options.restoreOriginalUri!(oktaAuth, 'foo');
expect(hasResolved).toEqual(true);
expect(restoreSpy).toHaveBeenCalledTimes(1);
expect(restoreSpy).toHaveBeenCalledWith(oktaAuth, 'foo');
});
});

it('gets initial state from oktaAuth and exposes it on the context', () => {
Expand Down Expand Up @@ -408,22 +454,4 @@ describe('<Security />', () => {
expect(wrapper.find(Security).html()).toBe('<p>AuthSdkError: No restoreOriginalUri callback passed to Security Component.</p>');
});
});

it('should only log warning of restoreOriginalUri option once', () => {
oktaAuth.options = {
restoreOriginalUri: restoreOriginalUri as any
};
const mockProps = {
oktaAuth,
restoreOriginalUri
};
const warning = 'Two custom restoreOriginalUri callbacks are detected. The one from the OktaAuth configuration will be overridden by the provided restoreOriginalUri prop from the Security component.';
const spy = jest.spyOn(console, 'warn');
const wrapper = mount(<Security {...mockProps} />);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(warning);
spy.mockClear();
wrapper.setProps({restoreOriginalUri: 'foo'}); // forces rerender
expect(spy).toBeCalledTimes(0);
});
});
Loading