Skip to content

Commit

Permalink
Add screenPopped e2e (#6163)
Browse files Browse the repository at this point in the history
* Add screenPopped e2e

* empty commit

* Fix popped screen event on iOS

* fix e2e on iOS

Co-authored-by: yogevbd <yogev132@gmail.com>
  • Loading branch information
guyca and yogevbd authored Apr 27, 2020
1 parent 1f887c8 commit 2f31a2f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions e2e/Stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ describe('Stack', () => {
await expect(elementByLabel('didDisappear')).toBeVisible();
});

it('Screen popped event', async () => {
await elementById(TestIDs.PUSH_LIFECYCLE_BTN).tap();
await elementById(TestIDs.SCREEN_POPPED_BTN).tap();
await expect(elementByLabel('Screen popped event')).toBeVisible();
});

it('unmount is called on pop', async () => {
await elementById(TestIDs.PUSH_LIFECYCLE_BTN).tap();
await elementById(TestIDs.POP_BTN).tap();
Expand Down
2 changes: 1 addition & 1 deletion lib/ios/StackControllerDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ - (instancetype)initWithEventEmitter:(RNNEventEmitter *)eventEmitter {
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController.viewControllers indexOfObject:_presentedViewController] < 0) {
if (_presentedViewController && ![navigationController.viewControllers containsObject:_presentedViewController]) {
[self sendScreenPoppedEvent:_presentedViewController];
}

Expand Down
15 changes: 13 additions & 2 deletions playground/src/screens/LifecycleScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Screens = require('./Screens');
const {
PUSH_TO_TEST_DID_DISAPPEAR_BTN,
DISMISS_MODAL_BTN,
SCREEN_POPPED_BTN,
POP_BTN
} = require('../testIDs');

Expand All @@ -25,6 +26,7 @@ class LifecycleScreen extends React.Component {

constructor(props) {
super(props);
this.showUnmountAndDisappearAlerts = true;
Navigation.events().bindComponent(this);
}

Expand All @@ -33,12 +35,12 @@ class LifecycleScreen extends React.Component {
}

componentDidDisappear() {
alert('didDisappear'); // eslint-disable-line no-alert
this.showUnmountAndDisappearAlerts && alert('didDisappear'); // eslint-disable-line no-alert
}

componentWillUnmount() {
setTimeout(() => {
alert('componentWillUnmount'); // eslint-disable-line no-alert
this.showUnmountAndDisappearAlerts && alert('componentWillUnmount'); // eslint-disable-line no-alert
}, 100);
}

Expand All @@ -50,6 +52,7 @@ class LifecycleScreen extends React.Component {
return (
<Root componentId={this.props.componentId} footer={this.state.text}>
<Button label='Push to test didDisappear' testID={PUSH_TO_TEST_DID_DISAPPEAR_BTN} onPress={this.push} />
{!this.props.isModal && <Button label='Screen popped events' testID={SCREEN_POPPED_BTN} onPress={this.screenPoppedEvent} />}
{this.renderCloseButton()}
</Root>
);
Expand All @@ -60,6 +63,14 @@ class LifecycleScreen extends React.Component {
<Button label='Pop' testID={POP_BTN} onPress={this.pop} />;

push = () => Navigation.push(this, Screens.Pushed);
screenPoppedEvent = async () => {
this.showUnmountAndDisappearAlerts = false;
const unregister = Navigation.events().registerScreenPoppedListener((event) => {
alert('Screen popped event')
unregister.remove();
});
await Navigation.pop(this);
}
pop = () => Navigation.pop(this);
dismiss = () => Navigation.dismissModal(this);
}
Expand Down
1 change: 1 addition & 0 deletions playground/src/testIDs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = {
PUSH_CONTEXT_SCREEN_BUTTON: `PUSH_CONTEXT_SCREEN_BUTTON`,
PUSH_OPTIONS_BUTTON: `PUSH_OPTIONS_BUTTON`,
PUSH_DEFAULT_OPTIONS_BUTTON: `PUSH_DEFAULT_OPTIONS_BUTTON`,
SCREEN_POPPED_BTN: 'SCREEN_POPPED_BTN',
SHOW_REDBOX_BUTTON: `SHOW_REDBOX_BUTTON`,
ORIENTATION_BTN: `ORIENTATION_BUTTON`,
PROVIDED_ID: `PROVIDED_ID`,
Expand Down

0 comments on commit 2f31a2f

Please sign in to comment.