Skip to content

Commit

Permalink
Some improvements (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Jul 27, 2021
1 parent 565d2c0 commit 1ddf5d4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 47 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"homepage": "https://react-countup.now.sh",
"main": "build",
"files": [
"build"
"build/index.js",
"build/index.d.ts"
],
"typings": "build/index.d.ts",
"scripts": {
Expand Down
20 changes: 2 additions & 18 deletions src/CountUp.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component, CSSProperties } from 'react';
import warning from 'warning';
import { createCountUpInstance } from './common';
import { createCountUpInstance, DEFAULTS } from './common';
import { CountUp as CountUpJs } from 'countup.js';
import { CallbackProps, CommonProps, RenderCounterProps } from './types';

Expand All @@ -17,25 +17,9 @@ class CountUp extends Component<CountUpProps> {
private timeoutId: ReturnType<typeof setTimeout> | undefined;

static defaultProps = {
decimal: '.',
decimals: 0,
delay: null,
duration: null,
easingFn: null,
formattingFn: null,
onEnd: () => {},
onPauseResume: () => {},
onReset: () => {},
onStart: () => {},
onUpdate: () => {},
prefix: '',
...DEFAULTS,
redraw: false,
separator: '',
start: 0,
startOnMount: true,
suffix: '',
style: undefined,
useEasing: true,
preserveValue: false,
};

Expand Down
12 changes: 0 additions & 12 deletions src/__tests__/CountUp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,6 @@ describe('CountUp component', () => {
expect(container).toMatchSnapshot();
});

it('renders with delay as a render prop component correctly', () => {
console.error = jest.fn();

const { container } = render(
<CountUp delay={1} end={10}>
{({ countUpRef }) => <rect ref={countUpRef}/>}
</CountUp>,
);

expect(console.error).toHaveBeenCalled();
});

it('renders as a render prop component correctly', () => {
const { container } = render(
<CountUp end={10}>{({ countUpRef }) => <div ref={countUpRef}/>}</CountUp>,
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/useCountUp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ describe('useCountUp', () => {
expect(span.textContent).toEqual('0');
});

it('calls onEnd correctly', async () => {
let startFn;
const onEnd = jest.fn();
const Hook = () => {
const { start } = useCountUp({ end: 10, duration: 1, onEnd, startOnMount: false, ref: "counter" });
startFn = start;
return <span id="counter"/>;
};

const { container } = render(<Hook/>);
act(() => {
startFn();
})

await checkContent(container, '10');
expect(onEnd).toHaveBeenCalled();
});

it('calls update correctly', async () => {
let updateFn;
const onUpdate = jest.fn();
Expand Down
8 changes: 8 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { CountUp } from 'countup.js';
import { CountUpProps } from './CountUp';
import { useCountUpProps } from './useCountUp';

export const DEFAULTS = {
decimal: '.',
delay: null,
prefix: '',
suffix: '',
start: 0
}

export const createCountUpInstance = (el: string | HTMLElement | HTMLInputElement, props: useCountUpProps | CountUpProps) => {
const {
decimal,
Expand Down
20 changes: 4 additions & 16 deletions src/useCountUp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallbackProps, CommonProps, UpdateFn } from './types';
import { useEffect, useRef } from 'react';
import { createCountUpInstance } from './common';
import React, { useEffect, useRef } from 'react';
import { createCountUpInstance, DEFAULTS } from './common';
import { CountUp as CountUpJs } from 'countup.js';

export interface useCountUpProps extends CommonProps, CallbackProps {
Expand All @@ -9,20 +9,8 @@ export interface useCountUpProps extends CommonProps, CallbackProps {
}

const defaults: Partial<useCountUpProps> = {
decimal: '.',
decimals: 0,
delay: null,
onEnd: () => {},
onPauseResume: () => {},
onReset: () => {},
onStart: () => {},
onUpdate: () => {},
prefix: '',
separator: '',
start: 0,
...DEFAULTS,
startOnMount: true,
suffix: '',
useEasing: true,
};

const useCountUp = (props: useCountUpProps) => {
Expand Down Expand Up @@ -53,10 +41,10 @@ const useCountUp = (props: useCountUpProps) => {
const restart = () => {
const { onStart, onEnd } = parsedProps;
getCountUp().reset();
onStart?.({ pauseResume, reset, update });
getCountUp().start(() => {
onEnd?.({ pauseResume, reset, start: restart, update });
});
onStart?.({ pauseResume, reset, update });
};

const pauseResume = () => {
Expand Down

0 comments on commit 1ddf5d4

Please sign in to comment.