Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add autoStart feature #138

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Prev Previous commit
Next Next commit
refactor(test): refactoring auto-start tests
  • Loading branch information
Ricardo Arrobo committed Jul 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 6fdbc647b848dc52d44b981d5abd28d165559650
4 changes: 2 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -76,8 +76,8 @@
"peerDependencies": {
"react": ">=16.8.0",
"react-native": ">=0.50.0",
"react-native-svg": ">=12.1.0",
"react-native-mmkv-storage": ">=0.9.1"
"react-native-mmkv-storage": ">=0.9.1",
"react-native-svg": ">=12.1.0"
},
"peerDependenciesMeta": {
"react": {
2 changes: 1 addition & 1 deletion package/src/helpers/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MMKVLoader } from 'react-native-mmkv-storage';
import { MMKVLoader } from "react-native-mmkv-storage";

const storage = new MMKVLoader().initialize();

12 changes: 6 additions & 6 deletions package/src/lib/SpotlightTour.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMMKVStorage } from 'react-native-mmkv-storage';
import storage from '../helpers/storage';
import hash from 'object-hash';

import hash from "object-hash";
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState, useEffect } from "react";
import { ColorValue, LayoutRectangle } from "react-native";
import { useMMKVStorage } from "react-native-mmkv-storage";

import { ChildFn, isChildFunction } from "../helpers/common";
import storage from "../helpers/storage";

import {
AutoStartOptions,
@@ -123,7 +123,7 @@ export const SpotlightTourProvider = forwardRef<SpotlightTour, SpotlightTourProv

const [current, setCurrent] = useState<number>();
const [spot, setSpot] = useState(ZERO_SPOT);
const [tourId, setTourId] = useMMKVStorage('tourId', storage, '');
const [tourId, setTourId] = useMMKVStorage("tourId", storage, "");

const overlay = useRef<TourOverlayRef>({
hideTooltip: () => Promise.resolve({ finished: false }),
@@ -151,7 +151,7 @@ export const SpotlightTourProvider = forwardRef<SpotlightTour, SpotlightTourProv
}, [renderStep, onStart]);

const startOnce = useCallback(() => {
if(!tourId){
if (!tourId) {
setTourId(hash(steps));
renderStep(0);
onStart?.();
21 changes: 7 additions & 14 deletions package/test/integration/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "@testing-library/jest-native/extend-expect";

import { expect as jestExpect } from "@jest/globals";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { expect, TypeFactories } from "@stackbuilders/assertive-ts";
import { fireEvent, render, waitFor } from "@testing-library/react-native";
import React from "react";
@@ -291,37 +290,31 @@ describe("[Integration] index.test.tsx", () => {
describe("when the autoStart property is set to never", () => {
it("the overlay is not shown", async () => {
const { getByText, queryByTestId } = render(<TestScreen autoStart="never" />);
await waitFor(() => getByText("Start"));
await waitFor(() => expect(getByText("Start")).toBePresent());
expect(queryByTestId("Overlay View")).toBeNull();
});
});

describe("when the autoStart property is set to always", () => {
it("shows the overlay view", async () => {
const { getByTestId } = render(<TestScreen autoStart="always" />);
await waitFor(() => getByTestId("Overlay View"));
await waitFor(() => expect(getByTestId("Overlay View")).toBePresent());
});
});

describe("when the autoStart property is set to once", () => {
describe("when the device is not registered", () => {
it("shows the overlay view", async () => {
const { getByTestId } = render(<TestScreen autoStart="once" />);
waitFor(() => {
jestExpect(AsyncStorage.getItem).toHaveBeenCalled();
jestExpect(AsyncStorage.setItem).toHaveBeenCalled();
});
await waitFor(() => getByTestId("Overlay View"));
it("shows the overlay view", async() => {
const { getByTestId } = render(<TestScreen autoStart="once" />);
await waitFor(() => expect(getByTestId("Overlay View")).toBePresent());
});
});
describe("when the device is already registered", () => {
it("the overlay is not shown", async () => {
await AsyncStorage.setItem("12345", "true");
const { queryByTestId } = render(<TestScreen autoStart="once" />);
waitFor(() => {
jestExpect(AsyncStorage.getItem).toHaveBeenCalled();
await waitFor(() => {
expect(queryByTestId("Overlay View")).toBeNull();
});
expect(queryByTestId("Overlay View")).toBeNull();
});
});
});
17 changes: 13 additions & 4 deletions package/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable max-classes-per-file */
import { useState } from "react";
import { Animated, LayoutRectangle } from "react-native";
import { MMKVInstance } from "react-native-mmkv-storage";

import {
isAnimatedTimingInterpolation,
@@ -137,10 +139,17 @@ jest
},
};
})
.mock("@react-native-async-storage/async-storage", () =>
AsyncStorageMock)

.mock("react-native-device-info", () => ({ getUniqueId: () => "12345" }));
/* eslint-disable @typescript-eslint/no-unused-vars */
.mock("react-native-mmkv-storage", () => ({
MMKVLoader: jest.fn().mockImplementation(() => ({
initialize: () => jest.fn(),
})),
useMMKVStorage: (_key: string, _storage: MMKVInstance, defaultValue: string) => {
const [value, setValue] = useState(defaultValue);
const setMockValue = (newValue: string): void => setValue(newValue);
return [value, setMockValue];
},
}));

afterEach(() => {
jest.resetAllMocks();
5 changes: 3 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -3306,6 +3306,7 @@ __metadata:
peerDependencies:
react: ">=16.8.0"
react-native: ">=0.50.0"
react-native-mmkv-storage: ">=0.9.1"
react-native-svg: ">=12.1.0"
peerDependenciesMeta:
react:
@@ -13904,11 +13905,11 @@ __metadata:

"typescript@patch:typescript@^4.0.0#~builtin<compat/typescript>, typescript@patch:typescript@^4.9.5#~builtin<compat/typescript>":
version: 4.9.5
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=ad5954"
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=23ec76"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 8f6260acc86b56bfdda6004bc53f32ea548f543e8baef7071c8e34d29d292f3e375c8416556c8de10b24deef6933cd1c16a8233dc84a3dd43a13a13265d0faab
checksum: ab417a2f398380c90a6cf5a5f74badd17866adf57f1165617d6a551f059c3ba0a3e4da0d147b3ac5681db9ac76a303c5876394b13b3de75fdd5b1eaa06181c9d
languageName: node
linkType: hard