-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop removing query parameters when persisting application state to t…
…he url
- Loading branch information
1 parent
0334a6c
commit 3524927
Showing
8 changed files
with
379 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
import {Store} from '@ngrx/store'; | ||
import {Observable} from 'rxjs'; | ||
import {map} from 'rxjs/operators'; | ||
|
||
import {Location} from '../app_routing/location'; | ||
import {SerializableQueryParams} from '../app_routing/types'; | ||
import {State} from '../app_state'; | ||
import * as selectors from '../selectors'; | ||
import {EXPERIMENTAL_PLUGIN_QUERY_PARAM_KEY} from '../webapp_data_source/tb_feature_flag_data_source_types'; | ||
import {FeatureFlagMetadata, FeatureFlagQueryParameters,} from '../webapp_data_source/tb_feature_flag_query_parameters'; | ||
|
||
export function getFeatureFlagStates(store: Store<State>): | ||
Observable<SerializableQueryParams> { | ||
return store.select(selectors.getEnabledExperimentalPlugins) | ||
.pipe(map((experimentalPlugins) => { | ||
const queryParams = experimentalPlugins.map((pluginId) => { | ||
return {key: EXPERIMENTAL_PLUGIN_QUERY_PARAM_KEY, value: pluginId}; | ||
}); | ||
|
||
const currentQueryParams = Object.fromEntries( | ||
serializableQueryParamsToEntries(new Location().getSearch() || [])); | ||
|
||
Object.values(FeatureFlagQueryParameters) | ||
.forEach((overriddenFeatureFlag: FeatureFlagMetadata) => { | ||
const queryParamOverride = | ||
overriddenFeatureFlag.queryParamOverride; | ||
if (queryParamOverride && | ||
queryParamOverride in currentQueryParams) { | ||
queryParams.push({ | ||
key: queryParamOverride, | ||
value: currentQueryParams[queryParamOverride], | ||
}); | ||
} | ||
}); | ||
|
||
return queryParams; | ||
|
||
function serializableQueryParamsToEntries( | ||
params: SerializableQueryParams): [string, string][] { | ||
return params.map(({key, value}) => [key, value]); | ||
} | ||
})); | ||
} |
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,85 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
import {TestBed} from '@angular/core/testing'; | ||
import {Store} from '@ngrx/store'; | ||
import {MockStore, provideMockStore} from '@ngrx/store/testing'; | ||
|
||
import {Location} from '../app_routing/location'; | ||
import {SerializableQueryParams} from '../app_routing/types'; | ||
import {State} from '../app_state'; | ||
import {appStateFromMetricsState, buildMetricsState} from '../metrics/testing'; | ||
import * as selectors from '../selectors'; | ||
|
||
import {getFeatureFlagStates} from './feature_flag_serializer'; | ||
|
||
describe('feature flag serializer', () => { | ||
let store: MockStore<State>; | ||
let location: Location; | ||
let getSearchSpy: jasmine.Spy; | ||
|
||
beforeEach(async () => { | ||
await TestBed | ||
.configureTestingModule({ | ||
providers: [ | ||
provideMockStore({ | ||
initialState: { | ||
...appStateFromMetricsState(buildMetricsState()), | ||
}, | ||
}), | ||
], | ||
}) | ||
.compileComponents(); | ||
|
||
store = TestBed.inject<Store<State>>(Store) as MockStore<State>; | ||
store.overrideSelector(selectors.getEnabledExperimentalPlugins, []); | ||
|
||
location = TestBed.inject(Location); | ||
getSearchSpy = spyOn(location, 'getSearch').and.returnValue([]); | ||
}); | ||
|
||
describe('getFeatureFlagStates', () => { | ||
it('returns empty list when no feature flags are active', async () => { | ||
const queryParams = await promiseGetFeatureFlagStates(store); | ||
expect(queryParams.length).toEqual(0); | ||
}); | ||
|
||
it('persists values of enabled experimental plugins', () => {}); | ||
|
||
it('persists flag states overridden by query params', async () => { | ||
store.overrideSelector(selectors.getEnabledExperimentalPlugins, []); | ||
getSearchSpy = spyOn(location, 'getSearch').and.returnValue([ | ||
{ | ||
key: 'defaultEnableDarkMode', | ||
value: 'true', | ||
}, | ||
]); | ||
const queryParams = await promiseGetFeatureFlagStates(store); | ||
expect(queryParams.length).toEqual(1); | ||
expect(queryParams[0].key).toEqual('defaultEnableDarkMode'); | ||
expect(queryParams[0].value).toEqual('true'); | ||
}); | ||
}); | ||
}); | ||
|
||
function promiseGetFeatureFlagStates(store: Store<State>): | ||
Promise<SerializableQueryParams> { | ||
return new Promise<SerializableQueryParams>((resolve) => { | ||
getFeatureFlagStates(store) | ||
.subscribe((queryParams) => { | ||
resolve(queryParams); | ||
}) | ||
.unsubscribe(); | ||
}); | ||
} |
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.