feat(config): add reset functions for global configurations#979
feat(config): add reset functions for global configurations#979
Conversation
- Add resetGlobalConfig to restore GlobalConfig to default values, including the original NoOpLogger and NoOpI18n instances - Add resetStencilShapeConfig to restore StencilShapeConfig to defaults - Update resetStyleDefaultsConfig test for consistency
WalkthroughAdds exported reset functions to restore GlobalConfig, StyleDefaultsConfig, and StencilShapeConfig to default snapshots; converts StencilShape to a named export and updates imports/exports, docs, Storybook preview, and tests to validate full-object restoration. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Preview as Storybook Preview
participant Core as `@maxgraph/core`
participant Registry as StencilRegistry
participant LoggerI18n as Logger/I18n
rect rgb(240,248,255)
Preview->>Core: import resetGlobalConfig(), resetStencilShapeConfig()
Preview->>Core: call resetGlobalConfig()
Core->>LoggerI18n: shallowCopy default logger/i18n into GlobalConfig
Core-->>Preview: GlobalConfig restored
end
rect rgb(245,255,240)
Preview->>Registry: unregisterAllStencilShapes()
Registry-->>Preview: all stencils removed
Preview->>Core: call resetStencilShapeConfig()
Core->>Registry: shallowCopy defaultStencilShapeConfig -> StencilShapeConfig
Core-->>Preview: StencilShapeConfig restored
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/core/__tests__/util/config.test.ts (1)
17-17: Consider removing unused import.The
describefunction is imported but not used in this test file. Consider removing it to keep the imports clean.🔎 Proposed fix
-import { describe, expect, test } from '@jest/globals'; +import { expect, test } from '@jest/globals';
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
packages/core/__tests__/util/config.test.ts(2 hunks)packages/core/__tests__/view/shape/stencil/StencilShape.test.ts(1 hunks)packages/core/src/index.ts(1 hunks)packages/core/src/util/config.ts(1 hunks)packages/core/src/view/shape/stencil/StencilShape.ts(2 hunks)packages/website/docs/usage/global-configuration.md(4 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use single quotes in TypeScript/JavaScript code (Prettier config:
singleQuote: true).
Files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/core/__tests__/util/config.test.tspackages/core/src/util/config.tspackages/core/src/view/shape/stencil/StencilShape.tspackages/core/src/index.ts
packages/core/__tests__/**/*.test.ts
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Test files must follow the naming pattern
**/*.test.tsand be located inpackages/core/__tests__/directory.
Files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/core/__tests__/util/config.test.ts
**/*.{ts,tsx,js,jsx,json,md}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Format code using Prettier with tab width 2, trailing comma ES5, print width 90, and end of line auto.
Files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/website/docs/usage/global-configuration.mdpackages/core/__tests__/util/config.test.tspackages/core/src/util/config.tspackages/core/src/view/shape/stencil/StencilShape.tspackages/core/src/index.ts
packages/core/__tests__/**/*.ts
📄 CodeRabbit inference engine (CLAUDE.md)
packages/core/__tests__/**/*.ts: Import paths in tests should omit the.jsextension (handled by moduleNameMapper)
Tests should be located inpackages/core/__tests__/mirroring the structure ofsrc/directory
Files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/core/__tests__/util/config.test.ts
packages/core/src/**/*.ts
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
packages/core/src/**/*.ts: Include.jsfile extensions in all import statements within packages/core/src/ to comply with ESLint rulen/file-extension-in-import.
Do not use console statements in any code. Use proper logging mechanisms instead (ESLint rule:no-console: error).
Do not useeval()function in any code (ESLint rule:no-eval: error).
Do not use const enums. Convert all const enums to regular enums (ESLint rule forbids const enums).
TypeScript source files in core package must strictly maintain typing with no implicit types and all functions having explicit return type annotations.
packages/core/src/**/*.ts: Use theisNullishfunction frominternal/utils.jswhen checking for null or undefined on variables that can have falsy values (numbers, strings, booleans)
Use thelog()function frominternal/utils.jsinstead of accessingGlobalConfig.loggerdirectly
Use functions frominternal/i18n-utils.js(translate, isI18nEnabled) instead of accessingGlobalConfig.i18ndirectly
Wrap multiple model changes inbatchUpdate()to optimize graph updates
Styles should be defined as objects conforming to theCellStyletype
Shapes must be registered via registries (e.g.,ShapeRegistry) before use;Graphclass auto-registers default shapes, butBaseGraphrequires manual registration
Files:
packages/core/src/util/config.tspackages/core/src/view/shape/stencil/StencilShape.tspackages/core/src/index.ts
🧠 Learnings (14)
📓 Common learnings
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/view/handler/config.test.ts:0-0
Timestamp: 2024-11-24T17:28:08.023Z
Learning: In `packages/core/__tests__/view/handler/config.test.ts`, when writing Jest tests in TypeScript for configuration reset functions, small duplication is acceptable if it maintains test clarity and readability. Avoid refactoring tests in ways that could make them harder to understand, especially when future use cases might require specific checks.
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 849
File: packages/html/stories/DragSource.stories.js:98-101
Timestamp: 2025-06-13T07:48:10.300Z
Learning: User tbouffard prefers answers in English; avoid switching to other languages in future replies.
📚 Learning: 2025-02-08T16:39:11.178Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 660
File: packages/core/src/view/geometry/node/StencilShapeRegistry.ts:0-0
Timestamp: 2025-02-08T16:39:11.178Z
Learning: The StencilShapeRegistry.getStencil method's implementation using non-null assertion (!) is validated by dedicated tests to handle null/undefined inputs correctly, returning undefined in such cases.
Applied to files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/website/docs/usage/global-configuration.mdpackages/core/src/view/shape/stencil/StencilShape.tspackages/core/src/index.ts
📚 Learning: 2024-11-24T17:28:08.023Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/view/handler/config.test.ts:0-0
Timestamp: 2024-11-24T17:28:08.023Z
Learning: In `packages/core/__tests__/view/handler/config.test.ts`, when writing Jest tests in TypeScript for configuration reset functions, small duplication is acceptable if it maintains test clarity and readability. Avoid refactoring tests in ways that could make them harder to understand, especially when future use cases might require specific checks.
Applied to files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/core/__tests__/util/config.test.tspackages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Shapes must be registered via registries (e.g., `ShapeRegistry`) before use; `Graph` class auto-registers default shapes, but `BaseGraph` requires manual registration
Applied to files:
packages/core/__tests__/view/shape/stencil/StencilShape.test.tspackages/website/docs/usage/global-configuration.mdpackages/core/src/view/shape/stencil/StencilShape.tspackages/core/src/index.ts
📚 Learning: 2025-04-24T12:33:36.243Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 776
File: packages/core/src/view/Graph.ts:77-83
Timestamp: 2025-04-24T12:33:36.243Z
Learning: All register functions in maxGraph (`registerDefaultShapes`, `registerDefaultEdgeStyles`, `registerDefaultPerimeters`, and `registerDefaultEdgeMarkers`) have internal state flags to prevent duplicate registrations, making them inherently idempotent.
Applied to files:
packages/website/docs/usage/global-configuration.md
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: For prototyping/demos, use `Graph` for automatic setup; for production, use `BaseGraph` with explicit feature registration
Applied to files:
packages/website/docs/usage/global-configuration.md
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Use functions from `internal/i18n-utils.js` (translate, isI18nEnabled) instead of accessing `GlobalConfig.i18n` directly
Applied to files:
packages/core/__tests__/util/config.test.tspackages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/__tests__/**/*.ts : Import paths in tests should omit the `.js` extension (handled by moduleNameMapper)
Applied to files:
packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/src/**/*.ts : Use the `log()` function from `internal/utils.js` instead of accessing `GlobalConfig.logger` directly
Applied to files:
packages/core/__tests__/util/config.test.tspackages/core/src/util/config.ts
📚 Learning: 2025-12-19T16:45:26.610Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-19T16:45:26.610Z
Learning: Applies to packages/core/__tests__/**/*.ts : Tests should be located in `packages/core/__tests__/` mirroring the structure of `src/` directory
Applied to files:
packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-10T13:43:28.685Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-10T13:43:28.685Z
Learning: Applies to packages/core/__tests__/**/*.test.ts : Test files must follow the naming pattern `**/*.test.ts` and be located in `packages/core/__tests__/` directory.
Applied to files:
packages/core/__tests__/util/config.test.ts
📚 Learning: 2025-12-10T13:43:28.685Z
Learnt from: CR
Repo: maxGraph/maxGraph PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-10T13:43:28.685Z
Learning: Applies to packages/core/package.json : Configure imports and exports to use ES2020 module format, with conditional exports supporting both ESM and CommonJS through proper package.json exports field.
Applied to files:
packages/core/__tests__/util/config.test.ts
📚 Learning: 2024-11-23T15:15:49.541Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/__tests__/util/config.test.ts:20-33
Timestamp: 2024-11-23T15:15:49.541Z
Learning: In `StyleDefaultsConfig`, negative offset values are acceptable; there are no invalid values for properties like `shadowOffsetX` and `shadowOffsetY`, which can be negative.
Applied to files:
packages/core/__tests__/util/config.test.ts
📚 Learning: 2024-11-23T16:12:51.380Z
Learnt from: tbouffard
Repo: maxGraph/maxGraph PR: 582
File: packages/core/src/util/config.ts:0-0
Timestamp: 2024-11-23T16:12:51.380Z
Learning: The `shallowCopy` function in `cloneUtils.ts` is intentionally designed to perform a shallow copy on objects that contain only primitive properties, and deep copying is not necessary.
Applied to files:
packages/core/src/view/shape/stencil/StencilShape.ts
🧬 Code graph analysis (3)
packages/core/__tests__/view/shape/stencil/StencilShape.test.ts (2)
packages/core/src/index.ts (2)
StencilShapeConfig(127-127)resetStencilShapeConfig(125-125)packages/core/src/view/shape/stencil/StencilShape.ts (2)
StencilShapeConfig(40-56)resetStencilShapeConfig(66-68)
packages/core/__tests__/util/config.test.ts (3)
packages/core/src/util/config.ts (3)
GlobalConfig(45-83)resetGlobalConfig(93-95)StyleDefaultsConfig(104-188)packages/core/src/util/logger.ts (1)
ConsoleLogger(55-104)packages/core/src/index.ts (1)
TranslationsAsI18n(133-133)
packages/core/src/util/config.ts (1)
packages/core/src/internal/clone-utils.ts (1)
shallowCopy(30-43)
🪛 markdownlint-cli2 (0.18.1)
packages/website/docs/usage/global-configuration.md
31-31: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
32-32: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
33-33: Unordered list indentation
Expected: 0; Actual: 2
(MD007, ul-indent)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build (macos-14)
- GitHub Check: build (windows-2022)
- GitHub Check: build (ubuntu-24.04)
🔇 Additional comments (10)
packages/core/src/util/config.ts (2)
35-35: LGTM!The import of
shallowCopywith the.jsextension is correct and follows the coding guidelines.
85-95: LGTM! Well-designed reset function.The implementation correctly captures a snapshot of
GlobalConfigat module initialization time, preserving the originalNoOpI18nandNoOpLoggerinstances. TheshallowCopyfunction will restore both primitive properties and object references, ensuring the original instances are restored as intended.The pattern is consistent with
resetStyleDefaultsConfigbelow and the JSDoc is clear and complete.packages/core/__tests__/view/shape/stencil/StencilShape.test.ts (1)
1-33: LGTM! Comprehensive test coverage.The test correctly verifies the reset behavior by:
- Capturing the original configuration
- Mutating multiple properties (
allowEvalanddefaultLocalized)- Calling the reset function
- Asserting both a specific property and full equality
The imports follow test conventions (no
.jsextensions), and the test structure is clear and easy to understand. Based on learnings, small duplication is acceptable if it maintains test clarity and readability.packages/core/src/view/shape/stencil/StencilShape.ts (2)
31-31: LGTM!The import correctly includes the
.jsextension as required by the coding guidelines for files inpackages/core/src/.
58-68: LGTM! Consistent implementation.The
resetStencilShapeConfig()function follows the same pattern asresetGlobalConfig()andresetStyleDefaultsConfig(), using a snapshot approach withshallowCopyto restore default values. The implementation is clean and the JSDoc is complete.packages/website/docs/usage/global-configuration.md (2)
31-33: LGTM! Documentation properly updated.The new reset functions
resetGlobalConfigandresetStencilShapeConfigare correctly added to the list with appropriate version tags (since 0.23.0). The formatting is consistent with other entries.Note: The markdownlint warnings (MD007) about list indentation are false positives. The 2-space indentation is correct for nested list items in Markdown.
47-47: LGTM! Minor clarity improvements.The wording adjustments enhance readability without changing the meaning:
- "allow" → "allows you" (line 47)
- Clarified statement about default stencil shapes (line 63)
- "XML object" → "XML objects" for grammatical correctness (line 88)
Also applies to: 63-63, 88-88
packages/core/__tests__/util/config.test.ts (2)
27-46: LGTM! Excellent test coverage.The test thoroughly verifies the reset behavior by:
- Capturing the original configuration and specific logger/i18n instances
- Replacing them with new instances (ConsoleLogger and TranslationsAsI18n)
- Verifying the mutation occurred
- Calling
resetGlobalConfig()- Asserting that the exact original instances are restored (not just equivalent objects)
- Verifying full configuration equality
The test correctly validates that the original
NoOpLoggerandNoOpI18ninstances are restored, which is the key requirement mentioned in the PR description.
48-62: LGTM! Enhanced test consistency.The addition of the full equality check with
toStrictEqual(originalConfig)on line 61 makes this test consistent with the newresetGlobalConfigtest. This ensures both specific property checks and comprehensive configuration equality validation.packages/core/src/index.ts (1)
124-128: LGTM! Public API correctly extended.The
resetStencilShapeConfigexport is properly added to the StencilShape export block, making it part of the public API. The formatting is consistent with the rest of the file.
|



Add functions to reset configuration objects to their default values:
resetGlobalConfig(): restores GlobalConfig to defaults, including the original NoOpLogger and NoOpI18n instancesresetStencilShapeConfig(): restores StencilShapeConfig to defaultsOther changes:
These reset functions are useful for testing scenarios and Storybook stories where configuration state needs to be restored between runs.
Summary by CodeRabbit
New Features
Bug Fixes / Tests
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.