1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
2
3
- const SCRIPT_SELECTOR =
4
- 'script[src="https://js.stripe.com/v3"], script[src="https://js.stripe.com/v3/"]' ;
3
+ const SCRIPT_SELECTOR = 'script[src^="https://js.stripe.com/v3"]' ;
5
4
6
5
describe ( 'pure module' , ( ) => {
6
+ beforeEach ( ( ) => {
7
+ jest . spyOn ( console , 'warn' ) . mockReturnValue ( ) ;
8
+ } ) ;
9
+
7
10
afterEach ( ( ) => {
8
- const script = document . querySelector ( SCRIPT_SELECTOR ) ;
9
- if ( script && script . parentElement ) {
10
- script . parentElement . removeChild ( script ) ;
11
+ const scripts = Array . from ( document . querySelectorAll ( SCRIPT_SELECTOR ) ) ;
12
+
13
+ for ( const script of scripts ) {
14
+ if ( script . parentElement ) {
15
+ script . parentElement . removeChild ( script ) ;
16
+ }
11
17
}
12
18
13
19
delete window . Stripe ;
20
+
14
21
jest . resetModules ( ) ;
22
+ jest . restoreAllMocks ( ) ;
15
23
} ) ;
16
24
17
25
test ( 'does not inject the script if loadStripe is not called' , async ( ) => {
@@ -26,4 +34,100 @@ describe('pure module', () => {
26
34
27
35
expect ( document . querySelector ( SCRIPT_SELECTOR ) ) . not . toBe ( null ) ;
28
36
} ) ;
37
+
38
+ test ( 'it can load the script with advanced fraud signals disabled' , ( ) => {
39
+ const { loadStripe} = require ( './pure' ) ;
40
+
41
+ loadStripe . setLoadParameters ( { advancedFraudSignals : false } ) ;
42
+ loadStripe ( 'pk_test_foo' ) ;
43
+
44
+ expect (
45
+ document . querySelector (
46
+ 'script[src^="https://js.stripe.com/v3?advancedFraudSignals=false"]'
47
+ )
48
+ ) . not . toBe ( null ) ;
49
+ } ) ;
50
+
51
+ test ( 'it should throw when setting invalid load parameters' , ( ) => {
52
+ const { loadStripe} = require ( './pure' ) ;
53
+
54
+ expect ( ( ) => {
55
+ loadStripe . setLoadParameters ( { howdy : true } ) ;
56
+ } ) . toThrow ( 'invalid load parameters' ) ;
57
+ } ) ;
58
+
59
+ test ( 'it should warn when calling loadStripe if a script already exists when parameters are set' , ( ) => {
60
+ const script = document . createElement ( 'script' ) ;
61
+ script . src = 'https://js.stripe.com/v3' ;
62
+ document . body . appendChild ( script ) ;
63
+
64
+ const { loadStripe} = require ( './pure' ) ;
65
+ loadStripe . setLoadParameters ( { advancedFraudSignals : true } ) ;
66
+ loadStripe ( 'pk_test_123' ) ;
67
+
68
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
69
+ expect ( console . warn ) . toHaveBeenLastCalledWith (
70
+ 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used'
71
+ ) ;
72
+ } ) ;
73
+
74
+ test ( 'it should warn when calling loadStripe if a script is added after parameters are set' , ( ) => {
75
+ const { loadStripe} = require ( './pure' ) ;
76
+ loadStripe . setLoadParameters ( { advancedFraudSignals : true } ) ;
77
+
78
+ const script = document . createElement ( 'script' ) ;
79
+ script . src = 'https://js.stripe.com/v3' ;
80
+ document . body . appendChild ( script ) ;
81
+
82
+ loadStripe ( 'pk_test_123' ) ;
83
+
84
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
85
+ expect ( console . warn ) . toHaveBeenLastCalledWith (
86
+ 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used'
87
+ ) ;
88
+ } ) ;
89
+
90
+ test ( 'it should warn when window.Stripe already exists if parameters are set' , ( ) => {
91
+ window . Stripe = jest . fn ( ( key ) => ( { key} ) ) as any ;
92
+
93
+ const { loadStripe} = require ( './pure' ) ;
94
+ loadStripe . setLoadParameters ( { advancedFraudSignals : true } ) ;
95
+ loadStripe ( 'pk_test_123' ) ;
96
+
97
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
98
+ expect ( console . warn ) . toHaveBeenLastCalledWith (
99
+ 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used'
100
+ ) ;
101
+ } ) ;
102
+
103
+ test ( 'it should not warn when a script already exists if parameters are not set' , ( ) => {
104
+ const script = document . createElement ( 'script' ) ;
105
+ script . src = 'https://js.stripe.com/v3' ;
106
+ document . body . appendChild ( script ) ;
107
+
108
+ const { loadStripe} = require ( './pure' ) ;
109
+ loadStripe ( 'pk_test_123' ) ;
110
+
111
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 0 ) ;
112
+ } ) ;
113
+
114
+ test ( 'it should not warn when window.Stripe already exists if parameters are not set' , ( ) => {
115
+ window . Stripe = jest . fn ( ( key ) => ( { key} ) ) as any ;
116
+
117
+ const { loadStripe} = require ( './pure' ) ;
118
+ loadStripe ( 'pk_test_123' ) ;
119
+
120
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 0 ) ;
121
+ } ) ;
122
+
123
+ test ( 'throws an error if calling setLoadParameters after loadStripe' , ( ) => {
124
+ const { loadStripe} = require ( './pure' ) ;
125
+
126
+ loadStripe . setLoadParameters ( { advancedFraudSignals : false } ) ;
127
+ loadStripe ( 'pk_foo' ) ;
128
+
129
+ expect ( ( ) => {
130
+ loadStripe . setLoadParameters ( { advancedFraudSignals : false } ) ;
131
+ } ) . toThrow ( 'cannot change load parameters' ) ;
132
+ } ) ;
29
133
} ) ;
0 commit comments