Skip to content

Commit

Permalink
chore: add 0.4.1 version that restores the without args version to fo…
Browse files Browse the repository at this point in the history
…llow semver
  • Loading branch information
alexdriaguine committed Aug 9, 2018
1 parent 7da12e3 commit 8f49f57
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,13 @@ exports[`withSpinner should support custom nested loading property 1`] = `
</withSpinner(DisplayComponent)>
</_class3>
`;

exports[`withSpinner should work without any args 1`] = `
<DefaultSpinner
data={
Object {
"loading": true,
}
}
/>
`;
16 changes: 16 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ const Loading = () => <span>Loading...</span>
jest.useFakeTimers()

describe('withSpinner', () => {
it('should work without any args', () => {
const Component = compose(
WrappedComponent => props => <WrappedComponent {...props} data={{loading: true}} />,
withSpinner(),
)(() => <div></div>)

const wrapper = shallow(<Component />).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 => <WrappedComponent {...props} data={{loading: true}} />,
Expand Down
10 changes: 6 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export type Properties = {
emptyComponent?: ReactType
}

const DefaultSpinner = () => <div>Loading...</div>

function getDataProperty(propName: string|Array<string>, props: Properties) {
return Array.isArray(propName)
? path(propName, props)
Expand All @@ -60,11 +62,11 @@ function getDataProperty(propName: string|Array<string>, 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<Properties, {showSpinner: boolean}> {
static displayName = wrapDisplayName(WrappedComponent, 'withSpinner')

Expand Down Expand Up @@ -101,7 +103,7 @@ export const withSpinner = ({
if (this.state.showSpinner) return <Spinner {...spinnerProps} {...this.props} />

if (this.timeout === null) {
this.timeout = setTimeout(() => {
this.timeout = window.setTimeout(() => {
this.setState({showSpinner: true})
}, timeout)
}
Expand Down

0 comments on commit 8f49f57

Please sign in to comment.