Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
widoz committed Dec 28, 2023
1 parent 315bed5 commit f2eb479
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ declare namespace EntitiesSearch {
postsOptions: PostsState<V>['postsOptions'];
}
| {
type: 'SET_CONTEXUAL_POSTS_OPTIONS';
type: 'UPDATE_CONTEXUAL_POSTS_OPTIONS';
contextualPostsOptions: PostsState<V>['contexualPostsOptions'];
}
| {
Expand Down
8 changes: 4 additions & 4 deletions sources/js/src/components/composite-posts-post-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useState, useEffect } from '@wordpress/element';

import { usePostsOptionsStorage } from '../hooks/use-posts-options-storage';
import { orderSelectedOptionsAtTheTop } from '../utils/order-selected-options-at-the-top';
import { uniqueOrderedSet } from '../utils/unique-ordered-set';
import { uniqueControlOptions } from '../utils/unique-control-options';

type SearchPhrase = Parameters<EntitiesSearch.Search['search']>[0];
type PostType<V> = EntitiesSearch.PostTypeControl<V>['value'];
Expand Down Expand Up @@ -53,7 +53,7 @@ export function CompositePostsPostTypes<P, T>(
selectedPostsOptions,
});
dispatch({
type: 'SET_CONTEXUAL_POSTS_OPTIONS',
type: 'UPDATE_CONTEXUAL_POSTS_OPTIONS',
contextualPostsOptions: postsOptions,
});
dispatch({
Expand Down Expand Up @@ -147,7 +147,7 @@ export function CompositePostsPostTypes<P, T>(
})
.then((result) => {
dispatch({
type: 'SET_CONTEXUAL_POSTS_OPTIONS',
type: 'UPDATE_CONTEXUAL_POSTS_OPTIONS',
contextualPostsOptions: result,
});
dispatch({
Expand All @@ -165,7 +165,7 @@ export function CompositePostsPostTypes<P, T>(
...props.posts,
value: valuesState.posts,
options: orderSelectedOptionsAtTheTop<P>(
uniqueOrderedSet(
uniqueControlOptions(
state.selectedPostsOptions.merge(state.postsOptions)
),
valuesState.posts
Expand Down
2 changes: 1 addition & 1 deletion sources/js/src/storage/posts/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function reducer<P>(
action: EntitiesSearch.PostsAction<P>
): EntitiesSearch.PostsState<P> {
switch (action.type) {
case 'SET_CONTEXUAL_POSTS_OPTIONS':
case 'UPDATE_CONTEXUAL_POSTS_OPTIONS':
return {
...state,
contexualPostsOptions: action.contextualPostsOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EntitiesSearch from '@types';
import { OrderedSet } from 'immutable';

export function uniqueOrderedSet<V>(
export function uniqueControlOptions<V>(
set: OrderedSet<EntitiesSearch.ControlOption<V>>
): OrderedSet<EntitiesSearch.ControlOption<V>> {
const uniqueOptions = OrderedSet<EntitiesSearch.ControlOption<V>>(
Expand Down
19 changes: 19 additions & 0 deletions tests/js/unit/storage/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ describe('reducer', () => {
};
});

it('Update the Contextual Posts Options', () => {
const contextualPostsOptions = OrderedSet([
{
value: faker.number.int(10),
label: 'Post One',
},
{
value: faker.number.int({ min: 11, max: 20 }),
label: 'Post Two',
},
]);
const newState = reducer(state, {
type: 'UPDATE_CONTEXUAL_POSTS_OPTIONS',
contextualPostsOptions,
});

expect(newState.contexualPostsOptions).toEqual(contextualPostsOptions);
});

it('Update the Posts Options', () => {
const postsOptions = OrderedSet([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { faker } from '@faker-js/faker';
import { convertPostEntitiesToControlOptions } from '../../../../sources/js/src/utils/convert-post-entities-to-control-options';

describe('Convert Entities To Control Options', () => {
/*
* Convert Post Entities to Control Options
*/
it('correctly convert entities to control options', () => {
const rawEntities = [];
for (let count = 0; count < 10; ++count) {
Expand All @@ -35,4 +32,24 @@ describe('Convert Entities To Control Options', () => {
expect(options.includes(entity.id)).toEqual(true);
}
});

it('throws an error if the option label is not a string', () => {
const rawEntities = [];
for (let count = 0; count < 10; ++count) {
rawEntities.push(
fromPartial<EntitiesSearch.PostEntityFields>({
title: faker.word.noun(),
id: faker.number.int(),
})
);
}

const entities =
OrderedSet<EntitiesSearch.PostEntityFields>(rawEntities);

expect(() => {
// To make the test fail, we pass the id as the label key
convertPostEntitiesToControlOptions(entities, 'id', 'id');
}).toThrow();
});
});
23 changes: 23 additions & 0 deletions tests/js/unit/utils/unique-control-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import EntitiesSearch from '@types';
import { OrderedSet } from 'immutable';

import { describe, expect, it } from '@jest/globals';

import { uniqueControlOptions } from '../../../../sources/js/src/utils/unique-control-options';

describe('Unique Control Options', () => {
it('Do not allow same control options within the same set', () => {
const set = OrderedSet<EntitiesSearch.ControlOption<string>>([
{ label: 'foo', value: 'foo' },
{ label: 'bar', value: 'bar' },
{ label: 'foo', value: 'foo' },
{ label: 'bar', value: 'bar' },
]);

expect(set.size).toBe(4);
const uniqueSet = uniqueControlOptions(set);
expect(uniqueSet.size).toBe(2);
expect(uniqueSet.first()?.value).toBe('foo');
expect(uniqueSet.last()?.value).toBe('bar');
});
});

0 comments on commit f2eb479

Please sign in to comment.