diff --git a/package.json b/package.json index df1d809..7297115 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "react-with-spinner", - "version": "0.4.0", + "version": "0.4.1", "description": "", "main": "dist/src/index.js", + "types": "dist/src/index.d.ts", "scripts": { "build": "tscomp build", "watch": "tscomp watch", diff --git a/src/__snapshots__/index.test.tsx.snap b/src/__snapshots__/index.test.tsx.snap index a55a566..2b65df8 100644 --- a/src/__snapshots__/index.test.tsx.snap +++ b/src/__snapshots__/index.test.tsx.snap @@ -138,3 +138,13 @@ exports[`withSpinner should support custom nested loading property 1`] = ` `; + +exports[`withSpinner should work without any args 1`] = ` + +`; diff --git a/src/index.test.tsx b/src/index.test.tsx index 7e1ec7f..bdbc251 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -11,6 +11,22 @@ const Loading = () => Loading... jest.useFakeTimers() describe('withSpinner', () => { + it('should work without any args', () => { + const Component = compose( + WrappedComponent => props => , + withSpinner(), + )(() =>
) + + const wrapper = shallow().first().shallow() + + expect(wrapper.type()).toBeNull() + + // Run timer so it passes withSpinner timeout + jest.runTimersToTime(100) + wrapper.update() + + expect(toJson(wrapper)).toMatchSnapshot() + }) it('should render spinner if loading is true', () => { const Component = compose( WrappedComponent => props => , diff --git a/src/index.tsx b/src/index.tsx index fad6775..81a2c94 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -51,6 +51,8 @@ export type Properties = { emptyComponent?: ReactType } +const DefaultSpinner = () =>
Loading...
+ function getDataProperty(propName: string|Array, props: Properties) { return Array.isArray(propName) ? path(propName, props) @@ -60,11 +62,11 @@ function getDataProperty(propName: string|Array, props: Properties) { export const withSpinner = ({ prop = 'data', timeout = 100, handleError = true, partial = false, - skipErrors, spinnerProps, + skipErrors = false, spinnerProps = {}, errorComponent: ErrorComponent = null, - spinnerComponent: Spinner, + spinnerComponent: Spinner = DefaultSpinner, emptyComponent: EmptyComponent = null, -}: Properties): any => WrappedComponent => +}: Properties = {} as Properties): any => WrappedComponent => class extends Component { static displayName = wrapDisplayName(WrappedComponent, 'withSpinner') @@ -101,7 +103,7 @@ export const withSpinner = ({ if (this.state.showSpinner) return if (this.timeout === null) { - this.timeout = setTimeout(() => { + this.timeout = window.setTimeout(() => { this.setState({showSpinner: true}) }, timeout) }