-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLEIGHT-32: new component-local validation -- validation and transien…
…t state should not be part of the redux store
- Loading branch information
Showing
26 changed files
with
445 additions
and
242 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,11 @@ | ||
import { ErrorCode } from './error-codes'; | ||
import { SleightError } from './SleightError'; | ||
|
||
export class ImproperContextUsageError extends SleightError { | ||
constructor() { | ||
super( | ||
ErrorCode.IMPROPER_CONTEXT_USAGE, | ||
ErrorCode.IMPROPER_CONTEXT_USAGE.toString() | ||
); | ||
} | ||
} |
This file contains 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,8 @@ | ||
import { ErrorCode } from './error-codes'; | ||
import { SleightError } from './SleightError'; | ||
|
||
export class NotImplementedError extends SleightError { | ||
constructor() { | ||
super(ErrorCode.NOT_IMPLEMENTED, ErrorCode.NOT_IMPLEMENTED.toString()); | ||
} | ||
} |
This file contains 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,12 @@ | ||
import { RoleKeyActionType } from '../features/model/role-key/role-key-editing-context'; | ||
import { ErrorCode } from './error-codes'; | ||
import { SleightError } from './SleightError'; | ||
|
||
export class UnhandledRoleKeyEditingEventTypeError extends SleightError { | ||
constructor(type: RoleKeyActionType) { | ||
super( | ||
ErrorCode.UNHANDLED_ROLE_KEY_EDITING_EVENT_TYPE, | ||
RoleKeyActionType[type] | ||
); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
export enum ErrorCode { | ||
SELECTOR_ID_NOT_FOUND, | ||
SELECTOR_ID_ALREADY_IN_USE, | ||
IMPROPER_CONTEXT_USAGE = 'do not use default context impl - provide values to context provider in JSX instead', | ||
|
||
NOT_EDITING_AN_ACTION, | ||
NOT_EDITING_AN_ACTION = 'not editing an action', | ||
NOT_IMPLEMENTED = 'method not implemented', | ||
|
||
UNHANDLED_ACTION_TYPE, | ||
UNHANDLED_ACTION_VALUE_TYPE, | ||
UNHANDLED_ACTION_VALUE_OPERATION, | ||
UNHANDLED_SEND_KEY_FIELD, | ||
UNHANDLED_SEND_KEY_MODE, | ||
UNHANDLED_SEND_KEY_MODIFIER_TYPE, | ||
UNHANDLED_SPEC_ITEM_TYPE, | ||
UNHANDLED_VARIABLE_TYPE, | ||
SELECTOR_ID_ALREADY_IN_USE = 'selector id already in use', | ||
SELECTOR_ID_NOT_FOUND = 'selector id not found', | ||
|
||
UNHANDLED_ACTION_TYPE = 'unhandled action type', | ||
UNHANDLED_ACTION_VALUE_OPERATION = 'unhandled action value operation', | ||
UNHANDLED_ACTION_VALUE_TYPE = 'unhandled action value type', | ||
UNHANDLED_ROLE_KEY_EDITING_EVENT_TYPE = 'unhandled role key editing event type', | ||
UNHANDLED_SPEC_ITEM_TYPE = 'unhandled spec item type', | ||
UNHANDLED_SEND_KEY_FIELD = 'unhandled send key field', | ||
UNHANDLED_SEND_KEY_MODE = 'unhandled send key mode', | ||
UNHANDLED_SEND_KEY_MODIFIER_TYPE = 'unhandled send key modifier type', | ||
UNHANDLED_VARIABLE_TYPE = 'unhandled variable type', | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
type Focus = { | ||
elementType: string | null; | ||
elementType: string | undefined; | ||
}; | ||
|
||
const initialState: Focus = { | ||
elementType: null, | ||
elementType: undefined, | ||
}; | ||
|
||
const focusSlice = createSlice({ | ||
name: 'focus', | ||
initialState, | ||
reducers: { | ||
setFocus: (state, action: PayloadAction<string>) => { | ||
setFocus: (state, action: PayloadAction<string | undefined>) => { | ||
state.elementType = action.payload; | ||
}, | ||
clearFocus: (state) => { | ||
state.elementType = null; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setFocus, clearFocus } = focusSlice.actions; | ||
export const { setFocus } = focusSlice.actions; | ||
export const focusReducer = focusSlice.reducer; |
This file contains 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 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,17 @@ | ||
import React from 'react'; | ||
import { ImproperContextUsageError } from '../../../error/ImproperContextUsageError'; | ||
|
||
interface EditingData<P> { | ||
// P is a payload | ||
localDispatchFn: React.Dispatch<P>; | ||
} | ||
const createDefaultEditingDataState = <P>(): EditingData<P> => { | ||
return { | ||
localDispatchFn: (p: P) => { | ||
throw new ImproperContextUsageError(); | ||
}, | ||
}; | ||
}; | ||
export const createEditingContext = <T>(): React.Context<EditingData<T>> => { | ||
return React.createContext(createDefaultEditingDataState<T>()); | ||
}; |
This file contains 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { notEmpty } from '../../../validation/common-validations'; | ||
import { notEmptyPredicate } from '../../../validation/common-validations'; | ||
import { | ||
createValidationError, | ||
Validator, | ||
} from '../../../validation/validator'; | ||
|
||
export const contextMatcherValidator: Validator<string> = { | ||
test: notEmpty, | ||
isValid: notEmptyPredicate, | ||
error: createValidationError("matcher can't be empty"), | ||
}; |
This file contains 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
71 changes: 71 additions & 0 deletions
71
src/features/model/role-key/RoleKeyParentComponent.test.tsx
This file contains 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,71 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import App from '../../../App'; | ||
import { store } from '../../../app/store'; | ||
import { Field } from '../../../validation/validation-field'; | ||
|
||
beforeEach(() => { | ||
render( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
); | ||
}); | ||
|
||
const clickRoleKeysSidebarSection = () => { | ||
const roleKeysSidebarSection = | ||
screen.getByText<HTMLButtonElement>('Role Keys'); | ||
fireEvent.click(roleKeysSidebarSection); | ||
}; | ||
|
||
const clickCreateNewRoleKey = () => { | ||
const createNewRoleKeyButton = screen.getByText('Create New Role Key'); | ||
fireEvent.click(createNewRoleKeyButton); | ||
}; | ||
|
||
const getRoleKeyInputField = (): HTMLInputElement => { | ||
const qs = `input[name="${Field[Field.RK_ROLE_KEY]}"]`; | ||
return document.querySelector<HTMLInputElement>(qs) as HTMLInputElement; | ||
}; | ||
|
||
describe('role key component tests', () => { | ||
it('should handle create new', () => { | ||
clickRoleKeysSidebarSection(); | ||
clickCreateNewRoleKey(); | ||
|
||
const input = getRoleKeyInputField(); | ||
|
||
expect(input?.value).toBe(''); | ||
}); | ||
|
||
it('should not save if validation errors', () => { | ||
clickRoleKeysSidebarSection(); | ||
clickCreateNewRoleKey(); | ||
// there will be a validation error for the role key being empty | ||
|
||
const saveButton = screen.getByText<HTMLButtonElement>('Save'); | ||
fireEvent.click(saveButton); | ||
|
||
expect(saveButton).toBeDisabled(); | ||
}); | ||
|
||
it('should invalidate empty role key', () => { | ||
clickRoleKeysSidebarSection(); | ||
clickCreateNewRoleKey(); | ||
|
||
const input = getRoleKeyInputField(); | ||
fireEvent.blur(input); | ||
|
||
expect(input).toHaveClass('is-invalid'); | ||
}); | ||
|
||
it('should validate non-empty role key', () => { | ||
clickRoleKeysSidebarSection(); | ||
clickCreateNewRoleKey(); | ||
|
||
const input = getRoleKeyInputField(); | ||
fireEvent.change(input, { target: { value: 'a' } }); | ||
|
||
expect(input).not.toHaveClass('is-invalid'); | ||
}); | ||
}); |
Oops, something went wrong.