Skip to content

Commit

Permalink
Fix JS QA
Browse files Browse the repository at this point in the history
  • Loading branch information
widoz committed Jul 20, 2024
1 parent 1a68941 commit 334de60
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 99 deletions.
13 changes: 6 additions & 7 deletions sources/client/src/api/search-entities-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ export async function searchEntitiesOptions< E >(
postTypes: Kensaku.Kind< string >,
queryArguments?: Kensaku.QueryArguments
): Promise< Set< Kensaku.ControlOption< E > > > {
const postsEntities =
await searchEntities< Kensaku.SearchEntityFields >(
type,
postTypes,
phrase,
queryArguments
);
const postsEntities = await searchEntities< Kensaku.SearchEntityFields >(
type,
postTypes,
phrase,
queryArguments
);

const { fields = [] } = queryArguments ?? {};
const [ label = 'title', value = 'id', ...extraFields ] = fields;
Expand Down
5 changes: 1 addition & 4 deletions sources/client/src/components/composite-entities-by-kind.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ export function CompositeEntitiesByKind< E, K >(
} );
} )
.catch( ( error ) => {
doAction(
'kensaku.on-change-entities.error',
error
);
doAction( 'kensaku.on-change-entities.error', error );
} );
};

Expand Down
4 changes: 1 addition & 3 deletions sources/client/src/components/preset-entities-by-kind.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ type EntitiesFinder = (
type EntitiesComponent = React.ComponentType<
Kensaku.BaseControl< EntitiesValue >
>;
type KindComponent = React.ComponentType<
Kensaku.BaseControl< KindValue >
>;
type KindComponent = React.ComponentType< Kensaku.BaseControl< KindValue > >;

type PublicComponentProps = {
entitiesFinder: EntitiesFinder;
Expand Down
4 changes: 1 addition & 3 deletions sources/client/src/components/search-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import { __ } from '@wordpress/i18n';
*/
import { useId } from '../hooks/use-id';

export function SearchControl(
props: Kensaku.SearchControl
): JSX.Element {
export function SearchControl( props: Kensaku.SearchControl ): JSX.Element {
const id = useId( props.id );
const label = props.label || __( 'Search', 'kensaku' );
const [ searchValue, setSearchValue ] = React.useState( '' );
Expand Down
15 changes: 3 additions & 12 deletions sources/client/src/hooks/use-entities-options-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ type _Reducer< E, K > = Reducer<
* @param searchEntities The function that will search the entities
*/
export function useEntitiesOptionsStorage< E, K >(
initialState: Pick<
Kensaku.EntitiesState< E, K >,
'entities' | 'kind'
>,
initialState: Pick< Kensaku.EntitiesState< E, K >, 'entities' | 'kind' >,
searchEntities: Kensaku.SearchEntitiesFunction< E, K >
): Readonly<
[
Kensaku.EntitiesState< E, K >,
Dispatch< Kensaku.StoreAction< E, K > >,
]
[ Kensaku.EntitiesState< E, K >, Dispatch< Kensaku.StoreAction< E, K > > ]
> {
const [ state, dispatch ] = useReducer< _Reducer< E, K > >(
reducer,
Expand Down Expand Up @@ -69,10 +63,7 @@ export function useEntitiesOptionsStorage< E, K >(
} );
} )
.catch( ( error ) => {
doAction(
'kensaku.on-storage-initialization.error',
error
);
doAction( 'kensaku.on-storage-initialization.error', error );
} );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );
Expand Down
8 changes: 5 additions & 3 deletions sources/client/src/hooks/use-query-viewable-post-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { useEntityRecords } from './use-entity-records';
* @public
*/
export function useQueryViewablePostTypes(): Kensaku.EntitiesRecords< Kensaku.ViewablePostType > {
const entitiesRecords = useEntityRecords<
Kensaku.PostType< 'edit' >
>( 'root', 'postType', { per_page: -1 } );
const entitiesRecords = useEntityRecords< Kensaku.PostType< 'edit' > >(
'root',
'postType',
{ per_page: -1 }
);

const viewablePostTypes = entitiesRecords
.records()
Expand Down
8 changes: 5 additions & 3 deletions sources/client/src/hooks/use-query-viewable-taxonomies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { useEntityRecords } from './use-entity-records';
* @public
*/
export function useQueryViewableTaxonomies(): Kensaku.EntitiesRecords< Kensaku.ViewableTaxonomy > {
const entitiesRecords = useEntityRecords<
Kensaku.Taxonomy< 'edit' >
>( 'root', 'taxonomy', { per_page: -1 } );
const entitiesRecords = useEntityRecords< Kensaku.Taxonomy< 'edit' > >(
'root',
'taxonomy',
{ per_page: -1 }
);

const viewableTaxonomies = entitiesRecords
.records()
Expand Down
8 changes: 2 additions & 6 deletions sources/client/src/hooks/use-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import { doAction } from '@wordpress/hooks';
*/
import { Set } from '../models/set';

type SearchPhrase = Parameters<
Kensaku.SearchControl[ 'onChange' ]
>[ 0 ];
type SearchPhrase = Parameters< Kensaku.SearchControl[ 'onChange' ] >[ 0 ];
type SearchFunc = ( phrase: SearchPhrase ) => void;

/**
Expand Down Expand Up @@ -50,9 +48,7 @@ export function useSearch< E, K >(
)
.catch( ( error ) => {
doAction( 'kensaku.on-search.error', error );
const emptySet = new Set<
Kensaku.ControlOption< E >
>();
const emptySet = new Set< Kensaku.ControlOption< E > >();
dispatch( {
type: 'UPDATE_CURRENT_ENTITIES_OPTIONS',
currentEntitiesOptions: emptySet,
Expand Down
18 changes: 7 additions & 11 deletions sources/client/src/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,10 @@ addAction(
}
);

addAction(
'kensaku.on-search.error',
'kensaku/on-search.error',
( error ) => {
console.error(
`Composite Entities by Kind - on Search Entities: ${
error.message ?? error
}`
);
}
);
addAction( 'kensaku.on-search.error', 'kensaku/on-search.error', ( error ) => {
console.error(
`Composite Entities by Kind - on Search Entities: ${
error.message ?? error
}`
);
} );
3 changes: 1 addition & 2 deletions sources/client/src/storage/entities/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export function reducer< E, K >(
...state,
selectedEntitiesOptions: action.selectedEntitiesOptions,
entities: action.selectedEntitiesOptions.map(
( option: Kensaku.ControlOption< E > ) =>
option.value
( option: Kensaku.ControlOption< E > ) => option.value
),
};

Expand Down
4 changes: 1 addition & 3 deletions sources/client/src/value-objects/control-option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import Kensaku from '@types';
import { assert } from '../utils/assert';
import { ImmutableRecord } from '../models/immutable-record';

export class ControlOption< V >
implements Kensaku.EnrichedControlOption< V >
{
export class ControlOption< V > implements Kensaku.EnrichedControlOption< V > {
public readonly label: string;
public readonly value: V;
public readonly extra: Kensaku.Record< unknown >;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ describe( 'CompositeEntitiesByKind', () => {
const rendered = render(
<CompositeEntitiesByKind
searchEntities={ () =>
Promise.resolve(
new Set< Kensaku.ControlOption< any > >()
)
Promise.resolve( new Set< Kensaku.ControlOption< any > >() )
}
entities={ {
value: new Set(),
Expand Down Expand Up @@ -96,9 +94,7 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set<
Kensaku.ControlOption< any >
>(),
options: new Set< Kensaku.ControlOption< any > >(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -178,9 +174,7 @@ describe( 'CompositeEntitiesByKind', () => {
} );

it( 'Pass to the children the updated entities options when the kind change', async () => {
let expectedEntities = new Set<
Kensaku.ControlOption< string >
>();
let expectedEntities = new Set< Kensaku.ControlOption< string > >();

const rendered = await act( () =>
render(
Expand Down Expand Up @@ -325,9 +319,7 @@ describe( 'CompositeEntitiesByKind', () => {
{ label: 'Post 2', value: 'post-2' },
] )
)
) as jest.Mock<
Kensaku.SearchEntitiesFunction< string, string >
>;
) as jest.Mock< Kensaku.SearchEntitiesFunction< string, string > >;

const rendered = await act( () =>
render(
Expand All @@ -341,9 +333,7 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set<
Kensaku.ControlOption< any >
>(),
options: new Set< Kensaku.ControlOption< any > >(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -373,10 +363,7 @@ describe( 'CompositeEntitiesByKind', () => {
const selectedEntities = new Set( [ '1', '2' ] );

const searchEntities = jest.fn() as jest.Mock<
Kensaku.CompositeEntitiesKinds<
string,
string
>[ 'searchEntities' ]
Kensaku.CompositeEntitiesKinds< string, string >[ 'searchEntities' ]
>;

await act( () =>
Expand All @@ -389,9 +376,7 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set<
Kensaku.ControlOption< any >
>(),
options: new Set< Kensaku.ControlOption< any > >(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -424,9 +409,7 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set<
Kensaku.ControlOption< any >
>(),
options: new Set< Kensaku.ControlOption< any > >(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -498,9 +481,7 @@ describe( 'CompositeEntitiesByKind', () => {
const rendered = render(
<CompositeEntitiesByKind
searchEntities={ () =>
Promise.resolve(
new Set< Kensaku.ControlOption< any > >()
)
Promise.resolve( new Set< Kensaku.ControlOption< any > >() )
}
entities={ {
value: new Set(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ describe( 'Post Types Select', () => {
* in the React Select component, so we mock it.
*/
it( 'call the given onChange handler', ( done ) => {
let expectedValue: Kensaku.SingularControl< string >[ 'value' ] =
'';
let expectedValue: Kensaku.SingularControl< string >[ 'value' ] = '';
const option: Kensaku.ControlOption< string > = {
label: faker.word.words( 2 ),
value: faker.word.noun(),
Expand Down
12 changes: 3 additions & 9 deletions tests/client/unit/hooks/use-entities-options-storage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ describe( 'Use Posts Options Storage', () => {
},
] )
)
) as jest.Mock<
Kensaku.SearchEntitiesFunction< number, string >
>;
) as jest.Mock< Kensaku.SearchEntitiesFunction< number, string > >;

const Component = () => {
useEntitiesOptionsStorage< number, string >(
Expand Down Expand Up @@ -103,9 +101,7 @@ describe( 'Use Posts Options Storage', () => {
return options?.include
? Promise.resolve( selectedEntitiesOptions )
: Promise.resolve( currentEntitiesOptions );
} ) as jest.Mock<
Kensaku.SearchEntitiesFunction< number, string >
>;
} ) as jest.Mock< Kensaku.SearchEntitiesFunction< number, string > >;

const dispatch = jest.fn();
jest.spyOn( React, 'useReducer' ).mockImplementation( ( _, state ) => [
Expand Down Expand Up @@ -189,9 +185,7 @@ describe( 'Use Posts Options Storage', () => {
},
] )
)
) as jest.Mock<
Kensaku.SearchEntitiesFunction< number, string >
>;
) as jest.Mock< Kensaku.SearchEntitiesFunction< number, string > >;

const Component = () => {
useEntitiesOptionsStorage< number, string >(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ describe( 'Ordered Selected Options at the Top', () => {
/*
* Given options are always re-ordered to includes the given collection values at the top
*/
function generateOptions(): Array<
Kensaku.ControlOption< string >
> {
function generateOptions(): Array< Kensaku.ControlOption< string > > {
const options: Array< Kensaku.ControlOption< string > > = [];

for ( let i = 1; i <= 10; i++ ) {
Expand Down

0 comments on commit 334de60

Please sign in to comment.