Skip to content

Commit

Permalink
fix: bring back mockState inside core
Browse files Browse the repository at this point in the history
  • Loading branch information
dackmin committed Dec 11, 2024
1 parent 11b50b2 commit 71832e1
Show file tree
Hide file tree
Showing 25 changed files with 51 additions and 43 deletions.
5 changes: 5 additions & 0 deletions packages/core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ export {
formatThousand,
} from './numbers';

export {
mockState,
} from './state';

export type {
GenericObject,
Grow,
GrowToSize,
FixedArray,
StateContent,
} from './types';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mockState } from './utils';
import { mockState } from './';

describe('react/utils', () => {
describe('state', () => {
describe('mockState(state, action)', () => {
it('should merge state with action object to simulate setState', () => {
expect(mockState({ foo: 'bar' }, { foo: 'test' }))
Expand Down
12 changes: 12 additions & 0 deletions packages/core/lib/state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { StateContent } from '../types';

/**
* mockState should be inside react package, but some external packages
* (like @poool/react-access) only depend on @junipero/core, so it will be
* here forever).
*/
export const mockState = <T extends StateContent>(
state: T,
action : ((prev: T) => T) | Partial<T>
) => typeof action === 'function'
? action(state) : ({ ...state, ...action as T });
8 changes: 8 additions & 0 deletions packages/core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ export type GrowToSize<T, A extends Array<T>, N extends number> = {
}[A['length'] extends N ? 0 : 1];

export type FixedArray<T, N extends number> = GrowToSize<T, [], N>;

/**
* Represents a state comming from useReducer.
* Volontarily abstracted to allow any kind of state.
*/
export declare interface StateContent {
[key: string]: any;
}
2 changes: 1 addition & 1 deletion packages/react/lib/Calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
subMonths,
addMonths,
getDaysInMonth,
mockState,
} from '@junipero/core';

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import { mockState } from '../utils';
import { ArrowLeft, ArrowRight } from '../icons';

export declare interface CalendarRef extends JuniperoRef {
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/CheckboxField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
useReducer,
useRef,
} from 'react';
import { classNames } from '@junipero/core';
import { classNames, mockState } from '@junipero/core';

import type {
FieldContent,
JuniperoRef,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';
import { Check } from '../icons';

Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/CodeField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
useRef,
useImperativeHandle,
} from 'react';
import { classNames } from '@junipero/core';
import { classNames, mockState } from '@junipero/core';

import type {
FieldContent,
JuniperoRef,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';

export declare interface CodeFieldRef extends JuniperoRef {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/ColorField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
exists,
stringifyColor,
parseColor,
mockState,
} from '@junipero/core';
import { useEventListener } from '@junipero/hooks';

Expand All @@ -24,7 +25,6 @@ import type {
SpecialComponentPropsWithRef,
} from '../types';
import type { TransitionProps } from '../Transition';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';
import Dropdown, { type DropdownProps, type DropdownRef } from '../Dropdown';
import DropdownToggle from '../DropdownToggle';
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/DateField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {
useRef,
useEffect,
} from 'react';
import { type FixedArray, classNames, exists } from '@junipero/core';
import { type FixedArray, classNames, exists, mockState } from '@junipero/core';

import type { TransitionProps } from '../Transition';
import type {
JuniperoRef,
FieldContent,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';
import { Arrows, Remove, Time } from '../icons';
import Dropdown, { type DropdownRef } from '../Dropdown';
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useReducer,
useEffect,
} from 'react';
import { classNames, omit } from '@junipero/core';
import { classNames, omit, mockState } from '@junipero/core';
import {
type UseClickProps,
type UseDismissProps,
Expand All @@ -30,7 +30,6 @@ import {

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import { DropdownContext, type DropdownContextType } from '../contexts';
import { mockState } from '../utils';

export declare interface DropdownRef extends JuniperoRef {
opened: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/FieldControl/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ComponentPropsWithoutRef, useCallback, useReducer } from 'react';
import { mockState } from '@junipero/core';

import { type FieldContextType, FieldControlContext } from '../contexts';
import { mockState } from '../utils';

export declare interface FieldControlProps extends Omit<
ComponentPropsWithoutRef<typeof FieldControlContext.Provider>, 'value'
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {
useMemo,
useEffect,
} from 'react';
import { classNames } from '@junipero/core';
import { classNames, mockState } from '@junipero/core';

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import type { ListColumnObject } from '../ListColumn';
import { ListContext, type ListContextType } from '../contexts';
import { ArrowDown, ArrowUp } from '../icons';
import { mockState } from '../utils';

export declare interface ListRef extends JuniperoRef {
orderable: boolean;
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import {
useRef,
} from 'react';
import { createPortal } from 'react-dom';
import { classNames, ensureNode } from '@junipero/core';
import { classNames, ensureNode, mockState } from '@junipero/core';

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import type { TransitionProps } from '../Transition';
import { useModal } from '../hooks';
import { mockState } from '../utils';
import { Remove } from '../icons';

export declare interface ModalRef extends JuniperoRef {
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/RadioField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
useImperativeHandle,
useEffect,
} from 'react';
import { classNames } from '@junipero/core';
import { classNames, mockState } from '@junipero/core';

import type {
FieldContent,
JuniperoRef,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';

export declare type RadioFieldValue = any;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/SelectField/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createRef, useEffect, useReducer, useState } from 'react';
import { render, fireEvent, act } from '@testing-library/react';
import { configMocks, mockIntersectionObserver } from 'jsdom-testing-mocks';
import { mockState } from '@junipero/core';
import userEvent from '@testing-library/user-event';

import { blur, focus, reset, sleep } from '~tests-utils';
import type { FieldContent } from '../types';
import { cloneDeep, set } from '../../../core/lib/core';
import { mockState } from '../utils';
import FieldControl from '../FieldControl';
import Label from '../Label';
import Abstract from '../Abstract';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/SelectField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
exists,
filterDeep,
findDeep,
mockState,
} from '@junipero/core';
import { useTimeout } from '@junipero/hooks';

Expand All @@ -28,7 +29,6 @@ import type {
import type { TransitionProps } from '../Transition';
import { useFieldControl } from '../hooks';
import { Arrows, Remove } from '../icons';
import { mockState } from '../utils';
import Dropdown, { type DropdownRef } from '../Dropdown';
import DropdownToggle from '../DropdownToggle';
import DropdownMenu from '../DropdownMenu';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/Slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
classNames,
getFloatPrecision,
ensureMinMax,
mockState,
} from '@junipero/core';
import { useEventListener } from '@junipero/hooks';

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import type { TransitionProps } from '../Transition';
import { mockState } from '../utils';
import Tooltip, { type TooltipRef } from '../Tooltip';

export declare interface SliderRef extends JuniperoRef {
Expand Down
7 changes: 4 additions & 3 deletions packages/react/lib/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type ReactNode,
type MouseEvent,
type ReactElement,
type ComponentPropsWithoutRef,
Children,
useEffect,
useImperativeHandle,
Expand All @@ -13,7 +14,7 @@ import {
import { classNames } from '@junipero/core';

import type { JuniperoRef, SpecialComponentPropsWithRef } from '../types';
import Tab, { TabProps, type TabObject } from '../Tab';
import Tab, { type TabProps, type TabObject } from '../Tab';

export declare interface TabsRef extends JuniperoRef {
activeTab: number;
Expand All @@ -28,7 +29,7 @@ export declare interface TabsProps extends Omit<
active?: number;
disabled?: boolean;
tabs?: Array<TabObject>;
filterTab?(child: ReactElement | ReactNode): boolean;
filterTab?(child: ReactElement<ComponentPropsWithoutRef<any>>): boolean;
onToggle?(index: number): void;
}

Expand All @@ -39,7 +40,7 @@ const Tabs = ({
active,
tabs,
disabled = false,
filterTab = (child: ReactElement) =>
filterTab = (child: ReactElement<ComponentPropsWithoutRef<any>>) =>
typeof child !== 'string' && child.type === Tab,
onToggle,
...rest
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import {
useReducer,
useRef,
} from 'react';
import { classNames, exists } from '@junipero/core';
import { classNames, exists, mockState } from '@junipero/core';

import type {
FieldContent,
JuniperoRef,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';
import { useFieldControl } from '../hooks';

export declare interface TextFieldRef extends JuniperoRef {
Expand Down
3 changes: 1 addition & 2 deletions packages/react/lib/Toggle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
useReducer,
useRef,
} from 'react';
import { classNames } from '@junipero/core';
import { classNames, mockState } from '@junipero/core';

import type {
FieldContent,
JuniperoRef,
SpecialComponentPropsWithRef,
} from '../types';
import { mockState } from '../utils';

export declare type ToggleValue = any;

Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/Tooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
classNames,
ensureNode,
omit,
mockState,
} from '@junipero/core';
import {
type UseDismissProps,
Expand All @@ -41,7 +42,6 @@ import type {
SpecialComponentPropsWithRef,
} from '../types';
import type { TransitionProps } from '../Transition';
import { mockState } from '../utils';

export declare interface TooltipRef extends JuniperoRef {
opened: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/Transition/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useReducer } from 'react';
import { mockState } from '@junipero/core';

import type { FieldContent } from '../types';
import { mockState } from '../utils';
import Button from '../Button';
import Label from '../Label';
import TextField from '../TextField';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/lib/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useReducer } from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { mockState } from '@junipero/core';
import { useTimeout } from '@junipero/hooks';

import type { FieldContent } from './types';
import { mockState } from './utils';
import TextField from './TextField';

type State = {
Expand Down
6 changes: 2 additions & 4 deletions packages/react/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
type Grow,
type GrowToSize,
type FixedArray,
type StateContent,
COLORS,
ensureNode,
classNames,
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
ensureMinMax,
getFloatPrecision,
formatThousand,
mockState,
} from '@junipero/core';

export {
Expand Down Expand Up @@ -375,8 +377,4 @@ export {
type ModalContextType,
} from './contexts';

export {
mockState,
} from './utils';

export type * from './types';
7 changes: 0 additions & 7 deletions packages/react/lib/utils.ts

This file was deleted.

0 comments on commit 71832e1

Please sign in to comment.