[PR #27928] test: create some hooks and utils test script, modified clipboard test script #31897

Closed
opened 2026-02-21 20:50:21 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/27928

State: closed
Merged: Yes


Description

This PR adds comprehensive test coverage for previously untested utilities and hooks in the web module, and enhances existing tests with detailed documentation.

Fixes

Close https://github.com/langgenius/dify/issues/27929

New Test Files Created (5 files)

  1. utils/context.spec.ts - Tests for React context creation utilities

    • 12 test cases covering createCtx and createSelectorCtx functions
    • Tests provider/consumer patterns, error handling, default values, and type safety
    • 100% code coverage (45/45 statements)
  2. utils/model-config.spec.ts - Tests for model configuration transformations

    • 26 test cases covering bidirectional conversion between UserInputFormItem and PromptVariable
    • Tests all input types: text, paragraph, number, checkbox, select, file, file-list, external tools
    • 100% code coverage (210/210 statements)
  3. service/utils.spec.ts - Tests for flow prefix utilities

    • 13 test cases covering FlowType mapping and API path construction
    • Tests integration scenarios and type safety
    • 100% code coverage (14/14 statements)
  4. hooks/use-format-time-from-now.spec.ts - Tests for internationalized relative time formatting

    • 18 test cases covering 20+ locales, time granularities, and edge cases
    • Tests performance and memoization behavior
    • 100% code coverage (61/61 statements)
  5. hooks/use-tab-searchparams.spec.ts - Tests for URL-synced tab state management

    • 23 test cases covering routing behaviors (push/replace), custom param names, disabled mode
    • Tests browser history integration and edge cases
    • 100% code coverage (47/47 statements)

Enhanced Existing Test Files (3 files)

  1. hooks/use-breakpoints.spec.ts - Added comprehensive file-level docstrings and detailed comments
  2. hooks/use-document-title.spec.ts - Added documentation explaining title management and branding behavior
  3. utils/clipboard.spec.ts - Added comprehensive documentation of clipboard API fallback strategy

Key Improvements

  • 92 new test cases added with 100% code coverage
  • Comprehensive docstrings at file and function level explaining purpose and behavior
  • Inline comments documenting test intent and expected outcomes
  • Edge case coverage including error handling, type safety, and boundary conditions
  • Follows TDD principles with Arrange-Act-Assert structure
  • All tests passing with no lint errors

Testing Results

Total Test Suites: 8
Total Tests: 110 passed, 0 failed
Code Coverage: 100% for all new test files

Screenshots

Not applicable - this PR adds test coverage only.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

Contribution by Gittensor, learn more at https://gittensor.io/

**Original Pull Request:** https://github.com/langgenius/dify/pull/27928 **State:** closed **Merged:** Yes --- ## Description This PR adds comprehensive test coverage for previously untested utilities and hooks in the web module, and enhances existing tests with detailed documentation. ### Fixes Close https://github.com/langgenius/dify/issues/27929 ### New Test Files Created (5 files) 1. **`utils/context.spec.ts`** - Tests for React context creation utilities - 12 test cases covering `createCtx` and `createSelectorCtx` functions - Tests provider/consumer patterns, error handling, default values, and type safety - **100% code coverage** (45/45 statements) 2. **`utils/model-config.spec.ts`** - Tests for model configuration transformations - 26 test cases covering bidirectional conversion between `UserInputFormItem` and `PromptVariable` - Tests all input types: text, paragraph, number, checkbox, select, file, file-list, external tools - **100% code coverage** (210/210 statements) 3. **`service/utils.spec.ts`** - Tests for flow prefix utilities - 13 test cases covering `FlowType` mapping and API path construction - Tests integration scenarios and type safety - **100% code coverage** (14/14 statements) 4. **`hooks/use-format-time-from-now.spec.ts`** - Tests for internationalized relative time formatting - 18 test cases covering 20+ locales, time granularities, and edge cases - Tests performance and memoization behavior - **100% code coverage** (61/61 statements) 5. **`hooks/use-tab-searchparams.spec.ts`** - Tests for URL-synced tab state management - 23 test cases covering routing behaviors (push/replace), custom param names, disabled mode - Tests browser history integration and edge cases - **100% code coverage** (47/47 statements) ### Enhanced Existing Test Files (3 files) 1. **`hooks/use-breakpoints.spec.ts`** - Added comprehensive file-level docstrings and detailed comments 2. **`hooks/use-document-title.spec.ts`** - Added documentation explaining title management and branding behavior 3. **`utils/clipboard.spec.ts`** - Added comprehensive documentation of clipboard API fallback strategy ### Key Improvements - ✅ **92 new test cases** added with 100% code coverage - ✅ **Comprehensive docstrings** at file and function level explaining purpose and behavior - ✅ **Inline comments** documenting test intent and expected outcomes - ✅ **Edge case coverage** including error handling, type safety, and boundary conditions - ✅ **Follows TDD principles** with Arrange-Act-Assert structure - ✅ **All tests passing** with no lint errors ### Testing Results ``` Total Test Suites: 8 Total Tests: 110 passed, 0 failed Code Coverage: 100% for all new test files ``` ## Screenshots Not applicable - this PR adds test coverage only. ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat` (backend) and `cd web && npx lint-staged` (frontend) to appease the lint gods Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:21 -05:00
yindo closed this issue 2026-02-21 20:50:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31897