Skip to content

Commit a070b79

Browse files
committed
Complete Unit Tests
1 parent fc2df07 commit a070b79

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

@types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare namespace EntitiesSearch {
1717
type Options<V> = Set<ControlOption<V>>;
1818
type Value = string | number;
1919

20+
// TODO Can we convert QueryArguments to an Immutable Map?
2021
interface QueryArguments
2122
extends Partial<
2223
Readonly<{

tests/client/unit/api/search-posts.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* External dependencies
33
*/
4+
import EntitiesSearch from '@types';
45
import { faker } from '@faker-js/faker';
56
import { describe, it, expect, jest } from '@jest/globals';
67

@@ -26,6 +27,70 @@ describe( 'Search Posts', () => {
2627
convertEntitiesToControlOptions( stubs, 'title', 'id' ).toArray()
2728
);
2829
} );
30+
31+
it( 'Should return a Set of Control Option with the title and id and extra fields.', async () => {
32+
const stubs = stubEntities();
33+
jest.mocked( searchEntities ).mockResolvedValue( stubs );
34+
const posts = await searchPosts( 'Phrase', new Set( [ 'post' ] ), {
35+
// @ts-ignore
36+
fields: [ 'title', 'id', 'slug', 'post_content', 'post_excerpt' ],
37+
} );
38+
39+
expect( posts.toArray() ).toEqual(
40+
convertEntitiesToControlOptions(
41+
stubs,
42+
'title',
43+
'id',
44+
'slug',
45+
'post_content',
46+
'post_excerpt'
47+
).toArray()
48+
);
49+
} );
50+
51+
it( 'Use the given label and value for the Control Options', async () => {
52+
const stubs = new Set( [
53+
{
54+
post_excerpt: faker.lorem.word(),
55+
slug: faker.lorem.slug(),
56+
},
57+
] );
58+
59+
jest.mocked( searchEntities ).mockResolvedValue( stubs );
60+
61+
const posts = await searchPosts( 'Phrase', new Set( [ 'post' ] ), {
62+
// @ts-ignore
63+
fields: [ 'post_excerpt', 'slug' ],
64+
} );
65+
66+
expect( posts.toArray() ).toEqual(
67+
convertEntitiesToControlOptions(
68+
stubs,
69+
'post_excerpt',
70+
'slug'
71+
).toArray()
72+
);
73+
} );
74+
75+
it( 'Expect to call searchEntities with the given parameters', async () => {
76+
const postTypes = new Set( [ 'post' ] );
77+
const phrase = 'Phrase';
78+
const fields: EntitiesSearch.SearchQueryFields = [ 'title', 'id' ];
79+
const queryArguments = {
80+
fields,
81+
};
82+
83+
jest.mocked( searchEntities ).mockResolvedValue( stubEntities() );
84+
85+
await searchPosts( phrase, postTypes, queryArguments );
86+
87+
expect( searchEntities ).toHaveBeenCalledWith(
88+
'post',
89+
postTypes,
90+
phrase,
91+
queryArguments
92+
);
93+
} );
2994
} );
3095

3196
function stubEntities(): Set< { id: number; title: string } > {

0 commit comments

Comments
 (0)