Skip to content

Commit

Permalink
Resolve tabsAttachMode from default options (#6155)
Browse files Browse the repository at this point in the history
Default options were not taken into account when resolving tabsAttachMode property.

Co-authored-by: Yogev Ben David <yogev132@gmail.com>
  • Loading branch information
guyca and yogevbd authored Apr 23, 2020
1 parent 81d01a0 commit a4b2c76
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private ViewController createBottomTabs(LayoutNode node) {
node.id,
parse(typefaceManager, node.getOptions()),
new Presenter(activity, defaultOptions),
new BottomTabsAttacher(tabs, bottomTabsPresenter),
new BottomTabsAttacher(tabs, bottomTabsPresenter, defaultOptions),
bottomTabsPresenter,
new BottomTabPresenter(activity, tabs, new ImageLoader(), defaultOptions));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.reactnativenavigation.viewcontrollers.bottomtabs;

import androidx.annotation.VisibleForTesting;
import android.view.ViewGroup;

import com.reactnativenavigation.parse.Options;
Expand All @@ -9,19 +8,23 @@

import java.util.List;

import androidx.annotation.VisibleForTesting;

public class BottomTabsAttacher {
private final List<ViewController> tabs;
private final BottomTabsPresenter presenter;
private Options defaultOptions;
@VisibleForTesting
AttachMode attachStrategy;

public BottomTabsAttacher(List<ViewController> tabs, BottomTabsPresenter presenter) {
public BottomTabsAttacher(List<ViewController> tabs, BottomTabsPresenter presenter, Options defaultOptions) {
this.tabs = tabs;
this.presenter = presenter;
this.defaultOptions = defaultOptions;
}

void init(ViewGroup parent, Options resolved) {
attachStrategy = AttachMode.get(parent, tabs, presenter, resolved);
attachStrategy = AttachMode.get(parent, tabs, presenter, resolved.withDefaultOptions(defaultOptions));
}

void attach() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
package com.reactnativenavigation.viewcontrollers.bottomtabs;

import android.view.ViewGroup;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.TabsAttachMode;
import com.reactnativenavigation.presentation.BottomTabsPresenter;
import com.reactnativenavigation.viewcontrollers.*;
import com.reactnativenavigation.viewcontrollers.ViewController;

import org.junit.Test;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class BottomTabsAttacherTest extends BaseTest {

private BottomTabsAttacher uut;
private AttachMode mode;
private Options defaultOptions = new Options();
private List<ViewController> tabs;

@Override
public void beforeEach() {
mode = Mockito.mock(AttachMode.class);
uut = new BottomTabsAttacher(Collections.EMPTY_LIST, Mockito.mock(BottomTabsPresenter.class));
tabs = Arrays.asList(mock(ViewController.class), mock(ViewController.class));
uut = new BottomTabsAttacher(tabs, Mockito.mock(BottomTabsPresenter.class), defaultOptions);
uut.attachStrategy = mode;
}

@Test
public void init_defaultOptionsAreTakenIntoAccount() {
defaultOptions.bottomTabsOptions.tabsAttachMode = TabsAttachMode.ON_SWITCH_TO_TAB;
uut.init(Mockito.mock(ViewGroup.class), Options.EMPTY);
assertThat(uut.attachStrategy).isInstanceOfAny(OnSwitchToTab.class);
}

@Test
public void attach_delegatesToStrategy() {
uut.attach();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void applyChildOptions_resolvedOptionsAreUsed() {
child4 = createStack(pushedScreen);

tabs = new ArrayList<>(Collections.singletonList(child4));
tabsAttacher = new BottomTabsAttacher(tabs, presenter);
tabsAttacher = new BottomTabsAttacher(tabs, presenter, Options.EMPTY);

initialOptions.bottomTabsOptions.currentTabIndex = new Number(0);
Options resolvedOptions = new Options();
Expand Down Expand Up @@ -401,7 +401,7 @@ public void superCreateItems() {
tabs = createTabs();
presenter = spy(new BottomTabsPresenter(tabs, new Options()));
bottomTabPresenter = spy(new BottomTabPresenter(activity, tabs, ImageLoaderMock.mock(), new Options()));
tabsAttacher = spy(new BottomTabsAttacher(tabs, presenter));
tabsAttacher = spy(new BottomTabsAttacher(tabs, presenter, Options.EMPTY));
uut = createBottomTabs();

uut.setParentController(Mockito.mock(ParentController.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public void mergeOptions_AffectsOnlyComponentViewControllers() {
@NonNull
private BottomTabsController newTabs(List<ViewController> tabs) {
BottomTabsPresenter bottomTabsPresenter = new BottomTabsPresenter(tabs, new Options());
return new BottomTabsController(activity, tabs, childRegistry, eventEmitter, imageLoaderMock, "tabsController", new Options(), new Presenter(activity, new Options()), new BottomTabsAttacher(tabs, bottomTabsPresenter), bottomTabsPresenter, new BottomTabPresenter(activity, tabs, ImageLoaderMock.mock(), new Options())) {
return new BottomTabsController(activity, tabs, childRegistry, eventEmitter, imageLoaderMock, "tabsController", new Options(), new Presenter(activity, new Options()), new BottomTabsAttacher(tabs, bottomTabsPresenter, Options.EMPTY), bottomTabsPresenter, new BottomTabPresenter(activity, tabs, ImageLoaderMock.mock(), new Options())) {
@NonNull
@Override
protected BottomTabs createBottomTabs() {
Expand Down

0 comments on commit a4b2c76

Please sign in to comment.