Skip to content

[Test Improver] Add unit tests for useI18n composable - #104

Draft
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/use-i18n-composable-9dda018fe7c7e41b
Draft

[Test Improver] Add unit tests for useI18n composable#104
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/use-i18n-composable-9dda018fe7c7e41b

Conversation

@github-actions

Copy link
Copy Markdown

🤖 This PR was created by Test Improver, an automated AI assistant focused on improving test coverage.

Goal and Rationale

shell/composables/useI18n.ts is a key translation composable used throughout the dashboard. It has real branching logic worth testing:

  • Module-level state: a store variable that persists across calls, creating interesting interaction patterns
  • Initialization guard: throws if called with a null/undefined store
  • Null-store fallback in t(): returns the key as-is when the store is unavailable
  • Dev-mode warning: logs to console.warn only in dev environments

Despite being globally mocked in jest.setup.js (so other tests can use it without a real store), the composable itself had zero dedicated unit tests.

Approach

The global mock in jest.setup.js would normally prevent direct testing. The solution is to use jest.unmock('@shell/composables/useI18n') combined with jest.resetModules() in beforeEach — this bypasses the global mock and loads the real implementation fresh for each test.

@shell/plugins/i18n is mocked per-test to isolate the stringFor dependency.

Tests Added (9 total)

Group Test What it verifies
initialization valid store useI18n(store) returns { t } function
initialization null store throws 'usI18n() must be called from setup()'
initialization undefined store same throw behaviour
t() full args delegates to stringFor(store, key, args, raw)
t() omitted args passes undefined for args and raw
t() null store fallback returns key without calling stringFor
t() dev-mode warning console.warn called when process.env.dev is set
t() no warning outside dev console.warn not called without process.env.dev
t() module-level store sharing t() uses the most recently bound store

Coverage Impact

Before: useI18n.ts — 0 dedicated tests
After: All branches and paths covered (initialization guard, both t() branches, dev-mode conditional)

Reproducibility

YARN_IGNORE_ENGINES=true NODE_OPTIONS=--max_old_space_size=8192 yarn test:ci --testPathPattern=shell/composables/useI18n.test.ts

Test Status

✅ All 9 tests pass
✅ ESLint passes with --max-warnings 0

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • download.cypress.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "download.cypress.io"

See Network Configuration for more information.

Generated by Daily Test Improver · ● 7M ·

9 tests covering:
- initialization: valid store returns { t } function
- initialization: null/undefined store throws descriptive error
- t(): delegates to stringFor with all args (key, args, raw)
- t(): passes undefined args/raw when omitted
- t(): returns key as fallback when store is null
- t(): logs console.warn when store is null in dev mode
- t(): no warning when store is null outside dev mode
- t(): module-level store is replaced by most recent useI18n call

Uses jest.unmock() + jest.resetModules() to bypass the global
setup mock in jest.setup.js and test the real implementation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants