-
Notifications
You must be signed in to change notification settings - Fork 468
feat(frontend): add Jest unit testing infrastructure #6432
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
Open
talissoncosta
wants to merge
8
commits into
main
Choose a base branch
from
feat/add-jest-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,364
−6,510
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2a91c6
feat(testing): add Jest unit testing infrastructure
talissoncosta 239f6e8
ci: add frontend PR workflow with unit tests
talissoncosta d381cf5
test(format): add unit tests and clean up redundant .toString() calls
talissoncosta e1f10f9
refactor(format): add moment import and fix money function types
talissoncosta 55f55d4
fix(ci): address PR review feedback
talissoncosta a75ede3
fix(ci): add permissions block to frontend-pull-request workflow
talissoncosta 076d406
chore(ci): align checkout action to v5 in both workflows
talissoncosta a31b8d0
refactor(test): consolidate unit tests using it.each for readability
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| name: Frontend Pull Requests | ||
talissoncosta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened, ready_for_review] | ||
| paths: | ||
| - frontend/** | ||
| - .github/workflows/frontend-pull-request.yml | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| unit-tests: | ||
| name: Unit Tests | ||
| runs-on: ubuntu-latest | ||
|
|
||
| defaults: | ||
| run: | ||
| working-directory: frontend | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v5 | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version-file: frontend/.nvmrc | ||
| cache: npm | ||
| cache-dependency-path: frontend/package-lock.json | ||
|
|
||
| - name: Install dependencies | ||
| run: npm ci | ||
|
|
||
| - name: Run unit tests | ||
| run: npm run test:unit | ||
206 changes: 206 additions & 0 deletions
206
frontend/common/utils/__tests__/featureFilterParams.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // Mock useProjectFlag to avoid deep dependency chain with legacy JS files | ||
| jest.mock('common/services/useProjectFlag', () => ({ | ||
| FEATURES_PAGE_SIZE: 100, | ||
| })) | ||
|
|
||
| import { | ||
| buildUrlParams, | ||
| buildApiFilterParams, | ||
| getFiltersFromParams, | ||
| hasActiveFilters, | ||
| } from 'common/utils/featureFilterParams' | ||
| import { SortOrder } from 'common/types/requests' | ||
| import { TagStrategy } from 'common/types/responses' | ||
| import type { FilterState } from 'common/types/featureFilters' | ||
|
|
||
| const createDefaultFilters = ( | ||
| overrides?: Partial<FilterState>, | ||
| ): FilterState => ({ | ||
| group_owners: [], | ||
| is_enabled: null, | ||
| owners: [], | ||
| search: null, | ||
| showArchived: false, | ||
| sort: { | ||
| label: 'Name', | ||
| sortBy: 'name', | ||
| sortOrder: SortOrder.ASC, | ||
| }, | ||
| tag_strategy: TagStrategy.INTERSECTION, | ||
| tags: [], | ||
| value_search: '', | ||
| ...overrides, | ||
| }) | ||
|
|
||
| describe('featureFilterParams', () => { | ||
| describe('buildUrlParams', () => { | ||
| it.each` | ||
| showArchived | expected | ||
| ${false} | ${'false'} | ||
| ${true} | ${'true'} | ||
| `( | ||
| 'sets is_archived to "$expected" when showArchived is $showArchived', | ||
| ({ expected, showArchived }) => { | ||
| const result = buildUrlParams(createDefaultFilters({ showArchived }), 1) | ||
| expect(result.is_archived).toBe(expected) | ||
| }, | ||
| ) | ||
|
|
||
| it('always includes is_archived (never undefined)', () => { | ||
| const result = buildUrlParams(createDefaultFilters(), 1) | ||
| expect(result.is_archived).toBeDefined() | ||
| }) | ||
|
|
||
| it('includes page number', () => { | ||
| const result = buildUrlParams(createDefaultFilters(), 5) | ||
| expect(result.page).toBe(5) | ||
| }) | ||
|
|
||
| it('includes sort parameters', () => { | ||
| const filters = createDefaultFilters({ | ||
| sort: { | ||
| label: 'Created', | ||
| sortBy: 'created_date', | ||
| sortOrder: SortOrder.DESC, | ||
| }, | ||
| }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.sortBy).toBe('created_date') | ||
| expect(result.sortOrder).toBe('desc') | ||
| }) | ||
|
|
||
| it('includes tags when present', () => { | ||
| const result = buildUrlParams( | ||
| createDefaultFilters({ tags: [1, 2, 3] }), | ||
| 1, | ||
| ) | ||
| expect(result.tags).toBe('1,2,3') | ||
| }) | ||
|
|
||
| it('excludes empty arrays and search', () => { | ||
| const filters = createDefaultFilters({ owners: [], search: '', tags: [] }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.tags).toBeUndefined() | ||
| expect(result.owners).toBeUndefined() | ||
| expect(result.search).toBeUndefined() | ||
| }) | ||
|
|
||
| it('includes search when present', () => { | ||
| const result = buildUrlParams(createDefaultFilters({ search: 'test' }), 1) | ||
| expect(result.search).toBe('test') | ||
| }) | ||
| }) | ||
|
|
||
| describe('buildApiFilterParams', () => { | ||
| const mockResolver = (apiKey: string) => | ||
| apiKey === 'test-key' ? 123 : undefined | ||
|
|
||
| it.each` | ||
| showArchived | expected | ||
| ${false} | ${'false'} | ||
| ${true} | ${'true'} | ||
| `( | ||
| 'sets is_archived to "$expected" when showArchived is $showArchived', | ||
| ({ expected, showArchived }) => { | ||
| const result = buildApiFilterParams( | ||
| createDefaultFilters({ showArchived }), | ||
| 1, | ||
| 'test-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result?.is_archived).toBe(expected) | ||
| }, | ||
| ) | ||
|
|
||
| it('always includes is_archived (never undefined)', () => { | ||
| const result = buildApiFilterParams( | ||
| createDefaultFilters(), | ||
| 1, | ||
| 'test-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result).not.toBeNull() | ||
| expect(result?.is_archived).toBeDefined() | ||
| }) | ||
|
|
||
| it('returns null when environment ID cannot be resolved', () => { | ||
| const result = buildApiFilterParams( | ||
| createDefaultFilters(), | ||
| 1, | ||
| 'invalid-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('includes environmentId and projectId', () => { | ||
| const result = buildApiFilterParams( | ||
| createDefaultFilters(), | ||
| 1, | ||
| 'test-key', | ||
| 42, | ||
| mockResolver, | ||
| ) | ||
| expect(result?.environmentId).toBe('123') | ||
| expect(result?.projectId).toBe(42) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getFiltersFromParams', () => { | ||
| it.each` | ||
| is_archived | expected | ||
| ${'true'} | ${true} | ||
| ${'false'} | ${false} | ||
| ${undefined} | ${false} | ||
| `( | ||
| 'parses is_archived=$is_archived to showArchived=$expected', | ||
| ({ expected, is_archived }) => { | ||
| const result = getFiltersFromParams(is_archived ? { is_archived } : {}) | ||
| expect(result.showArchived).toBe(expected) | ||
| }, | ||
| ) | ||
|
|
||
| it.each` | ||
| page | expected | ||
| ${'3'} | ${3} | ||
| ${undefined} | ${1} | ||
| `('parses page=$page to $expected', ({ expected, page }) => { | ||
| const result = getFiltersFromParams(page ? { page } : {}) | ||
| expect(result.page).toBe(expected) | ||
| }) | ||
|
|
||
| it('parses tags as array of numbers', () => { | ||
| const result = getFiltersFromParams({ tags: '1,2,3' }) | ||
| expect(result.tags).toEqual([1, 2, 3]) | ||
| }) | ||
|
|
||
| it('parses sort order', () => { | ||
| const result = getFiltersFromParams({ | ||
| sortBy: 'created_date', | ||
| sortOrder: 'desc', | ||
| }) | ||
| expect(result.sort.sortBy).toBe('created_date') | ||
| expect(result.sort.sortOrder).toBe(SortOrder.DESC) | ||
| }) | ||
| }) | ||
|
|
||
| describe('hasActiveFilters', () => { | ||
| it('returns false for default filters', () => { | ||
| expect(hasActiveFilters(createDefaultFilters())).toBe(false) | ||
| }) | ||
|
|
||
| it.each` | ||
| scenario | overrides | ||
| ${'tags present'} | ${{ tags: [1] }} | ||
| ${'showArchived'} | ${{ showArchived: true }} | ||
| ${'search present'} | ${{ search: 'test' }} | ||
| ${'is_enabled set'} | ${{ is_enabled: true }} | ||
| ${'owners present'} | ${{ owners: [1] }} | ||
| `('returns true when $scenario', ({ overrides }) => { | ||
| expect(hasActiveFilters(createDefaultFilters(overrides))).toBe(true) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.