Skip to content

Commit

Permalink
added title and subtitle testIds (#7866)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: d4vidi <[email protected]>
  • Loading branch information
Niryo and d4vidi authored May 26, 2024
1 parent dde5643 commit f23bc2b
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 7 deletions.
41 changes: 38 additions & 3 deletions e2e/Stack.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import jestExpect from 'expect';
import Utils from './Utils';
import TestIDs from '../playground/src/testIDs';
import Android from './AndroidUtils';

const { elementByLabel, elementById, sleep } = Utils;

const driver = {
root: {
navToTitleAndSubtitle: async () => {
await elementById(TestIDs.PUSH_TITLE_WITH_SUBTITLE).tap();
return driver.titleWithSubtitle;
},
},

titleWithSubtitle: {
titleId: `${TestIDs.TOPBAR_ID}.title`,
subtitleId: `${TestIDs.TOPBAR_ID}.subtitle`,
title: () => elementById(driver.titleWithSubtitle.titleId),
titleByLabel: () => elementByLabel('Title'),
subtitle: () => elementById(driver.titleWithSubtitle.subtitleId),
subtitleByLabel: () => elementByLabel('Subtitle'),
},
};

describe('Stack', () => {
beforeEach(async () => {
await device.launchApp({ newInstance: true });
Expand Down Expand Up @@ -78,9 +97,25 @@ describe('Stack', () => {
});

it('push title with subtitle', async () => {
await elementById(TestIDs.PUSH_TITLE_WITH_SUBTITLE).tap();
await expect(elementByLabel('Title')).toBeVisible();
await expect(elementByLabel('Subtitle')).toBeVisible();
const innerDriver = await driver.root.navToTitleAndSubtitle();
await expect(innerDriver.titleByLabel()).toBeVisible();
await expect(innerDriver.subtitleByLabel()).toBeVisible();
});

it('push title & subtitle with derived test IDs', async () => {
const innerDriver = await driver.root.navToTitleAndSubtitle();
await expect(innerDriver.title()).toBeVisible();
await expect(innerDriver.subtitle()).toBeVisible();
});

it.e2e('push title & subtitle with derived test IDs (strict e2e)', async () => {
const innerDriver = await driver.root.navToTitleAndSubtitle();

const titleAttr = await innerDriver.titleByLabel().getAttributes();
jestExpect(titleAttr.identifier).toEqual(innerDriver.titleId);

const subtitleAttr = await innerDriver.subtitleByLabel().getAttributes();
jestExpect(subtitleAttr.identifier).toEqual(innerDriver.subtitleId);
});

it.e2e('screen lifecycle', async () => {
Expand Down
9 changes: 6 additions & 3 deletions lib/Mock/Components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export const TopBar = class extends Component<TopBarProps> {

render() {
const topBarOptions = this.props.topBarOptions;
const topbarTestId = topBarOptions?.testID;
const titleTestId = topbarTestId ? { testID: `${topbarTestId}.title` } : {};
const subtitleTestId = topbarTestId ? { testID: `${topbarTestId}.subtitle` } : {};
if (topBarOptions?.visible === false) return null;
else {
const component = topBarOptions?.title?.component;
return (
<View testID={topBarOptions?.testID}>
<Text>{topBarOptions?.title?.text}</Text>
<Text>{topBarOptions?.subtitle?.text}</Text>
<View testID={topbarTestId}>
<Text {...titleTestId}>{topBarOptions?.title?.text}</Text>
<Text {...subtitleTestId}>{topBarOptions?.subtitle?.text}</Text>
{this.renderButtons(topBarOptions?.leftButtons)}
{this.renderButtons(topBarOptions?.rightButtons)}
{component &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public void setSubtitleAlignment(Alignment alignment) {

public void setTestId(String testId) {
setTag(testId);
titleAndButtonsContainer.setTestId(testId);
}

public void setTitleTextColor(@ColorInt int color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class TitleAndButtonsContainer(context: Context) : ViewGroup(context) {
clearComponent()
}

fun setTestId(testId: String) {
titleSubTitleBar.setTestId(testId)
}

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
val titleComponent = getTitleComponent()
val isCenter = titleComponentAlignment == Alignment.Center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class TitleSubTitleLayout(context: Context) : LinearLayout(context) {
}
}

fun setTestId(testId: String) {
if (testId.isEmpty()) {
this.titleTextView.tag = null
this.subTitleTextView.tag = null
} else {
this.titleTextView.tag = "$testId.title"
this.subTitleTextView.tag = "$testId.subtitle"
}
}

fun getTitle() = (this.titleTextView.text ?: "").toString()

fun clear() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.reactnativenavigation.views

import android.app.Activity
import android.view.ViewGroup
import android.widget.FrameLayout
import com.reactnativenavigation.BaseTest
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleSubTitleLayout
import org.assertj.core.api.AssertionsForInterfaceTypes.*
import org.junit.Test

private const val UUT_WIDTH = 1000
private const val UUT_HEIGHT = 100


class TitleSubTitleLayoutTest : BaseTest() {
private val testId = "mock-testId"

lateinit var uut: TitleSubTitleLayout
private lateinit var activity: Activity

override fun beforeEach() {
super.beforeEach()
setup()
}

private fun setup() {
activity = newActivity()

uut = TitleSubTitleLayout(activity)

activity.setContentView(FrameLayout(activity).apply {
addView(uut, ViewGroup.LayoutParams(UUT_WIDTH, UUT_HEIGHT))
})
idleMainLooper()
}

@Test
fun `should set Test ID canonically`(){
uut.setTestId(testId)
assertThat(uut.getTitleTxtView().tag).isEqualTo("$testId.title")
assertThat(uut.getSubTitleTxtView().tag).isEqualTo("$testId.subtitle")
}

@Test
fun `should clear test ID`() {
uut.setTestId(testId)
uut.setTestId("")
assertThat(uut.getTitleTxtView().tag).isNull()
assertThat(uut.getSubTitleTxtView().tag).isNull()
}
}
2 changes: 2 additions & 0 deletions lib/ios/RNNTitleViewHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

@property(nonatomic, strong) RNNTitleOptions *titleOptions;
@property(nonatomic, strong) RNNSubtitleOptions *subtitleOptions;
@property(nonatomic, strong) Text *parentTestID;

- (instancetype)initWithTitleViewOptions:(RNNOptions *)titleOptions
subTitleOptions:(RNNOptions *)subtitleOptions
parentTestID:(Text *)parentTestID
viewController:(UIViewController *)viewController;

- (void)setup;
Expand Down
14 changes: 13 additions & 1 deletion lib/ios/RNNTitleViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ @implementation RNNTitleViewHelper

- (instancetype)initWithTitleViewOptions:(RNNTitleOptions *)titleOptions
subTitleOptions:(RNNSubtitleOptions *)subtitleOptions
parentTestID:(Text *)parentTestID
viewController:(UIViewController *)viewController {
self = [super init];
if (self) {
self.viewController = viewController;
self.titleOptions = titleOptions;
self.subtitleOptions = subtitleOptions;
self.parentTestID = parentTestID;
}
return self;
}
Expand Down Expand Up @@ -81,7 +83,7 @@ - (void)setup {
if (self.title) {
self.titleView.titleLabel = [self setupTitle];
}

[self centerTitleView:navigationBarBounds
titleLabel:self.titleView.titleLabel
subtitleLabel:self.titleView.subtitleLabel];
Expand Down Expand Up @@ -135,6 +137,11 @@ - (UILabel *)setupSubtitle {
subtitleLabel.textColor = color;
}

if (_parentTestID && _parentTestID.hasValue && ((NSString *)_parentTestID.get).length > 0) {
subtitleLabel.accessibilityIdentifier =
[NSString stringWithFormat:@"%@.subtitle", _parentTestID.get];
}

[self.titleView addSubview:subtitleLabel];

return subtitleLabel;
Expand Down Expand Up @@ -174,6 +181,11 @@ - (UILabel *)setupTitle {
titleLabel.textColor = color;
}

if (_parentTestID && _parentTestID.hasValue && ((NSString *)_parentTestID.get).length > 0) {
titleLabel.accessibilityIdentifier =
[NSString stringWithFormat:@"%@.title", _parentTestID.get];
}

[self.titleView addSubview:titleLabel];

return titleLabel;
Expand Down
1 change: 1 addition & 0 deletions lib/ios/TopBarTitlePresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ - (void)setTitleViewWithSubtitle:(RNNTopBarOptions *)options {
_titleViewHelper =
[[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.title
subTitleOptions:options.subtitle
parentTestID:options.testID
viewController:self.boundViewController];

if (options.title.text.hasValue) {
Expand Down
1 change: 1 addition & 0 deletions playground/src/screens/StackScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default class StackScreen extends React.Component<NavigationProps> {
name: Screens.Pushed,
options: {
topBar: {
testID: testIDs.TOPBAR_ID,
title: {
text: 'Title',
},
Expand Down
1 change: 1 addition & 0 deletions playground/src/testIDs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const testIDs = {
CUSTOM_BACK_BTN: 'CUSTOM_BACK_BUTTON',
PUSH_CUSTOM_BACK_BTN: 'PUSH_CUSTOM_BACK_BTN',
PUSH_TITLE_WITH_SUBTITLE: 'PUSH_TITLE_WITH_SUBTITLE',
TOPBAR_ID: 'TOPBAR_ID',
BACK_BUTTON: 'BACK_BUTTON',
TOGGLE_BACK: 'TOGGLE_BACK',
SWITCH_TAB_BY_INDEX_BTN: 'SWITCH_TAB_BY_INDEX_BTN',
Expand Down

0 comments on commit f23bc2b

Please sign in to comment.