Skip to content

Commit

Permalink
Process processed value instead of old one, bring type safety to proc…
Browse files Browse the repository at this point in the history
…essSearchBar (#6676)

* Process processed value instead of old one

This fixes two cases:

1) A processor changes a values type to be an object or array. In that case we would previously not recurse down as the old value was still of another type.

2) A processor changes a field nested inside an object or array value. When recursing down, the options[key] would no longer match the value.

* Bring type safety to processSearchBar

Fixes an error where hideOnScroll was not being applied.

Also makes the whole function a lot more readable

* Added tests for processSearchBar

Co-authored-by: Guy Carmeli <guyca@users.noreply.github.com>
  • Loading branch information
danilobuerger and guyca authored Oct 21, 2020
1 parent 46e4d92 commit 055758d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
47 changes: 43 additions & 4 deletions lib/src/commands/OptionsProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { Store } from '../components/Store';
import { OptionProcessorsStore } from '../processors/OptionProcessorsStore';
import { Options, OptionsModalPresentationStyle } from '../interfaces/Options';
import { mock, when, anyString, instance, anyNumber, verify } from 'ts-mockito';
import { mock, when, instance, anyNumber, verify } from 'ts-mockito';
import { ColorService } from '../adapters/ColorService';
import { AssetService } from '../adapters/AssetResolver';
import { Deprecations } from './Deprecations';
Expand All @@ -25,7 +25,9 @@ describe('navigation options', () => {
const assetService = instance(mockedAssetService);

const mockedColorService = mock(ColorService) as ColorService;
when(mockedColorService.toNativeColor(anyString())).thenReturn(666);
when(mockedColorService.toNativeColor('red')).thenReturn(0xffff0000);
when(mockedColorService.toNativeColor('green')).thenReturn(0xff00ff00);
when(mockedColorService.toNativeColor('blue')).thenReturn(0xff0000ff);
const colorService = instance(mockedColorService);
optionProcessorsRegistry = new OptionProcessorsStore();
uut = new OptionsProcessor(
Expand Down Expand Up @@ -172,8 +174,8 @@ describe('navigation options', () => {
};
uut.processOptions(options, CommandName.SetRoot);
expect(options).toEqual({
statusBar: { backgroundColor: 666 },
topBar: { background: { color: 666 } },
statusBar: { backgroundColor: 0xffff0000 },
topBar: { background: { color: 0xff0000ff } },
});
});

Expand Down Expand Up @@ -279,4 +281,41 @@ describe('navigation options', () => {
}
);
});

it('transform searchBar bool to object', () => {
const options = { topBar: { searchBar: true as any } };
uut.processOptions(options, CommandName.SetRoot);
expect(options.topBar.searchBar).toStrictEqual({
visible: true,
hideOnScroll: false,
hideTopBarOnFocus: false,
obscuresBackgroundDuringPresentation: false,
backgroundColor: null,
tintColor: null,
placeholder: '',
});
});

it('transform searchBar bool to object and merges in deprecated values', () => {
const options = {
topBar: {
searchBar: true as any,
searchBarHiddenWhenScrolling: true,
hideNavBarOnFocusSearchBar: true,
searchBarBackgroundColor: 'red',
searchBarTintColor: 'green',
searchBarPlaceholder: 'foo',
},
};
uut.processOptions(options, CommandName.SetRoot);
expect(options.topBar.searchBar).toStrictEqual({
visible: true,
hideOnScroll: true,
hideTopBarOnFocus: true,
obscuresBackgroundDuringPresentation: false,
backgroundColor: 0xffff0000,
tintColor: 0xff00ff00,
placeholder: 'foo',
});
});
});
57 changes: 31 additions & 26 deletions lib/src/commands/OptionsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Store } from '../components/Store';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { ColorService } from '../adapters/ColorService';
import { AssetService } from '../adapters/AssetResolver';
import { Options } from '../interfaces/Options';
import { Options, OptionsSearchBar, OptionsTopBar } from '../interfaces/Options';
import { Deprecations } from './Deprecations';
import { OptionProcessorsStore } from '../processors/OptionProcessorsStore';
import { CommandName } from '../interfaces/CommandName';
Expand Down Expand Up @@ -49,7 +49,7 @@ export class OptionsProcessor {
}

private processObject(
objectToProcess: object,
objectToProcess: Record<string, any>,
parentOptions: object,
onProcess: (key: string, parentOptions: object) => void,
commandName: CommandName,
Expand All @@ -72,8 +72,9 @@ export class OptionsProcessor {

onProcess(key, parentOptions);

if (!isEqual(key, 'passProps') && (isObject(value) || isArray(value))) {
this.processObject(value, parentOptions, onProcess, commandName, objectPath);
const processedValue = objectToProcess[key];
if (!isEqual(key, 'passProps') && (isObject(processedValue) || isArray(processedValue))) {
this.processObject(processedValue, parentOptions, onProcess, commandName, objectPath);
}
});
}
Expand Down Expand Up @@ -138,30 +139,34 @@ export class OptionsProcessor {
}
}

private processSearchBar(key: string, value: any, options: Record<string, any>) {
if (isEqual(key, 'searchBar')) {
typeof value === 'boolean' && this.deprecations.onProcessOptions(key, options, '');
private processSearchBar(key: string, value: OptionsSearchBar | boolean, options: OptionsTopBar) {
if (key !== 'searchBar') {
return;
}

const deprecatedSearchBarOptions: OptionsSearchBar = {
visible: false,
hideOnScroll: options.searchBarHiddenWhenScrolling ?? false,
hideTopBarOnFocus: options.hideNavBarOnFocusSearchBar ?? false,
obscuresBackgroundDuringPresentation: false,
backgroundColor: options.searchBarBackgroundColor,
tintColor: options.searchBarTintColor,
placeholder: options.searchBarPlaceholder ?? '',
};

if (typeof value === 'boolean') {
// Deprecated
this.deprecations.onProcessOptions(key, options, '');

options[key] = {
...deprecatedSearchBarOptions,
visible: value,
};
} else {
options[key] = {
...options[key],
visible: options[key].visible ?? value,
hiddenWhenScrolling:
options[key].hiddenWhenScrolling ?? options.searchBarHiddenWhenScrolling ?? false,
hideTopBarOnFocus:
options[key].hideTopBarOnFocus ?? options.hideNavBarOnFocusSearchBar ?? false,
obscuresBackgroundDuringPresentation:
options[key].obscuresBackgroundDuringPresentation ?? false,
placeholder: options[key].placeholder ?? options.searchBarPlaceholder ?? '',
...deprecatedSearchBarOptions,
...value,
};
this.processColor(
'backgroundColor',
options[key].backgroundColor ?? options.searchBarBackgroundColor,
options[key]
);
this.processColor(
'tintColor',
options[key].tintColor ?? options.searchBarTintColor,
options[key]
);
}
}

Expand Down

0 comments on commit 055758d

Please sign in to comment.