npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-detox-context

v0.3.1

Published

Easily pass data from your Detox e2e tests to the JS layer

Downloads

3,816

Readme

react-native-detox-context

Easily pass data from your Detox e2e tests to the JS layer of your React Native application.

  • Minimal setup.
  • Dynamically pass strings, booleans or objects.
  • Android and iOS support.
  • Access context data in any of your React components.
  • Written in TypeScript.

Example Detox e2e test

import { contextArgs } from 'react-native-detox-context';

describe('Some Test suite', () => {
    it('should test my feature', async () => {
        // This args will automatically be available in your 
        // React Native app during the e2e test.
        const args = contextArgs({
            someString: 'a string',
            someNumber: 45,
            someBool: true,
            someObject: { prop1: 'value1', prop2: 'value2' },
        });

        await device.launchApp({ 
            newInstance: true, 
            launchArgs: args 
        });
  });
});

Example RN app

import React from 'react';
import { withDetoxContext, DetoxContext } from 'react-native-detox-context';

function App() {
  console.log('isAutomatedTest:', DetoxContext.isAutomatedTest); // true during e2e tests

  console.log('String value from context:', DetoxContext.getString('someString'));
  console.log('Number value from context:', DetoxContext.getNumber('someNumber'));
  console.log('Boolean value from context:', DetoxContext.getBool('someBool'));
  console.log('Object value from context:', DetoxContext.getObject<SomeType>('someObject'));

  return <View />;
}

export default withDetoxContext(App);

Motivation

When writing end-to-end tests with Detox, there usually comes a time when you need to supply some flag and/or data to your React components (such as test data, or some flag to disable a feature, or perhaps authentication details etc). The standard way to achieve this in Detox is by using its Launch Arguments API. This API works great (and react-native-detox-context makes use of it), but it involves developers having to work in native code, and understand things like working with Android Bundles and iOS Process Info arguments. Not only that, you usually end up having to revisit your native code each time you have a new piece of data you'd like to send.

The react-native-detox-context library removes the need for native knowlegde and greatly simplifies how you pass data to your React Native application. You set it up once at the native layer and never need to revisit it again.

Installation

Note: These steps assume you have a React Native project that's already setup with Detox.

yarn add react-native-detox-context

JS setup

Wrap your root component in the withDetoxContext higher order component:

import { withDetoxContext } from 'react-native-detox-context';

function App() {
    ...
}

export default withDetoxContext(App);

Android setup

Update your MainActivity.java with the following changes:

package com.somepackage;

import com.facebook.react.ReactActivity;
+ import android.os.Bundle;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.hudl.rn.detoxcontext.DetoxContext;

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "somecomponent";
  }

+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected Bundle getLaunchOptions() {
+        return DetoxContext.processLaunchArgs(getIntent());
+      }
+    };
+  }
}

iOS setup

First run a pod install (e.g. npx pod-install) so that the react-native-detox-context library gets linked.

Secondly update your AppDelegate.m with these changes:

  1. Add the following import
@import react_native_detox_context;
  1. Update the initialProperties param:

// Before (initialProperties is nil)
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                 moduleName:@"somecomponent"
                                          initialProperties:nil];

// After (i.e. update the initialProperties from nil to [DetoxContext processLaunchArgs])
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                 moduleName:@"somecomponent"
                                          initialProperties:[DetoxContext processLaunchArgs]];

Usage

Send arguments from e2e tests

Use the contextArgs function, passing an object. The object can have string values, boolean values or object values.

Pass the object returned from contextArgs() to the launchArgs prop in the Detox device.launchApp function.

import { contextArgs } from 'react-native-detox-context';

describe('Some Test suite', () => {
    it('should test my feature', async () => {
        // This args will automatically be available in your 
        // React Native app during the e2e test.
        const args = contextArgs({
            someString: 'a string',
            someNumber: 45,
            someBool: true,
            someObject: { prop1: 'value1', prop2: 'value2' },
        });

        await device.launchApp({ 
            newInstance: true, 
            launchArgs: args 
        });
  });
});

Use arguments in your React Native application

import { withDetoxContext, DetoxContext } from 'react-native-detox-context';

interface SomeType {
    prop1: string;
    prop2: string;
}

function App() {
    console.log('isAutomatedTest:', DetoxContext.isAutomatedTest); // true during e2e tests

    // If the key you pass is present in the Detox Context then the value will 
    // be returned, otherwise 'undefined' will be returned.
    //
    // You can use these functions anywhere in your component tree.
    console.log('String value', DetoxContext.getString('someString'));
    console.log('Number value', DetoxContext.getNumber('someNumber'));
    console.log('Boolean value', DetoxContext.getBool('someBool'));
    console.log('Object value', DetoxContext.getObject<SomeType>('someObject'));
    
    ...
}

export default withDetoxContext(App);

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT