11import type { WalkerOS } from '@elbwalker/types' ;
22import type { DestinationWebAPI } from '.' ;
3+ import { createEvent } from '@elbwalker/utils' ;
4+ import { elb , Walkerjs } from '@elbwalker/walker.js' ;
5+ import { events , mapping } from '../examples' ;
36
47describe ( 'Destination API' , ( ) => {
5- const mockSendWeb = jest . fn ( ) ;
8+ const mockSendWeb = jest . fn ( ) ; //.mockImplementation(console.log);
9+
610 jest . mock ( '@elbwalker/utils/web' , ( ) => ( {
711 ...jest . requireActual ( '@elbwalker/utils/web' ) ,
812 sendWeb : mockSendWeb ,
913 } ) ) ;
1014
1115 let destination : DestinationWebAPI . Destination ;
12- const event = { event : 'entity action' } as WalkerOS . Event ;
13- const data = JSON . stringify ( event ) ;
16+ let event : WalkerOS . Event ;
1417 const url = 'https://api.example.com/' ;
1518
16- function push (
17- event : WalkerOS . Event ,
18- custom : DestinationWebAPI . Custom = { url } ,
19- mapping = { } ,
20- options = { } ,
21- ) {
22- destination . push ( event , { custom } , mapping , options ) ;
23- }
24-
25- beforeEach ( async ( ) => {
19+ beforeEach ( ( ) => {
2620 destination = jest . requireActual ( '.' ) . default ;
21+ event = createEvent ( ) ;
22+ Walkerjs ( { pageview : false , session : false , run : true } ) ;
23+ jest . clearAllMocks ( ) ;
2724 } ) ;
2825
2926 test ( 'init' , ( ) => {
30- push ( event , { url : '' } ) ; // No url
31- expect ( mockSendWeb ) . not . toHaveBeenCalled ( ) ;
27+ destination . config = { } ;
28+ elb ( 'walker destination' , destination ) ;
3229
33- push ( event ) ;
30+ elb ( event ) ; // No url
31+ expect ( mockSendWeb ) . not . toHaveBeenCalled ( ) ;
3432
33+ destination . config . custom = { url } ;
34+ elb ( event ) ;
3535 expect ( mockSendWeb ) . toHaveBeenCalledTimes ( 1 ) ;
36- expect ( mockSendWeb ) . toHaveBeenCalledWith (
37- url ,
38- data ,
36+
37+ const [ calledUrl , calledData , calledOptions ] = mockSendWeb . mock . calls [ 0 ] ;
38+ expect ( calledUrl ) . toBe ( url ) ;
39+ expect ( JSON . parse ( calledData ) ) . toEqual ( event ) ;
40+ expect ( calledOptions ) . toEqual (
3941 expect . objectContaining ( {
4042 transport : 'fetch' ,
4143 } ) ,
@@ -49,7 +51,10 @@ describe('Destination API', () => {
4951 } ) ;
5052
5153 test ( 'transform' , ( ) => {
52- push ( event , { url, transform : ( ) => 'transformed' } ) ;
54+ elb ( 'walker destination' , destination , {
55+ custom : { url, transform : ( ) => 'transformed' } ,
56+ } ) ;
57+ elb ( event ) ;
5358 expect ( mockSendWeb ) . toHaveBeenCalledWith (
5459 url ,
5560 'transformed' ,
@@ -58,35 +63,44 @@ describe('Destination API', () => {
5863 } ) ;
5964
6065 test ( 'headers' , ( ) => {
61- push ( event , { url, headers : { foo : 'bar' } } ) ;
66+ elb ( 'walker destination' , destination , {
67+ custom : { url, headers : { foo : 'bar' } } ,
68+ } ) ;
69+ elb ( event ) ;
6270 expect ( mockSendWeb ) . toHaveBeenCalledWith (
6371 url ,
64- data ,
72+ expect . any ( String ) ,
6573 expect . objectContaining ( {
6674 headers : { foo : 'bar' } ,
6775 } ) ,
6876 ) ;
6977 } ) ;
7078
7179 test ( 'method' , ( ) => {
72- push ( event , { url, method : 'POST' } ) ;
80+ elb ( 'walker destination' , destination , {
81+ custom : { url, method : 'POST' } ,
82+ } ) ;
83+ elb ( event ) ;
7384 expect ( mockSendWeb ) . toHaveBeenCalledWith (
7485 url ,
75- data ,
86+ expect . any ( String ) ,
7687 expect . objectContaining ( {
7788 method : 'POST' ,
7889 } ) ,
7990 ) ;
8091 } ) ;
8192
82- test ( 'mapping data' , ( ) => {
83- push ( event , { url, method : 'POST' } , { } , { data : { foo : 'bar' } } ) ;
93+ test ( 'event entity action' , ( ) => {
94+ elb ( 'walker destination' , destination , {
95+ custom : { url } ,
96+ mapping : mapping . config ,
97+ } ) ;
98+ elb ( event ) ;
99+
84100 expect ( mockSendWeb ) . toHaveBeenCalledWith (
85101 url ,
86- JSON . stringify ( { foo : 'bar' } ) ,
87- expect . objectContaining ( {
88- method : 'POST' ,
89- } ) ,
102+ events . entity_action ( ) ,
103+ expect . any ( Object ) ,
90104 ) ;
91105 } ) ;
92106} ) ;
0 commit comments