Skip to content

test: Add unit tests for stringHelper.contains and randomAlphanumeric - #1173

Open
olitreadwell wants to merge 2 commits into
DemocracyLab:masterfrom
olitreadwell:test/civictech/add_stringhelper_contains_test
Open

test: Add unit tests for stringHelper.contains and randomAlphanumeric#1173
olitreadwell wants to merge 2 commits into
DemocracyLab:masterfrom
olitreadwell:test/civictech/add_stringhelper_contains_test

Conversation

@olitreadwell

Copy link
Copy Markdown

What changed

Adds unit tests for two uncovered helpers in common/components/utils/string.js, both in common/components/test/string.test.js:

  • contains: one test, four assertions (true case, false case, empty-list edge case).
  • randomAlphanumeric: one test, three assertions (returns a string, returns a non-empty string, returns a different value across calls).

No source code changed.

Why

common/components/utils/string.js exports six static helpers.

The existing test file covered four of them (isEmptyOrWhitespace, trimStartString, startsWithAny, isValidSlug) but not contains or randomAlphanumeric.

The source file also carries a // TODO: Update unit tests comment.

So both helpers had zero coverage.

These tests lock in their behaviour.

How to verify

git fetch https://github.com/olitreadwell/CivicTechExchange.git test/civictech/add_stringhelper_contains_test
git checkout FETCH_HEAD
npm ci --ignore-scripts
npx jest common/components/test/string.test.js

Expected: 6 passed (was 4 before this change).

Coverage

N/A: the repo has no coverage tool wired up.

Method-level: contains and randomAlphanumeric go from 0 tests to 1 each.

Checks run locally

  • Tests: PASS (full suite: 25 passed, 3 skipped, 0 failed; was 23 passed before)
  • Lint / format: PASS (npx prettier --check clean on the changed file)
  • Typecheck: N/A (repo uses Flow; no typecheck script, and no types were changed)
  • Build: N/A (webpack build needs a Python/Django backend + collectstatic; a test-only change does not touch build output)

The stringHelper.contains method had no test coverage.
The other four methods in string.js are tested, but contains was
skipped, and the source file has a TODO to update its unit tests.

This adds a test that covers the true case (a substring is present),
the false case (no substring matches), and the empty-list edge case.
The other five methods in string.js are tested, but
randomAlphanumeric had no test coverage.

This adds a test that checks the return value is a non-empty
string and that two calls produce different values.
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