-
Notifications
You must be signed in to change notification settings - Fork 51
[test]: unit-test for batch edit basic fields #8500
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
kwhuber
wants to merge
11
commits into
main
Choose a base branch
from
issue-8499
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.
+182
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab97fee
[test]: rough sketch of unit test
kwhuber f31587b
Potential fix for pull request finding 'CodeQL / Unused variable, imp…
CarolineDenis 57fb8d5
Potential fix for pull request finding 'CodeQL / Unused variable, imp…
CarolineDenis d6188dc
[test]: WIP of disable relationships for batch edit and enable rollba…
kwhuber 5d438c5
Merge branch 'main' into issue-8499
kwhuber 6fff37d
Merge remote-tracking branch 'origin/issue-8499' into issue-8499
kwhuber 341e3e5
test commit
kwhuber 0cc0e45
more WIP of disable relationships for batch edit and enable rollback …
kwhuber 1c3de4d
[test]: disable relationships for batch edit and enable rollback in u…
kwhuber 954e480
[fix]: small change for BE relationships and rollback permissions
kwhuber 320ae37
[test]: WIP of create a query; very relationsips are not editable
kwhuber 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
73 changes: 73 additions & 0 deletions
73
specifyweb/frontend/js_src/lib/components/BatchEdit/__tests__/index.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,73 @@ | ||
| import { requireContext } from '../../../tests/helpers'; | ||
| import type { RA } from '../../../utils/types'; | ||
| import { tables } from '../../DataModel/tables'; | ||
| import type { QueryField } from '../../QueryBuilder/helpers'; | ||
| import type { MappingPath } from '../../WbPlanView/Mapper'; | ||
| import { buildBatchEditFromQueryBody } from '../index'; | ||
|
|
||
| requireContext(); | ||
|
|
||
| const queryField = ( | ||
| mappingPath: MappingPath, | ||
| isDisplay: boolean = true | ||
| ): QueryField => ({ | ||
| id: 0, | ||
| mappingPath, | ||
| sortType: undefined, | ||
| isDisplay, | ||
| filters: [], | ||
| }); | ||
|
|
||
| /* A query over Collection Object that reaches through a to-one relationship | ||
| * (accession), a to-many one (accession agents), and an unchecked field. | ||
| */ | ||
| const fields: RA<QueryField> = [ | ||
| queryField(['catalogNumber']), | ||
| queryField(['guid'], false), // Unchecked in the query builder | ||
| queryField(['accession', 'accessionNumber']), | ||
| queryField(['accession', 'accessionAgents', '#1', 'role']), | ||
| ]; | ||
|
|
||
| const buildBody = ( | ||
| hasRelationships: boolean, | ||
| extraFields: RA<QueryField> = fields | ||
| ) => | ||
| buildBatchEditFromQueryBody({ | ||
| query: new tables.SpQuery.Resource({ name: 'Test Query' }), | ||
| limit: 5000, | ||
| fields: extraFields, | ||
| baseTableName: 'CollectionObject', | ||
| dataSetName: 'Test Query - Fri Sep 11 2026', | ||
| recordSetId: 42, | ||
| treeDefsFilter: {}, | ||
| hasRelationships, | ||
| }); | ||
|
|
||
| describe('buildBatchEditFromQueryBody', () => { | ||
| test('captions describe each displayed field with relationships', () => { | ||
| expect(buildBody(true).captions).toEqual([ | ||
| 'Cat #', | ||
| 'Accession #', | ||
| 'Accession Agents - Role', | ||
| ]); | ||
| }); | ||
|
|
||
| test('fields hidden in the query builder get no caption', () => { | ||
| const captions = buildBody(true).captions; | ||
| expect(captions).toHaveLength(3); | ||
| expect(captions).not.toContain('Collection Object - GUID'); | ||
| }); | ||
|
|
||
| // Covers front end half of verifying relationships are not editable. | ||
| test('omitRelationships is the inverse of the preference', () => { | ||
| expect(buildBody(true).omitrelationships).toBe(false); | ||
| expect(buildBody(false).omitrelationships).toBe(true); | ||
| }); | ||
|
|
||
| test('carries the data set name, limit and record set through', () => { | ||
| const body = buildBody(true); | ||
| expect(body.name).toBe('Test Query - Fri Sep 11 2026'); | ||
| expect(body.limit).toBe(5000); | ||
| expect(body.recordsetid).toBe(42); | ||
| }); | ||
| }); |
109 changes: 109 additions & 0 deletions
109
specifyweb/frontend/js_src/lib/components/WbUtils/__tests__/datasetVariants.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,109 @@ | ||
| import { hasPermission } from '../../Permissions/helpers'; | ||
| import { userPreferences } from '../../Preferences/userPreferences'; | ||
| import { datasetVariants } from '../datasetVariants'; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| jest.mock('../../Permissions/helpers', () => ({ | ||
| hasPermission: jest.fn(), | ||
| })); | ||
|
|
||
| const mockedHasPermission = hasPermission as jest.Mock; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| const setBatchEditPreferences = ( | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| enableRelationships: boolean, | ||
| showRollback: boolean | ||
| ): void => { | ||
| userPreferences.set( | ||
| 'batchEdit', | ||
| 'editor', | ||
| 'enableRelationships', | ||
| enableRelationships | ||
| ); | ||
| userPreferences.set('batchEdit', 'editor', 'showRollback', showRollback); | ||
| }; | ||
|
|
||
| // Drop every explicitly set preference so lookups fall back to the defaults | ||
| const resetPreferences = (): void => userPreferences.setRaw({}); | ||
|
|
||
| beforeAll(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('batch edit rollback availability', () => { | ||
| test.each([ | ||
| [false, true, true, true], | ||
| [true, true, true, false], | ||
| [false, false, true, false], | ||
| [false, true, false, false], | ||
| [true, false, true, false], | ||
| [true, true, false, false], | ||
| [false, false, false, false], | ||
| [true, false, false, false], | ||
| ])( | ||
| 'enableRelationships=%s showRollback=%s permission=%s -> canUndo=%s', | ||
| (enableRelationships, showRollback, permission, expected) => { | ||
| setBatchEditPreferences(enableRelationships, showRollback); | ||
| mockedHasPermission.mockReturnValue(permission); | ||
|
|
||
| expect(datasetVariants.batchEdit.canUndo()).toBe(expected); | ||
| } | ||
| ); | ||
|
|
||
| test('checks the batch edit rollback permission', () => { | ||
| setBatchEditPreferences(false, true); | ||
| mockedHasPermission.mockReturnValue(true); | ||
|
|
||
| datasetVariants.batchEdit.canUndo(); | ||
|
|
||
| expect(mockedHasPermission).toHaveBeenCalledWith( | ||
| '/batch_edit/dataset', | ||
| 'rollback' | ||
| ); | ||
| }); | ||
|
|
||
| test('does not consult permissions when relationships are enabled', () => { | ||
| setBatchEditPreferences(true, true); | ||
| mockedHasPermission.mockReturnValue(true); | ||
|
|
||
| expect(datasetVariants.batchEdit.canUndo()).toBe(false); | ||
| expect(mockedHasPermission).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('rollback is hidden based off default preferences', () => { | ||
| resetPreferences(); | ||
|
|
||
| expect( | ||
| userPreferences.definition('batchEdit', 'editor', 'enableRelationships') | ||
| .defaultValue | ||
| ).toBe(true); | ||
| expect( | ||
| userPreferences.definition('batchEdit', 'editor', 'showRollback') | ||
| .defaultValue | ||
| ).toBe(true); | ||
|
|
||
| // Unset preferences resolve to the declared defaults | ||
| expect( | ||
| userPreferences.get('batchEdit', 'editor', 'enableRelationships') | ||
| ).toBe(true); | ||
| expect(userPreferences.get('batchEdit', 'editor', 'showRollback')).toBe( | ||
| true | ||
| ); | ||
|
|
||
| expect(datasetVariants.batchEdit.canUndo()).toBe(false); | ||
| expect(mockedHasPermission).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('workbench rollback is not affected by batch edit preferences', () => { | ||
| setBatchEditPreferences(true, true); | ||
| mockedHasPermission.mockReturnValue(true); | ||
|
|
||
| expect(datasetVariants.workbench.canUndo()).toBe(true); | ||
| expect(mockedHasPermission).toHaveBeenCalledWith( | ||
| '/workbench/dataset', | ||
| 'unupload' | ||
| ); | ||
| }); | ||
| }); | ||
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.