Skip to content

Commit 3a7c499

Browse files
refactor: improve tests with side effects of setup checks
1 parent 6d88aa0 commit 3a7c499

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

modules/@shopify/checkout-sheet-kit/android/src/main/java/com/shopify/reactnative/checkoutsheetkit/RCTCheckoutWebView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void setAuth(String authToken) {
128128
scheduleSetupIfNeeded();
129129
}
130130

131-
private void scheduleSetupIfNeeded() {
131+
void scheduleSetupIfNeeded() {
132132
if (pendingSetup) {
133133
return;
134134
}
@@ -205,7 +205,7 @@ private CheckoutWebViewEventProcessor getCheckoutWebViewEventProcessor() {
205205
);
206206
}
207207

208-
private void removeCheckout() {
208+
void removeCheckout() {
209209
Log.d(TAG, "removeCheckout: Called, webview exists: " + (checkoutWebView != null));
210210
if (checkoutWebView != null) {
211211
Log.d(TAG, "removeCheckout: Destroying webview");

modules/@shopify/checkout-sheet-kit/android/src/test/java/com/shopify/reactnative/checkoutsheetkit/RCTCheckoutWebViewTest.java

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ public void testSetCheckoutUrl_withSameUrl_doesNotScheduleSetup() throws Excepti
136136
Handler mockHandler = mock(Handler.class);
137137
when(mockHandler.post(any(Runnable.class))).thenReturn(true);
138138
setPrivateField(webView, "mainHandler", mockHandler);
139-
setPrivateField(webView, "checkoutUrl", "https://shopify.com/checkout");
139+
140+
webView.setCheckoutUrl("https://shopify.com/checkout");
141+
reset(mockHandler);
142+
when(mockHandler.post(any(Runnable.class))).thenReturn(true);
140143

141144
webView.setCheckoutUrl("https://shopify.com/checkout");
142145

@@ -145,12 +148,15 @@ public void testSetCheckoutUrl_withSameUrl_doesNotScheduleSetup() throws Excepti
145148

146149
@Test
147150
public void testSetCheckoutUrl_withNull_clearsUrlAndLastConfiguration() throws Exception {
148-
setPrivateField(webView, "checkoutUrl", "https://shopify.com/checkout");
151+
RCTCheckoutWebView spyWebView = spy(new RCTCheckoutWebView(mockContext));
152+
doNothing().when(spyWebView).scheduleSetupIfNeeded();
153+
doNothing().when(spyWebView).removeCheckout();
149154

150-
webView.setCheckoutUrl(null);
155+
spyWebView.setCheckoutUrl("https://shopify.com/checkout");
156+
spyWebView.setCheckoutUrl(null);
151157

152-
assertThat(getPrivateField(webView, "checkoutUrl")).isNull();
153-
assertThat(getPrivateField(webView, "lastConfiguration")).isNull();
158+
assertThat(getPrivateField(spyWebView, "checkoutUrl")).isNull();
159+
assertThat(getPrivateField(spyWebView, "lastConfiguration")).isNull();
154160
}
155161

156162
@Test
@@ -169,7 +175,10 @@ public void testSetAuth_withSameToken_doesNotScheduleSetup() throws Exception {
169175
Handler mockHandler = mock(Handler.class);
170176
when(mockHandler.post(any(Runnable.class))).thenReturn(true);
171177
setPrivateField(webView, "mainHandler", mockHandler);
172-
setPrivateField(webView, "auth", "existing-token");
178+
179+
webView.setAuth("existing-token");
180+
reset(mockHandler);
181+
when(mockHandler.post(any(Runnable.class))).thenReturn(true);
173182

174183
webView.setAuth("existing-token");
175184

@@ -322,6 +331,50 @@ public void testSetup_alwaysResetsPendingSetupFlag() throws Exception {
322331
assertThat(getPrivateField(webView, "pendingSetup")).isEqualTo(false);
323332
}
324333

334+
@Test
335+
public void testSetCheckoutUrl_withNonNullUrl_callsScheduleSetupIfNeeded() {
336+
RCTCheckoutWebView spyWebView = spy(new RCTCheckoutWebView(mockContext));
337+
doNothing().when(spyWebView).scheduleSetupIfNeeded();
338+
339+
spyWebView.setCheckoutUrl("https://shopify.com/checkout");
340+
341+
verify(spyWebView).scheduleSetupIfNeeded();
342+
}
343+
344+
@Test
345+
public void testSetCheckoutUrl_withNullUrl_callsRemoveCheckout() {
346+
RCTCheckoutWebView spyWebView = spy(new RCTCheckoutWebView(mockContext));
347+
doNothing().when(spyWebView).scheduleSetupIfNeeded();
348+
doNothing().when(spyWebView).removeCheckout();
349+
350+
spyWebView.setCheckoutUrl("https://shopify.com/checkout");
351+
spyWebView.setCheckoutUrl(null);
352+
353+
verify(spyWebView).removeCheckout();
354+
}
355+
356+
@Test
357+
public void testSetAuth_callsScheduleSetupIfNeeded() {
358+
RCTCheckoutWebView spyWebView = spy(new RCTCheckoutWebView(mockContext));
359+
doNothing().when(spyWebView).scheduleSetupIfNeeded();
360+
361+
spyWebView.setAuth("auth-token");
362+
363+
verify(spyWebView).scheduleSetupIfNeeded();
364+
}
365+
366+
@Test
367+
public void testSetCheckoutUrlAndAuth_batchesToSingleSetup() throws Exception {
368+
Handler mockHandler = mock(Handler.class);
369+
when(mockHandler.post(any(Runnable.class))).thenReturn(true);
370+
setPrivateField(webView, "mainHandler", mockHandler);
371+
372+
webView.setCheckoutUrl("https://shopify.com/checkout");
373+
webView.setAuth("auth-token");
374+
375+
verify(mockHandler, times(1)).post(any(Runnable.class));
376+
}
377+
325378
private void setPrivateField(Object object, String fieldName, Object value) throws Exception {
326379
Field field = object.getClass().getDeclaredField(fieldName);
327380
field.setAccessible(true);

0 commit comments

Comments
 (0)