1
1
/**
2
2
* External dependencies
3
3
*/
4
+ import EntitiesSearch from '@types' ;
4
5
import { faker } from '@faker-js/faker' ;
5
6
import { describe , it , expect , jest } from '@jest/globals' ;
6
7
@@ -26,6 +27,70 @@ describe( 'Search Posts', () => {
26
27
convertEntitiesToControlOptions ( stubs , 'title' , 'id' ) . toArray ( )
27
28
) ;
28
29
} ) ;
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
+ } ) ;
29
94
} ) ;
30
95
31
96
function stubEntities ( ) : Set < { id : number ; title : string } > {
0 commit comments