Skip to content

Commit 5f2b67d

Browse files
authored
Fix defaultUrl sending hash content (#1132)
1 parent ba43d86 commit 5f2b67d

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/__tests__/core/index.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
import Immutable from 'immutable';
22
import { dataFns } from '../../utils/data_utils';
3+
import { clientID, domain } from '../../core/index';
4+
import { initI18n } from '../../i18n';
5+
import { setURL } from '../testUtils';
36

47
const setResolvedConnection = (...params) => require('core/index').setResolvedConnection(...params);
8+
const setup = (...params) => require('core/index').setup(...params);
59

610
const mockLock = 'm';
711
let mockSet;
12+
let mockInit;
13+
14+
jest.mock('i18n', () => ({
15+
initI18n: jest.fn()
16+
}));
17+
818
jest.mock('utils/data_utils', () => ({
919
dataFns: () => ({
10-
set: mockSet
20+
get: jest.fn(),
21+
set: mockSet,
22+
init: mockInit
1123
})
1224
}));
1325

26+
describe('setup', () => {
27+
beforeEach(() => {
28+
mockInit = jest.fn();
29+
jest.resetModules();
30+
});
31+
it('default redirectUrl should not include location.hash', () => {
32+
setURL('https://test.com/path/#not-this-part');
33+
const options = {};
34+
setup('id', 'clientID', 'domain', options, 'hookRunner', 'emitEventFn');
35+
const { mock } = mockInit;
36+
expect(mock.calls.length).toBe(1);
37+
const model = mock.calls[0][1].toJS();
38+
expect(model.auth.redirectUrl).toBe('https://test.com/path/');
39+
});
40+
});
41+
1442
describe('setResolvedConnection', () => {
1543
beforeEach(() => {
1644
mockSet = jest.fn();

src/__tests__/testUtils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,25 @@ export const mockComponent = (type, domElement = 'div') => props =>
2626

2727
export const extractPropsFromWrapper = (wrapper, index = 0) =>
2828
removeDataFromProps(wrapper.find('div').at(index).props());
29+
30+
//set urls with jest: https://github.com/facebook/jest/issues/890#issuecomment-298594389
31+
export const setURL = url => {
32+
const parser = document.createElement('a');
33+
parser.href = url;
34+
[
35+
'href',
36+
'protocol',
37+
'host',
38+
'hostname',
39+
'origin',
40+
'port',
41+
'pathname',
42+
'search',
43+
'hash'
44+
].forEach(prop => {
45+
Object.defineProperty(window.location, prop, {
46+
value: parser[prop],
47+
writable: true
48+
});
49+
});
50+
};

src/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ function extractAuthOptions(options) {
266266
// if responseType was not set and there is a redirectUrl, it defaults to code. Otherwise token.
267267
responseType = typeof responseType === 'string' ? responseType : redirectUrl ? 'code' : 'token';
268268
// now we set the default because we already did the validation
269-
redirectUrl = redirectUrl || window.location.href;
269+
redirectUrl = redirectUrl || `${window.location.origin}${window.location.pathname}`;
270270

271271
sso = typeof sso === 'boolean' ? sso : true;
272272

0 commit comments

Comments
 (0)