Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 👔 supported zustand v5 #21

Merged
merged 1 commit into from
Dec 7, 2024
Merged

feat: 👔 supported zustand v5 #21

merged 1 commit into from
Dec 7, 2024

Conversation

Albert-Gao
Copy link
Owner

@Albert-Gao Albert-Gao commented Dec 7, 2024

Summary by Sourcery

Add support for Zustand v5 by updating peer dependencies and refactor test files to use the 'screen' object from '@testing-library/react'. Update Rollup configuration to use '@rollup/plugin-terser' and enhance ESLint configuration with a new setup.

New Features:

  • Add support for Zustand v5 by updating peer dependencies.

Enhancements:

  • Refactor test files to use the 'screen' object from '@testing-library/react' for querying elements.

Build:

  • Update Rollup configuration to use '@rollup/plugin-terser' instead of 'rollup-plugin-terser'.

Tests:

  • Refactor tests to improve readability and maintainability by using 'screen' from '@testing-library/react'.

Copy link

sourcery-ai bot commented Dec 7, 2024

Reviewer's Guide by Sourcery

This PR updates the library to support Zustand v5, including upgrading dependencies and modernizing the testing approach. The implementation focuses on integrating Zustand's new useShallow hook for selector optimization and updating the build configuration.

Class diagram for Zustand selector functions

classDiagram
    class ZustandFuncSelectors {
        +use: object
    }
    class ZustandHookSelectors {
        +use: object
    }
    class StoreApi
    class UseBoundStore
    class useStore
    class useShallow
    ZustandFuncSelectors --> StoreApi
    ZustandFuncSelectors --> UseBoundStore
    ZustandFuncSelectors --> useStore
    ZustandFuncSelectors --> useShallow
    ZustandHookSelectors --> StoreApi
    ZustandHookSelectors --> UseBoundStore
    ZustandHookSelectors --> useStore
    ZustandHookSelectors --> useShallow
    note for useShallow "New hook integrated for selector optimization"
Loading

File-Level Changes

Change Details Files
Updated Zustand integration to use the new useShallow hook for better performance
  • Added import for useShallow from zustand/react/shallow
  • Modified selector functions to wrap selectors with useShallow
  • Updated both createSelectorFunctions and createSelectorHooks to use the new pattern
src/createSelectorFunctions.ts
src/createSelectorHooks.ts
Modernized testing approach using Testing Library best practices
  • Replaced direct getByTestId destructuring with screen queries
  • Added @testing-library/jest-dom for improved assertions
  • Updated test syntax to follow current Testing Library conventions
tests/context-usage.test.tsx
tests/createSelectorHooks.test.tsx
tests/createSelectorFunctions.test.tsx
Updated build configuration and dependencies
  • Migrated from rollup-plugin-terser to @rollup/plugin-terser
  • Updated peer dependency requirement to zustand >= 5.0.0
  • Upgraded various development dependencies to their latest versions
  • Converted ESLint configuration to new flat config format
package.json
rollup.config.mjs
eslint.config.mjs
eslint.config.js

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Albert-Gao Albert-Gao merged commit 3728b1d into master Dec 7, 2024
2 checks passed
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Albert-Gao - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -32,44 +32,49 @@
"node": ">=18.0.0"
},
"peerDependencies": {
"zustand": "*"
"zustand": ">=5.0.0"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider adding upper bound to zustand peer dependency

To prevent potential breaking changes, consider specifying an upper bound version constraint (e.g., '>=5.0.0 <6.0.0').

Suggested change
"zustand": ">=5.0.0"
"zustand": ">=5.0.0 <6.0.0"


expect(getByTestId('text').textContent).toBe('2');
expect(screen.getByTestId('text').textContent).toBe('2');
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): Missing tests for new useShallow functionality

Since the implementation now uses useShallow from zustand/react/shallow, we should add specific tests to verify that the shallow comparison behavior works as expected, especially with object and array state values.

@@ -0,0 +1,61 @@
import typescriptEslint from '@typescript-eslint/eslint-plugin';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider restructuring the ESLint configuration to use dynamic imports and consistent plugin handling patterns

The configuration can be simplified while maintaining all functionality. Here's a cleaner approach:

import { FlatCompat } from '@eslint/eslintrc';
import globals from 'globals';
import js from '@eslint/js';
import { fixupPluginRules } from '@eslint/compat';

// Import and fix up all plugins consistently
const plugins = {
  '@typescript-eslint': fixupPluginRules(await import('@typescript-eslint/eslint-plugin')),
  'prettier': fixupPluginRules(await import('eslint-plugin-prettier')),
  'react-hooks': fixupPluginRules(await import('eslint-plugin-react-hooks'))
};

export default [
  ...(new FlatCompat()).extends(
    'prettier',
    'plugin:prettier/recommended',
    'plugin:@typescript-eslint/eslint-recommended'
  ),
  {
    plugins,
    languageOptions: {
      globals: { ...globals.browser, Atomics: 'readonly', SharedArrayBuffer: 'readonly' },
      parser: (await import('@typescript-eslint/parser')).default,
      ecmaVersion: 2018,
      sourceType: 'module',
      parserOptions: { ecmaFeatures: { jsx: true } }
    },
    rules: {
      'prettier/prettier': 'error',
      '@typescript-eslint/explicit-function-return-type': 'off',
      'no-use-before-define': 'off',
      '@typescript-eslint/no-use-before-define': ['error'],
      'react-hooks/rules-of-hooks': 'error',
      'react-hooks/exhaustive-deps': 'warn',
      'react/react-in-jsx-scope': 'off'
    }
  }
];

Key improvements:

  1. Removed unnecessary path manipulation since FlatCompat defaults work fine
  2. Standardized plugin handling with consistent use of fixupPluginRules
  3. Used dynamic imports to reduce top-level import complexity
  4. Flattened configuration structure where possible
  5. Removed intermediate variables that weren't adding clarity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant