;
-
-/**
- * Perform a reset for the area counter (default-area by default).
- */
-export function manuallyResetPromiseCounter(area?: string): void;
-
-/**
- * Decrement the area counter (default-area by default).
- */
-export function manuallyDecrementPromiseCounter(area?: string): void;
-
-/**
- * Increment the area counter (default-area by default).
- */
-export function manuallyIncrementPromiseCounter(area?: string): void;
-
-/**
- * Configuration contract: user can setup areas (display more than one spinner) or delay when
- * the spinner is shown (this is useful when a user has a fast connection, to avoid unneccessary flickering)
- */
-
-interface Config {
- area?: string;
- delay?: number;
-}
-
-/**
- * It wraps a given React component into a new component that adds properties to watch
- * pending promises (HOC).
- * @param component Input component to be wrapped.
- * @returns It returns a new component that extends the input one.
- */
-
-export interface ComponentToWrapProps {
- config: Config;
- promiseInProgress: boolean;
-}
-
-export interface TrackerHocProps {
- config?: Config;
-}
-
-export function promiseTrackerHoc(
- component: React.ComponentType
-): React.ComponentType
;
-
-/**
- * React Promise Tracker custom hook, this hook will expose a promiseInProgress boolean flag.
- *
- * @param configuration (optional can be null).
- * @returns promiseInProgressFlag.
- */
-export function usePromiseTracker(
- outerConfig?: Config
-): { promiseInProgress: boolean };
+// Type definitions for react-promise-tracker
+// Project: https://github.com/Lemoncode/react-promise-tracker
+// Definitions by: Lemoncode
+
+import * as React from "react";
+
+/**
+ * It tracks a promise while in pending state.
+ * @param promise Input promise to be tracked.
+ * @returns It returns the same promise as input.
+ */
+export function trackPromise(promise: Promise, area?: string): Promise;
+
+/**
+ * Perform a reset for the area counter (default-area by default).
+ */
+export function manuallyResetPromiseCounter(area?: string): void;
+
+/**
+ * Decrement the area counter (default-area by default).
+ */
+export function manuallyDecrementPromiseCounter(area?: string): void;
+
+/**
+ * Increment the area counter (default-area by default).
+ */
+export function manuallyIncrementPromiseCounter(area?: string): void;
+
+/**
+ * Configuration contract: user can setup areas (display more than one spinner) or delay when
+ * the spinner is shown (this is useful when a user has a fast connection, to avoid unneccessary flickering)
+ */
+
+interface Config {
+ area?: string;
+ delay?: number;
+}
+
+/**
+ * React Promise Tracker custom hook, this hook will expose a promiseInProgress boolean flag.
+ *
+ * @param configuration (optional can be null).
+ * @returns promiseInProgressFlag.
+ */
+export function usePromiseTracker(
+ outerConfig?: Config
+): { promiseInProgress: boolean };
diff --git a/src/index.js b/src/index.js
index bc4a301..ccb14e9 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,8 +1,7 @@
-export {
- trackPromise,
- manuallyResetPromiseCounter,
- manuallyDecrementPromiseCounter,
- manuallyIncrementPromiseCounter,
-} from './trackPromise';
-export { promiseTrackerHoc } from './trackerHoc';
-export { usePromiseTracker } from './trackerHook';
+export {
+ trackPromise,
+ manuallyResetPromiseCounter,
+ manuallyDecrementPromiseCounter,
+ manuallyIncrementPromiseCounter,
+} from './trackPromise';
+export { usePromiseTracker } from './trackerHook';
diff --git a/src/trackerHoc.js b/src/trackerHoc.js
deleted file mode 100644
index 0066ab9..0000000
--- a/src/trackerHoc.js
+++ /dev/null
@@ -1,80 +0,0 @@
-import React, { Component } from 'react';
-import {
- emitter,
- getCounter,
- promiseCounterUpdateEventId,
-} from './trackPromise';
-import { setupConfig } from './setupConfig';
-
-// Props:
-// config: {
-// area: // can be null|undefined|'' (will default to DefaultArea) or area name
-// delay: // Wait Xms to display the spinner (fast connections scenario avoid blinking)
-// default value 0ms
-// }
-export const promiseTrackerHoc = ComponentToWrap => {
- return class promiseTrackerComponent extends Component {
- constructor(props) {
- super(props);
-
- this.state = {
- promiseInProgress: false,
- internalPromiseInProgress: false,
- config: setupConfig(props.config),
- };
-
- this.notifyPromiseInProgress = this.notifyPromiseInProgress.bind(this);
- this.updateProgress = this.updateProgress.bind(this);
- this.subscribeToCounterUpdate = this.subscribeToCounterUpdate.bind(this);
- }
-
- notifyPromiseInProgress() {
- this.state.config.delay === 0
- ? this.setState({ promiseInProgress: true })
- : setTimeout(() => {
- const progress = Boolean(getCounter(this.state.config.area) > 0);
- this.setState({ promiseInProgress: progress });
- }, this.state.config.delay);
- }
-
- updateProgress(progress, afterUpdateCallback) {
- this.setState(
- { internalPromiseInProgress: progress },
- afterUpdateCallback
- );
-
- !progress
- ? this.setState({ promiseInProgress: false })
- : this.notifyPromiseInProgress();
- }
-
- subscribeToCounterUpdate() {
- emitter.on(promiseCounterUpdateEventId, (anyPromiseInProgress, area) => {
- if (this.state.config.area === area) {
- this.updateProgress(anyPromiseInProgress);
- }
- });
- }
-
- componentDidMount() {
- this.updateProgress(
- Boolean(getCounter(this.state.config.area) > 0),
- this.subscribeToCounterUpdate
- );
- }
-
- componentWillUnmount() {
- emitter.off(promiseCounterUpdateEventId);
- }
-
- render() {
- return (
-
- );
- }
- };
-};
diff --git a/src/trackerHoc.test.js b/src/trackerHoc.test.js
deleted file mode 100644
index c565660..0000000
--- a/src/trackerHoc.test.js
+++ /dev/null
@@ -1,632 +0,0 @@
-import React from 'react';
-import { render, screen } from '@testing-library/react';
-import { promiseTrackerHoc } from './trackerHoc';
-import * as trackPromiseAPI from './trackPromise';
-import { defaultArea } from './constants';
-
-const TestSpinnerComponent = (props) => (
- props: {JSON.stringify(props, null, 4)}
-);
-
-describe('trackerHoc', () => {
- describe('Initial Status', () => {
- it('should render component with trackedPromiseInProgress equals false and area equals "default-area" when render promiseTrackerHoc without props', () => {
- // Arrange
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false, area equals "default-area" and customProp equals "test" when feeding customProp equals "test"', () => {
- // Arrange
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false and area equals "testArea" when feeding area equals "testArea"', () => {
- // Arrange
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = false;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false to different area', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = false;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 0 and emit event with progress equals true', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = true;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals true to different area', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = true;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 1', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = true;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals true to different area', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = true;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals false', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = false;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 1 and emit event with progress equals false to different area', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = false;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render();
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false and area equals "testArea" when feeding area equals "testArea" and delay equals 300', () => {
- // Arrange
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = false;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals false to different area and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = false;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals true when counter is 0 and emit event with progress equals true and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest
- .fn()
- .mockReturnValueOnce(0)
- .mockReturnValueOnce(1);
-
- const progress = true;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
- const setTimeoutStub = jest
- .spyOn(window, 'setTimeout')
- .mockImplementation((callback) => callback());
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 0 and emit event with progress equals true to different area and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(0);
-
- const progress = true;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals true and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = true;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals true to different area and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = true;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals false and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = false;
- const area = defaultArea;
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
-
- it('should render component with trackedPromiseInProgress equals false when counter is 1 and emit event with progress equals false to different area and delay equals 300', () => {
- // Arrange
- trackPromiseAPI.getCounter = jest.fn().mockReturnValue(1);
-
- const progress = false;
- const area = 'otherArea';
- const emitterStub = jest
- .spyOn(trackPromiseAPI.emitter, 'on')
- .mockImplementation((id, callback) => callback(progress, area));
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
-
- // Assert
- const { asFragment } = render(
-
- );
-
- expect(trackPromiseAPI.getCounter).toHaveBeenCalled();
- expect(asFragment()).toMatchSnapshot();
- });
- });
-
- describe('Handling delay timeouts', () => {
- beforeEach(() => {
- jest.useFakeTimers();
- });
-
- afterAll(() => {
- jest.useRealTimers();
- });
-
- it('should render NO SPINNER when counter is 1 but delay is set to 300 (before timing out)', async () => {
- // Arrange
- const TestSpinnerComponent = (props) => {
- return (
-
- {props.promiseInProgress ?
SPINNER
: NO SPINNER
}
-
- );
- };
-
- const getCounterStub = jest.spyOn(trackPromiseAPI, 'getCounter');
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
- render();
-
- // Check very beginning (no promises going on) NO SPINNER is shown
- // TODO: this assert could be skipped (move to another test)
- expect(screen.getByText('NO SPINNER')).toBeInTheDocument();
- expect(getCounterStub).toHaveBeenCalled();
-
- // Assert
- // This promise will resolved after 1 seconds, by doing this
- // we will be able to test 2 scenarios:
- // [0] first 200ms spinner won't be shown (text NOSPINNER)
- // [1] after 200ms spinner will be shown (text SPINNER)
- // [2] after 1000ms spinner will be hidded again (text NOSPINNER)
-
- const myFakePromise = new Promise((resolve) => {
- setTimeout(() => {
- resolve(true);
- }, 1000);
- });
-
- trackPromiseAPI.trackPromise(myFakePromise);
-
- jest.advanceTimersByTime(100);
-
- // [0] first 200ms spinner won't be shown (text NOSPINNER)
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- jest.advanceTimersByTime(300);
-
- // Before the promise get's resolved
- // [1] after 200ms spinner will be shown (text SPINNER)
- expect(await screen.findByText('SPINNER')).toBeInTheDocument();
-
- // After the promise get's resolved
- jest.runAllTimers();
-
- // [2] after 1000ms spinner will be hidded again (text NOSPINNER)
- // Wait for fakePromise (simulated ajax call) to be completed
- // no spinner should be shown
-
- await myFakePromise;
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
- expect(screen.queryByText('SPINNER')).not.toBeInTheDocument();
- });
-
- it('should render SPINNER when counter is 1, delay is set to 1000 and promise has 2000 timeout', async () => {
- // Arrange
- const TestSpinnerComponent = (props) => {
- return (
-
- {props.promiseInProgress ?
SPINNER
: NO SPINNER
}
-
- );
- };
-
- const getCounterStub = jest.spyOn(trackPromiseAPI, 'getCounter');
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
- render();
-
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
- expect(getCounterStub).toHaveBeenCalled();
-
- // Assert
- const myFakePromise = new Promise((resolve) => {
- setTimeout(() => {
- resolve(true);
- }, 2000);
- });
-
- trackPromiseAPI.trackPromise(myFakePromise);
-
- jest.advanceTimersByTime(500);
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- // Total advance 1000
- jest.advanceTimersByTime(500);
- expect(await screen.findByText('SPINNER')).toBeInTheDocument();
-
- // Total advance 1999
- jest.advanceTimersByTime(999);
- expect(await screen.findByText('SPINNER')).toBeInTheDocument();
-
- // After the promise get's resolved
- jest.runAllTimers();
-
- await myFakePromise;
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- // Total advance 2010
- jest.advanceTimersByTime(11);
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
- });
-
- it('should render NO SPINNER when counter is 1, delay is set to 2000 and promise has 1000 timeout', async () => {
- // Arrange
- const TestSpinnerComponent = (props) => {
- return (
-
- {props.promiseInProgress ?
SPINNER
: NO SPINNER
}
-
- );
- };
-
- const getCounterStub = jest.spyOn(trackPromiseAPI, 'getCounter');
-
- // Act
- const TrackedComponent = promiseTrackerHoc(TestSpinnerComponent);
- render();
-
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
- expect(getCounterStub).toHaveBeenCalled();
-
- // Assert
- const myFakePromise = new Promise((resolve) => {
- setTimeout(() => {
- resolve(true);
- }, 1000);
- });
-
- trackPromiseAPI.trackPromise(myFakePromise);
-
- jest.advanceTimersByTime(500);
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- // Total advance 1000
- jest.advanceTimersByTime(500);
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- await myFakePromise;
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- // Total advance 1999
- jest.advanceTimersByTime(999);
- expect(await screen.findByText('NO SPINNER')).toBeInTheDocument();
-
- // After the promise get's resolved
- jest.runAllTimers();
- });
- });
-});