Skip to content

Commit

Permalink
refs #91, spec uniqueness case insensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
synkarius committed Dec 1, 2022
1 parent 44ded96 commit 64247c0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/core/validators/spec-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { mapSpecToPreview } from '../../ui/model/spec/spec-preview';
import { container } from '../../di/config/brandi-config';
import { Tokens } from '../../di/config/brandi-tokens';
import { VariableDTO } from '../../data/model/variable/variable-dto';

const roleKeyTakenValidator = createRoleKeyTakenValidator<SpecDTO, Spec>(
Field.SP_ROLE_KEY,
Expand Down Expand Up @@ -160,6 +161,13 @@ const deletionValidator: FieldValidator<Spec> = {
},
};

const getSpecPreviewComparable = (
spec: Spec,
variables: Readonly<Record<string, VariableDTO>>
): string => {
return mapSpecToPreview(spec, variables).toLowerCase();
};

/** Compares spec previews rather than full variable
* permutations. Probably good enough in most cases.
*/
Expand All @@ -174,9 +182,9 @@ const basicSpecUniquenessValidator: FieldValidator<Spec> = {
.map((sp) => specMapper.mapToDomain(sp, data.selectors))
.map((sp) => ({
name: sp.name,
preview: mapSpecToPreview(sp, data.variables),
preview: getSpecPreviewComparable(sp, data.variables),
}));
const editingSpecPreview = mapSpecToPreview(spec, data.variables);
const editingSpecPreview = getSpecPreviewComparable(spec, data.variables);
for (const existingSpecPreview of existingSpecPreviews) {
if (existingSpecPreview.preview === editingSpecPreview) {
return {
Expand Down
39 changes: 39 additions & 0 deletions src/test/resources/import-send-key-05.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"import05": {
"version": "0.1.0.alpha",
"actions": [],
"commands": [],
"contexts": [],
"fns": [],
"selectors": [
{
"id": "20951e4d-5541-4dd7-bac9-a7b299e8fe2d",
"items": [
{
"id": "62b7df75-5b84-4930-858e-e91a5e9cf7f6",
"value": "alpha"
}
]
}
],
"specs": [
{
"id": "0c9209eb-748b-4bee-95c8-3eebc9f9889a",
"name": "WG Spec / 'alpha' / spe-0c9209eb-748b",
"roleKey": "",
"enabled": true,
"locked": false,
"items": [
{
"id": "f85bb4c7-7a94-4d9c-9794-2d3ac425f579",
"itemType": "Selector",
"itemId": "20951e4d-5541-4dd7-bac9-a7b299e8fe2d",
"optional": false,
"grouped": false
}
]
}
],
"variables": []
}
}
31 changes: 30 additions & 1 deletion src/ui/model/spec/SpecComponent.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { spec03 } from '../../../test/resources/spec-03.json';
import { spec04 } from '../../../test/resources/spec-04.json';
import { variable01 } from '../../../test/resources/variable-01.json';
import { variable02 } from '../../../test/resources/variable-02.json';
import { castJsonForTest } from '../../../test/utils/import-test-json-util';
import { import05 } from '../../../test/resources/import-send-key-05.json';
import {
castJsonForTest,
loadTestData,
} from '../../../test/utils/import-test-json-util';
import { container } from '../../../di/config/brandi-config';
import { BrowserRouter } from 'react-router-dom';
import { fieldName } from '../../../validation/field-name';
Expand Down Expand Up @@ -566,6 +570,31 @@ describe('spec component tests', () => {
expect(saveButton).toBeDisabled();
});

it('should invalidate duplicate spec with different case', async () => {
loadTestData(import05);
doRender();

const addNewSpecItemButton = screen.getByText(ADD_NEW_SPEC_ITEM);
await user.click(addNewSpecItemButton);
const specTypeSelect = screen.getByRole('list', {
name: fieldName(Field.SP_ITEM_TYPE_SELECT),
});
await user.selectOptions(specTypeSelect, SpecItemType.Enum.SELECTOR);
const selectorInput = screen.getByRole('textbox', {
name: fieldName(Field.SP_ITEM_SELECTOR),
});
await user.type(selectorInput, 'ALPHA');
const saveButton = screen.getByRole('button', {
name: SAVE,
});
await user.click(saveButton);

const errorText = screen.getByText(getSpecUniquenessErrorRegex());

expect(errorText).toBeInTheDocument();
expect(saveButton).toBeDisabled();
});

it('should invalidate spec with zero non-optional spec items', async () => {
doRender();

Expand Down

0 comments on commit 64247c0

Please sign in to comment.