-
Notifications
You must be signed in to change notification settings - Fork 466
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
13
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,463
−6,510
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2345d53
fix: handle versioned environments in feature toggle
talissoncosta 3e6b489
fix: align feature_segment filter for versioned environment toggle
talissoncosta bc64f6e
fix: initialize FeatureListStore for CreateFlag modal in versioned en…
talissoncosta a3b37b1
fix: use api_key instead of id for environment lookup in toggle
talissoncosta a0b61f6
fix: pass numeric environmentId for versioned toggle API calls
talissoncosta 0ffde92
feat(testing): add Jest unit testing infrastructure
talissoncosta 0c7529c
ci: add frontend PR workflow with unit tests
talissoncosta 9f9f825
test(format): add unit tests and clean up redundant .toString() calls
talissoncosta 01efdca
refactor(format): add moment import and fix money function types
talissoncosta 0947abc
fix(ci): address PR review feedback
talissoncosta 3e3fab2
fix: remove unrelated FeaturesPage changes from PR
talissoncosta 5bd4b19
fix(ci): add permissions block to frontend-pull-request workflow
talissoncosta c6e25db
chore(ci): align checkout action to v5 in both workflows
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
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,33 @@ | ||
| 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 | ||
|
|
||
| jobs: | ||
| unit-tests: | ||
| name: Unit Tests | ||
| runs-on: ubuntu-latest | ||
|
|
||
| defaults: | ||
| run: | ||
| working-directory: frontend | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - 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 | ||
|
||
245 changes: 245 additions & 0 deletions
245
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,245 @@ | ||
| // 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('should set is_archived to "false" when showArchived is false', () => { | ||
| const filters = createDefaultFilters({ showArchived: false }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.is_archived).toBe('false') | ||
| }) | ||
|
|
||
| it('should set is_archived to "true" when showArchived is true', () => { | ||
| const filters = createDefaultFilters({ showArchived: true }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.is_archived).toBe('true') | ||
| }) | ||
|
|
||
| it('should always include is_archived (never undefined)', () => { | ||
| const filters = createDefaultFilters() | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.is_archived).toBeDefined() | ||
| expect(result.is_archived).not.toBeUndefined() | ||
| }) | ||
|
|
||
| it('should include page number', () => { | ||
| const filters = createDefaultFilters() | ||
| const result = buildUrlParams(filters, 5) | ||
| expect(result.page).toBe(5) | ||
| }) | ||
|
|
||
| it('should include 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('should include tags when present', () => { | ||
| const filters = createDefaultFilters({ tags: [1, 2, 3] }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.tags).toBe('1,2,3') | ||
| }) | ||
|
|
||
| it('should exclude empty arrays', () => { | ||
| const filters = createDefaultFilters({ owners: [], tags: [] }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.tags).toBeUndefined() | ||
| expect(result.owners).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should include search when present', () => { | ||
| const filters = createDefaultFilters({ search: 'test' }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.search).toBe('test') | ||
| }) | ||
|
|
||
| it('should exclude empty search', () => { | ||
| const filters = createDefaultFilters({ search: '' }) | ||
| const result = buildUrlParams(filters, 1) | ||
| expect(result.search).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('buildApiFilterParams', () => { | ||
| const mockResolver = (apiKey: string) => | ||
| apiKey === 'test-key' ? 123 : undefined | ||
|
|
||
| it('should set is_archived to "false" when showArchived is false', () => { | ||
| const filters = createDefaultFilters({ showArchived: false }) | ||
| const result = buildApiFilterParams( | ||
| filters, | ||
| 1, | ||
| 'test-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result?.is_archived).toBe('false') | ||
| }) | ||
|
|
||
| it('should set is_archived to "true" when showArchived is true', () => { | ||
| const filters = createDefaultFilters({ showArchived: true }) | ||
| const result = buildApiFilterParams( | ||
| filters, | ||
| 1, | ||
| 'test-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result?.is_archived).toBe('true') | ||
| }) | ||
|
|
||
| it('should always include is_archived in params (never undefined)', () => { | ||
| const filters = createDefaultFilters() | ||
| const result = buildApiFilterParams( | ||
| filters, | ||
| 1, | ||
| 'test-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result).not.toBeNull() | ||
| expect(result?.is_archived).toBeDefined() | ||
| expect(result?.is_archived).toBe('false') | ||
| }) | ||
|
|
||
| it('should return null when environment ID cannot be resolved', () => { | ||
| const filters = createDefaultFilters() | ||
| const result = buildApiFilterParams( | ||
| filters, | ||
| 1, | ||
| 'invalid-key', | ||
| 1, | ||
| mockResolver, | ||
| ) | ||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should include environmentId and projectId', () => { | ||
| const filters = createDefaultFilters() | ||
| const result = buildApiFilterParams( | ||
| filters, | ||
| 1, | ||
| 'test-key', | ||
| 42, | ||
| mockResolver, | ||
| ) | ||
| expect(result?.environmentId).toBe('123') | ||
| expect(result?.projectId).toBe(42) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getFiltersFromParams', () => { | ||
| it('should parse is_archived=true to showArchived=true', () => { | ||
| const params = { is_archived: 'true' } | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.showArchived).toBe(true) | ||
| }) | ||
|
|
||
| it('should parse is_archived=false to showArchived=false', () => { | ||
| const params = { is_archived: 'false' } | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.showArchived).toBe(false) | ||
| }) | ||
|
|
||
| it('should default showArchived to false when is_archived is not present', () => { | ||
| const params = {} | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.showArchived).toBe(false) | ||
| }) | ||
|
|
||
| it('should parse page number', () => { | ||
| const params = { page: '3' } | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.page).toBe(3) | ||
| }) | ||
|
|
||
| it('should default page to 1 when not present', () => { | ||
| const params = {} | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.page).toBe(1) | ||
| }) | ||
|
|
||
| it('should parse tags as array of numbers', () => { | ||
| const params = { tags: '1,2,3' } | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.tags).toEqual([1, 2, 3]) | ||
| }) | ||
|
|
||
| it('should parse sort order', () => { | ||
| const params = { sortBy: 'created_date', sortOrder: 'desc' } | ||
| const result = getFiltersFromParams(params) | ||
| expect(result.sort.sortBy).toBe('created_date') | ||
| expect(result.sort.sortOrder).toBe(SortOrder.DESC) | ||
| }) | ||
| }) | ||
|
|
||
| describe('hasActiveFilters', () => { | ||
| it('should return false for default filters', () => { | ||
| const filters = createDefaultFilters() | ||
| expect(hasActiveFilters(filters)).toBe(false) | ||
| }) | ||
|
|
||
| it('should return true when tags are present', () => { | ||
| const filters = createDefaultFilters({ tags: [1] }) | ||
| expect(hasActiveFilters(filters)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return true when showArchived is true', () => { | ||
| const filters = createDefaultFilters({ showArchived: true }) | ||
| expect(hasActiveFilters(filters)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return true when search is present', () => { | ||
| const filters = createDefaultFilters({ search: 'test' }) | ||
| expect(hasActiveFilters(filters)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return true when is_enabled is set', () => { | ||
| const filters = createDefaultFilters({ is_enabled: true }) | ||
| expect(hasActiveFilters(filters)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return true when owners are present', () => { | ||
| const filters = createDefaultFilters({ owners: [1] }) | ||
| expect(hasActiveFilters(filters)).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.