-
Notifications
You must be signed in to change notification settings - Fork 48
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
Adding Simple URL-based feature flags #117
Merged
+203
−32
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3defdff
Fixes #66: As a developer, I want to limit the audience
NatHillardUSDS 3ada521
Fixes issue #107 Geoplatform AWS throws errors on load - we had confi…
NatHillardUSDS babdbae
Fixing type to use Location instead of URL
NatHillardUSDS c333611
Updating README with info on how to use feature flags
NatHillardUSDS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,39 @@ | ||
import * as React from 'react'; | ||
import {render, screen} from '@testing-library/react'; | ||
import {URLFlagProvider, useFlags} from './FlagContext'; | ||
|
||
describe('URL params are parsed and passed to children', () => { | ||
describe('when the URL has a "flags" parameter set', () => { | ||
// We artificially set the URL to localhost?flags=1,2,3 | ||
beforeEach(() => { | ||
window.history.pushState({}, 'Test Title', '/?flags=1,2,3'); | ||
}); | ||
describe('when using useFlags', () => { | ||
beforeEach(() => { | ||
const FlagConsumer = () => { | ||
const flags = useFlags(); | ||
return ( | ||
<> | ||
<div>{flags.includes('1') ? 'yes1' : 'no1'}</div> | ||
<div>{flags.includes('2') ? 'yes2' : 'no2'}</div> | ||
<div>{flags.includes('3') ? 'yes3' : 'no3'}</div> | ||
<div>{flags.includes('4') ? 'yes4' : 'no4'}</div> | ||
</> | ||
); | ||
}; | ||
render( | ||
<URLFlagProvider location={location}> | ||
<FlagConsumer /> | ||
</URLFlagProvider>, | ||
); | ||
}); | ||
|
||
it('gives child components the flag values', async () => { | ||
expect(screen.queryByText('yes1')).toBeInTheDocument(); | ||
expect(screen.queryByText('yes2')).toBeInTheDocument(); | ||
expect(screen.queryByText('yes3')).toBeInTheDocument(); | ||
expect(screen.queryByText('yes4')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
import * as React from 'react'; | ||
import * as queryString from 'query-string'; | ||
|
||
/** | ||
* FlagContext stores feature flags and passes them to consumers | ||
*/ | ||
interface IFlagContext { | ||
/** | ||
* Contains a list of all currently-active flags | ||
*/ | ||
flags: string[]; | ||
} | ||
|
||
const FlagContext = React.createContext<IFlagContext>({flags: []}); | ||
|
||
/** | ||
* `useFlags` returns all feature flags. | ||
* | ||
* @return {Flags[]} flags All project feature flags | ||
*/ | ||
const useFlags = () : string[] => { | ||
const {flags} = React.useContext(FlagContext); | ||
return flags; | ||
}; | ||
|
||
interface IURLFlagProviderProps { | ||
children: React.ReactNode, | ||
location: Location | ||
} | ||
|
||
/** | ||
* `URLFlagProvider` is a provider for FlagContext. | ||
* It is passed the current URL and parses the | ||
* "flags" parameter, assumed to be a comma-separated | ||
* list of currently-active flags. | ||
* @param {URL} location : the current URL object | ||
* @param {ReactNode} children : the children components | ||
* @return {ReactNode} URLFlagProvider component | ||
**/ | ||
const URLFlagProvider = ({children, location}: IURLFlagProviderProps) => { | ||
const flagString = queryString.parse(location.search).flags; | ||
let flags: string[] = []; | ||
if (flagString && typeof flagString === 'string') { | ||
flags = (flagString as string).split(','); | ||
} | ||
console.log(JSON.stringify(location), JSON.stringify(flags)); | ||
|
||
return ( | ||
<FlagContext.Provider | ||
value={{flags}}> | ||
{children} | ||
</FlagContext.Provider> | ||
); | ||
}; | ||
|
||
export {FlagContext, URLFlagProvider, useFlags}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this is an indication that prettier isn't running correctly for my ide setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, that appears to have snuck in - this was formatted automatically when I ran
gatsby develop