forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from cjcenizal/implement/storeStateInLocalstor…
…age/refactor-pure-functions Refactor state-hashing files into state_hashing and state_storage subdirectories.
- Loading branch information
Showing
16 changed files
with
170 additions
and
149 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 was deleted.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
src/ui/public/state_management/state_hashing/__tests__/unhash_url.js
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,73 @@ | ||
import expect from 'expect.js'; | ||
import ngMock from 'ng_mock'; | ||
import sinon from 'auto-release-sinon'; | ||
|
||
import StateProvider from 'ui/state_management/state'; | ||
import { unhashUrl } from 'ui/state_management/state_hashing'; | ||
|
||
describe('unhashUrl', () => { | ||
let unhashableStates; | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
|
||
beforeEach(ngMock.inject(Private => { | ||
const State = Private(StateProvider); | ||
const unhashableState = new State('testParam'); | ||
sinon.stub(unhashableState, 'translateHashToRison').withArgs('hash').returns('replacement'); | ||
unhashableStates = [unhashableState]; | ||
})); | ||
|
||
describe('does nothing', () => { | ||
it('if missing input', () => { | ||
expect(() => { | ||
unhashUrl(); | ||
}).to.not.throwError(); | ||
}); | ||
|
||
it('if just a host and port', () => { | ||
const url = 'https://localhost:5601'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if just a path', () => { | ||
const url = 'https://localhost:5601/app/kibana'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if just a path and query', () => { | ||
const url = 'https://localhost:5601/app/kibana?foo=bar'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if empty hash with query', () => { | ||
const url = 'https://localhost:5601/app/kibana?foo=bar#'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if empty hash without query', () => { | ||
const url = 'https://localhost:5601/app/kibana#'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if empty hash without query', () => { | ||
const url = 'https://localhost:5601/app/kibana#'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if hash is just a path', () => { | ||
const url = 'https://localhost:5601/app/kibana#/discover'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
|
||
it('if hash does not have matching query string vals', () => { | ||
const url = 'https://localhost:5601/app/kibana#/discover?foo=bar'; | ||
expect(unhashUrl(url, unhashableStates)).to.be(url); | ||
}); | ||
}); | ||
|
||
it('replaces query string vals in hash for matching states with output of state.toRISON()', () => { | ||
const urlWithHashes = 'https://localhost:5601/#/?foo=bar&testParam=hash'; | ||
const exp = 'https://localhost:5601/#/?foo=bar&testParam=replacement'; | ||
expect(unhashUrl(urlWithHashes, unhashableStates)).to.be(exp); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
src/ui/public/state_management/state_hashing/get_unhashable_states_provider.js
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,5 @@ | ||
export default function getUnhashableStatesProvider(getAppState, globalState) { | ||
return function getUnhashableStates() { | ||
return [getAppState(), globalState].filter(Boolean); | ||
}; | ||
} |
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,11 @@ | ||
export { | ||
default as getUnhashableStatesProvider, | ||
} from './get_unhashable_states_provider'; | ||
|
||
export { | ||
default as unhashQueryString, | ||
} from './unhash_query_string'; | ||
|
||
export { | ||
default as unhashUrl, | ||
} from './unhash_url'; |
8 changes: 8 additions & 0 deletions
8
src/ui/public/state_management/state_hashing/unhash_query_string.js
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,8 @@ | ||
import { mapValues } from 'lodash'; | ||
|
||
export default function unhashQueryString(parsedQueryString, states) { | ||
return mapValues(parsedQueryString, (val, key) => { | ||
const state = states.find(s => key === s.getQueryParamName()); | ||
return state ? state.translateHashToRison(val) : val; | ||
}); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/ui/public/state_management/state_hashing/unhash_url.js
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,36 @@ | ||
import { | ||
parse as parseUrl, | ||
format as formatUrl, | ||
} from 'url'; | ||
|
||
import unhashQueryString from './unhash_query_string'; | ||
|
||
export default function unhashUrl(urlWithHashes, states) { | ||
if (!urlWithHashes) return urlWithHashes; | ||
|
||
const urlWithHashesParsed = parseUrl(urlWithHashes, true); | ||
if (!urlWithHashesParsed.hostname) { | ||
// passing a url like "localhost:5601" or "/app/kibana" should be prevented | ||
throw new TypeError( | ||
'Only absolute urls should be passed to `unhashUrl()`. ' + | ||
'Unable to detect url hostname.' | ||
); | ||
} | ||
|
||
if (!urlWithHashesParsed.hash) return urlWithHashes; | ||
|
||
const appUrl = urlWithHashesParsed.hash.slice(1); // trim the # | ||
if (!appUrl) return urlWithHashes; | ||
|
||
const appUrlParsed = parseUrl(urlWithHashesParsed.hash.slice(1), true); | ||
if (!appUrlParsed.query) return urlWithHashes; | ||
|
||
const appQueryWithoutHashes = unhashQueryString(appUrlParsed.query || {}, states); | ||
return formatUrl({ | ||
...urlWithHashesParsed, | ||
hash: formatUrl({ | ||
pathname: appUrlParsed.pathname, | ||
query: appQueryWithoutHashes, | ||
}) | ||
}); | ||
} |
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,7 @@ | ||
export { | ||
default as HashingStore, | ||
} from './hashing_store'; | ||
|
||
export { | ||
default as LazyLruStore, | ||
} from './lazy_lru_store'; |
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.