@@ -4,56 +4,78 @@ import Facebook from '../facebook';
44describe ( 'Share > Facebook' , ( ) => {
55 test ( 'url' , ( ) => {
66 const facebook = Facebook . create ( { } ) ;
7- Object . defineProperty ( facebook , 'entityLink' , { value : jest . fn ( ) , writable : true } ) ;
8- facebook . entityLink . mockReturnValueOnce ( 'https://demo.frontity.com/the-beauty-of-gullfoss' ) ;
7+ Object . defineProperty ( facebook , 'entityLink' , {
8+ value : jest . fn ( ) ,
9+ writable : true ,
10+ } ) ;
11+ facebook . entityLink . mockReturnValueOnce (
12+ 'https://demo.frontity.com/the-beauty-of-gullfoss' ,
13+ ) ;
914
10- expect ( facebook . url ( { type : 'post' , id : 60 , quote : 'q' , hashtag : '#tag' } ) ) . toMatchSnapshot ( ) ;
15+ expect (
16+ facebook . url ( { type : 'post' , id : 60 , quote : 'q' , hashtag : '#tag' } ) ,
17+ ) . toMatchSnapshot ( ) ;
1118 } ) ;
1219
13- test ( 'requestCount success' , done => {
14- const request = { get : jest . fn ( ) } ;
20+ test ( 'requestCount success' , async ( ) => {
21+ const request = {
22+ get : jest . fn ( ( ) => ( {
23+ accept : jest . fn ( ) . mockResolvedValueOnce ( {
24+ body : {
25+ share : {
26+ comment_count : 0 ,
27+ share_count : 123 ,
28+ } ,
29+ id : 'https://demo.frontity.com/the-beauty-of-gullfoss' ,
30+ } ,
31+ } ) ,
32+ } ) ) ,
33+ } ;
1534 const facebook = Facebook . create ( { } , { request } ) ;
16- Object . defineProperty ( facebook , 'entityLink' , { value : jest . fn ( ) , writable : true } ) ;
17- facebook . entityLink . mockReturnValueOnce ( 'https://demo.frontity.com/the-beauty-of-gullfoss' ) ;
18-
19- request . get . mockResolvedValueOnce ( {
20- body : {
21- share : {
22- comment_count : 0 ,
23- share_count : 123 ,
24- } ,
25- id : 'https://demo.frontity.com/the-beauty-of-gullfoss' ,
26- } ,
35+
36+ Object . defineProperty ( facebook , 'entityLink' , {
37+ value : jest . fn ( ) ,
38+ writable : true ,
2739 } ) ;
2840
29- when (
30- ( ) => facebook . count ( { type : 'post' , id : 60 } ) ,
31- ( ) => {
32- expect ( facebook . count ( { type : 'post' , id : 60 } ) ) . toBe ( 123 ) ;
33- expect ( facebook . entityLink . mock . calls ) . toMatchSnapshot ( ) ;
34- expect ( facebook . entityLink . mock . results ) . toMatchSnapshot ( ) ;
35- expect ( request . get . mock . calls ) . toMatchSnapshot ( ) ;
36- expect ( request . get . mock . results ) . toMatchSnapshot ( ) ;
37- done ( ) ;
38- } ,
41+ facebook . entityLink . mockReturnValueOnce (
42+ 'https://demo.frontity.com/the-beauty-of-gullfoss' ,
3943 ) ;
4044
4145 facebook . requestCount ( { type : 'post' , id : 60 } ) ;
46+
47+ await when ( ( ) => facebook . count ( { type : 'post' , id : 60 } ) ) ;
48+
49+ expect ( facebook . count ( { type : 'post' , id : 60 } ) ) . toBe ( 123 ) ;
50+ expect ( facebook . entityLink . mock . calls ) . toMatchSnapshot ( ) ;
51+ expect ( facebook . entityLink . mock . results ) . toMatchSnapshot ( ) ;
52+ expect ( request . get . mock . calls ) . toMatchSnapshot ( ) ;
53+ expect ( request . get . mock . results ) . toMatchSnapshot ( ) ;
4254 } ) ;
4355
4456 test ( 'requestCount fails' , async ( ) => {
45- const request = { get : jest . fn ( ) } ;
57+ const request = {
58+ get : jest . fn ( ( ) => ( {
59+ accept : jest
60+ . fn ( )
61+ . mockResolvedValueOnce ( 'fail' )
62+ . mockResolvedValueOnce ( { bad : 'response' } ) ,
63+ } ) ) ,
64+ } ;
4665 const facebook = Facebook . create ( { } , { request } ) ;
47- Object . defineProperty ( facebook , 'entityLink' , { value : jest . fn ( ) , writable : true } ) ;
48- facebook . entityLink . mockReturnValue ( 'https://demo.frontity.com/the-beauty-of-gullfoss' ) ;
66+ Object . defineProperty ( facebook , 'entityLink' , {
67+ value : jest . fn ( ) ,
68+ writable : true ,
69+ } ) ;
70+ facebook . entityLink . mockReturnValue (
71+ 'https://demo.frontity.com/the-beauty-of-gullfoss' ,
72+ ) ;
4973
5074 // First request (request fails)
51- request . get . mockRejectedValueOnce ( 'fail' ) ;
5275 await facebook . requestCount ( { type : 'post' , id : 60 } ) ;
5376 expect ( facebook . count ( { type : 'post' , id : 60 } ) ) . toBe ( null ) ;
5477
5578 // Second request (bad response)
56- request . get . mockResolvedValueOnce ( { bad : 'response' } ) ;
5779 await facebook . requestCount ( { type : 'post' , id : 60 } ) ;
5880 expect ( facebook . count ( { type : 'post' , id : 60 } ) ) . toBe ( null ) ;
5981
0 commit comments