-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[+] Added
getDefaultCallbackUrl
helper (#936)
* [+] Added `getDefaultCallbackUrl` helper * Updated CHANGELOG.md
- Loading branch information
1 parent
ecfc39e
commit 54929e7
Showing
19 changed files
with
478 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { builtCallbackUrl } from '../builtCallbackUrl'; | ||
|
||
describe('builtCallbackUrl tests', () => { | ||
const url = 'https://wallet.multiversx.com'; | ||
|
||
test('returns callbackUrl unmodified if urlParams is empty', () => { | ||
expect(builtCallbackUrl({ callbackUrl: url })).toBe(url); | ||
}); | ||
|
||
test('adds urlParams', () => { | ||
expect( | ||
builtCallbackUrl({ | ||
callbackUrl: url, | ||
urlParams: { status: 'success' } | ||
}) | ||
).toBe('https://wallet.multiversx.com/?status=success'); | ||
}); | ||
|
||
test('adds urlParams and keeps existing hash', () => { | ||
expect( | ||
builtCallbackUrl({ | ||
callbackUrl: url + '#test', | ||
urlParams: { status: 'success' } | ||
}) | ||
).toBe('https://wallet.multiversx.com/?status=success#test'); | ||
}); | ||
|
||
test('keeps existing urlParams', () => { | ||
expect( | ||
builtCallbackUrl({ | ||
callbackUrl: url + '?page=1', | ||
urlParams: { status: 'success' } | ||
}) | ||
).toBe('https://wallet.multiversx.com/?page=1&status=success'); | ||
}); | ||
|
||
test('keeps existing hash', () => { | ||
expect( | ||
builtCallbackUrl({ | ||
callbackUrl: url + '?page=1#logs', | ||
urlParams: { status: 'success' } | ||
}) | ||
).toBe('https://wallet.multiversx.com/?page=1&status=success#logs'); | ||
}); | ||
|
||
test('throws error if callbackUrl is invalid and urlParams are defined', () => { | ||
expect( | ||
builtCallbackUrl({ | ||
callbackUrl: '', | ||
urlParams: { status: 'success' } | ||
}) | ||
).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getWindowLocation } from './index'; | ||
|
||
export const getDefaultCallbackUrl = () => { | ||
const { pathname, search, hash } = getWindowLocation(); | ||
|
||
return `${pathname ?? ''}${search ?? ''}${hash ?? ''}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './addOriginToLocationPath'; | ||
export * from './getDefaultCallbackUrl'; | ||
export * from './getWindowLocation'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { getDefaultCallbackUrl } from '../getDefaultCallbackUrl'; | ||
|
||
const searchMock = '?search=mock'; | ||
const pathnameMock = '/unlock'; | ||
const originMock = 'https://multiversx.com'; | ||
const hashMock = '#main'; | ||
const hrefMock = 'https://multiversx.com/technology'; | ||
const urlMock = '/unlock?search=mock#main'; | ||
|
||
let windowSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
describe('Get window location', () => { | ||
it('getDefaultCallbackUrl should be undefined', () => { | ||
windowSpy.mockImplementation(() => undefined); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(''); | ||
}); | ||
|
||
it('window should return search', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
search: searchMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(searchMock); | ||
}); | ||
|
||
it('getDefaultCallbackUrl should return pathname', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
pathname: pathnameMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(pathnameMock); | ||
}); | ||
|
||
it('getDefaultCallbackUrl should return empty string when only origin is specified', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
origin: originMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(''); | ||
}); | ||
|
||
it('getDefaultCallbackUrl should return hash', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
hash: hashMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(hashMock); | ||
}); | ||
|
||
it('getDefaultCallbackUrl should return empty', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
href: hrefMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(''); | ||
}); | ||
|
||
it('getDefaultCallbackUrl should return complete URL without origin', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
location: { | ||
pathname: pathnameMock, | ||
origin: originMock, | ||
hash: hashMock, | ||
href: hrefMock, | ||
search: searchMock | ||
} | ||
})); | ||
|
||
const callbackUrl = getDefaultCallbackUrl(); | ||
expect(callbackUrl).toStrictEqual(urlMock); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.