@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
63 lines (51 loc) • 1.77 kB
JavaScript
import React from "react";
import { View } from "react-native";
import { render } from "@testing-library/react-native";
import { ActionsProvider } from "../../../../quick-brick-core/App/ActionsProvider/ActionsProvider";
import { useActions } from "../";
const FavouritesContext = React.createContext(null);
const FavouritesProvider = (Component) =>
function FavouritesProviderWrapper(props) {
return (
<FavouritesContext.Provider value="favourites">
<Component {...props} />
</FavouritesContext.Provider>
);
};
const ContextConsumer = ({ actionId }: { actionId: string }) => {
const context = useActions(actionId);
return <View favourites={context} testID="context-consumer-view" />;
};
describe("Actions Provider hook", () => {
it("subscribes to correct action", () => {
const plugins = [
{ identifier: "fake plugin" },
{
identifier: "favs-action",
module: {
actionName: "faves",
contextProvider: FavouritesProvider,
context: FavouritesContext,
},
},
];
const wrapper = render(
<ActionsProvider plugins={plugins}>
<ContextConsumer actionId={"favs-action"} />
</ActionsProvider>
);
const { getByTestId } = wrapper;
const consumerView = getByTestId("context-consumer-view");
expect(consumerView.props.favourites).toEqual("favourites");
});
it("handles empty plugins", () => {
const wrapper = render(
<ActionsProvider plugins={[]}>
<ContextConsumer actionId={"favs-action"} />
</ActionsProvider>
);
const { getByTestId } = wrapper;
const consumerView = getByTestId("context-consumer-view");
expect(consumerView.props.favourites).toBe(undefined);
});
});