1+ import type {
2+ CashTrackingSessionStartEvent ,
3+ TransactionCompleteEvent ,
4+ } from '@shopify/ui-extensions/point-of-sale' ;
5+
16import { getExtension } from '../index' ;
27
38import { createTestSandbox , type TestSandbox } from './helpers' ;
49
10+ function makeTransactionCompleteEvent ( ) : TransactionCompleteEvent {
11+ return Object . assign ( new Event ( 'transactioncomplete' ) , {
12+ transactionType : 'Sale' as const ,
13+ discounts : [ ] ,
14+ taxTotal : { amount : 0 , currency : 'USD' } ,
15+ subtotal : { amount : 0 , currency : 'USD' } ,
16+ grandTotal : { amount : 0 , currency : 'USD' } ,
17+ paymentMethods : [ ] ,
18+ balanceDue : { amount : 0 , currency : 'USD' } ,
19+ shippingLines : [ ] ,
20+ taxLines : [ ] ,
21+ executedAt : '2024-01-01T00:00:00Z' ,
22+ lineItems : [ ] ,
23+ } ) ;
24+ }
25+
26+ function makeCashTrackingSessionStartEvent ( ) : CashTrackingSessionStartEvent {
27+ return Object . assign ( new Event ( 'cashtrackingsessionstart' ) , {
28+ id : 1 ,
29+ openingTime : '2024-01-01T00:00:00Z' ,
30+ } ) ;
31+ }
32+
533describe ( 'shopify.addEventListener / extension.dispatch' , ( ) => {
634 let sandbox : TestSandbox ;
735
836 beforeEach ( ( ) => {
937 sandbox = createTestSandbox ( ) ;
10- sandbox . placeToml ( ) ;
38+ sandbox . placeToml ( { target : 'pos.app.ready.data' } ) ;
1139 } ) ;
1240
1341 afterEach ( ( ) => {
1442 sandbox . destroy ( ) ;
1543 } ) ;
1644
1745 function setUpExt ( ) {
18- const extension = getExtension ( 'purchase.checkout.block.render ' , {
46+ const extension = getExtension ( 'pos.app.ready.data ' , {
1947 configSearchDir : sandbox . tempDir ,
2048 } ) ;
2149 extension . setUp ( ) ;
@@ -35,11 +63,11 @@ describe('shopify.addEventListener / extension.dispatch', () => {
3563 const listener = jest . fn ( ) ;
3664 shopify . addEventListener ( 'transactioncomplete' , listener ) ;
3765
38- const eventData = { transaction : { id : 1 } } ;
39- extension . dispatch ( 'transactioncomplete' , eventData ) ;
66+ const event = makeTransactionCompleteEvent ( ) ;
67+ extension . dispatch ( 'transactioncomplete' , event ) ;
4068
4169 expect ( listener ) . toHaveBeenCalledTimes ( 1 ) ;
42- expect ( listener ) . toHaveBeenCalledWith ( eventData ) ;
70+ expect ( listener ) . toHaveBeenCalledWith ( event ) ;
4371 } ) ;
4472
4573 it ( 'fires all listeners registered for the same event' , ( ) => {
@@ -50,10 +78,11 @@ describe('shopify.addEventListener / extension.dispatch', () => {
5078 shopify . addEventListener ( 'transactioncomplete' , listenerA ) ;
5179 shopify . addEventListener ( 'transactioncomplete' , listenerB ) ;
5280
53- extension . dispatch ( 'transactioncomplete' , 42 ) ;
81+ const event = makeTransactionCompleteEvent ( ) ;
82+ extension . dispatch ( 'transactioncomplete' , event ) ;
5483
55- expect ( listenerA ) . toHaveBeenCalledWith ( 42 ) ;
56- expect ( listenerB ) . toHaveBeenCalledWith ( 42 ) ;
84+ expect ( listenerA ) . toHaveBeenCalledWith ( event ) ;
85+ expect ( listenerB ) . toHaveBeenCalledWith ( event ) ;
5786 } ) ;
5887
5988 it ( 'does not fire other events when dispatching one' , ( ) => {
@@ -64,7 +93,7 @@ describe('shopify.addEventListener / extension.dispatch', () => {
6493 shopify . addEventListener ( 'transactioncomplete' , target ) ;
6594 shopify . addEventListener ( 'cashtrackingsessionstart' , other ) ;
6695
67- extension . dispatch ( 'transactioncomplete' , undefined ) ;
96+ extension . dispatch ( 'transactioncomplete' , makeTransactionCompleteEvent ( ) ) ;
6897
6998 expect ( target ) . toHaveBeenCalledTimes ( 1 ) ;
7099 expect ( other ) . not . toHaveBeenCalled ( ) ;
@@ -77,7 +106,7 @@ describe('shopify.addEventListener / extension.dispatch', () => {
77106 shopify . addEventListener ( 'transactioncomplete' , listener ) ;
78107 shopify . removeEventListener ( 'transactioncomplete' , listener ) ;
79108
80- extension . dispatch ( 'transactioncomplete' , undefined ) ;
109+ extension . dispatch ( 'transactioncomplete' , makeTransactionCompleteEvent ( ) ) ;
81110
82111 expect ( listener ) . not . toHaveBeenCalled ( ) ;
83112 } ) ;
@@ -89,7 +118,7 @@ describe('shopify.addEventListener / extension.dispatch', () => {
89118 shopify . addEventListener ( 'transactioncomplete' , listener ) ;
90119 shopify . addEventListener ( 'transactioncomplete' , listener ) ;
91120
92- extension . dispatch ( 'transactioncomplete' , undefined ) ;
121+ extension . dispatch ( 'transactioncomplete' , makeTransactionCompleteEvent ( ) ) ;
93122
94123 expect ( listener ) . toHaveBeenCalledTimes ( 1 ) ;
95124 } ) ;
@@ -105,7 +134,7 @@ describe('shopify.addEventListener / extension.dispatch', () => {
105134 shopify . addEventListener ( 'transactioncomplete' , follower ) ;
106135
107136 expect ( ( ) =>
108- extension . dispatch ( 'transactioncomplete' , undefined ) ,
137+ extension . dispatch ( 'transactioncomplete' , makeTransactionCompleteEvent ( ) ) ,
109138 ) . not . toThrow ( ) ;
110139 expect ( throwing ) . toHaveBeenCalledTimes ( 1 ) ;
111140 expect ( follower ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -114,7 +143,10 @@ describe('shopify.addEventListener / extension.dispatch', () => {
114143 it ( 'is a no-op when dispatching an event with no registered listeners' , ( ) => {
115144 const extension = setUpExt ( ) ;
116145 expect ( ( ) =>
117- extension . dispatch ( 'cashtrackingsessionstart' , { } ) ,
146+ extension . dispatch (
147+ 'cashtrackingsessionstart' ,
148+ makeCashTrackingSessionStartEvent ( ) ,
149+ ) ,
118150 ) . not . toThrow ( ) ;
119151 } ) ;
120152
@@ -126,7 +158,7 @@ describe('shopify.addEventListener / extension.dispatch', () => {
126158 extension . tearDown ( ) ;
127159
128160 extension . setUp ( ) ;
129- extension . dispatch ( 'transactioncomplete' , undefined ) ;
161+ extension . dispatch ( 'transactioncomplete' , makeTransactionCompleteEvent ( ) ) ;
130162
131163 expect ( leaked ) . not . toHaveBeenCalled ( ) ;
132164 } ) ;
0 commit comments