Skip to content

Commit aead146

Browse files
mfdsleegeunhyeok
authored andcommitted
feat: close messaging view on iOS
1 parent 1fafd6c commit aead146

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ PODS:
302302
- React-jsinspector (0.70.6)
303303
- React-logger (0.70.6):
304304
- glog
305-
- react-native-zendesk-messaging (0.1.2):
305+
- react-native-zendesk-messaging (0.2.1):
306306
- React-Core
307307
- ZendeskSDKMessaging (= 2.13.0)
308308
- React-perflogger (0.70.6)
@@ -598,7 +598,7 @@ SPEC CHECKSUMS:
598598
React-jsiexecutor: b4a65947391c658450151275aa406f2b8263178f
599599
React-jsinspector: 60769e5a0a6d4b32294a2456077f59d0266f9a8b
600600
React-logger: 1623c216abaa88974afce404dc8f479406bbc3a0
601-
react-native-zendesk-messaging: ea81846bc38a4778218d02ba037b967059dcb374
601+
react-native-zendesk-messaging: e4418f96cb02c6741cce7059972f01312eac8e0f
602602
React-perflogger: 8c79399b0500a30ee8152d0f9f11beae7fc36595
603603
React-RCTActionSheet: 7316773acabb374642b926c19aef1c115df5c466
604604
React-RCTAnimation: 5341e288375451297057391227f691d9b2326c3d
@@ -627,4 +627,4 @@ SPEC CHECKSUMS:
627627

628628
PODFILE CHECKSUM: 95ff72db15f9303f9feae89ece9cba53e19302a2
629629

630-
COCOAPODS: 1.12.1
630+
COCOAPODS: 1.13.0

ios/ZendeskMessaging.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ @interface RCT_EXTERN_MODULE(ZendeskMessaging, NSObject)
2020
RCT_EXTERN_METHOD(openMessagingView:(RCTPromiseResolveBlock)resolve
2121
rejecter:(RCTPromiseRejectBlock)reject)
2222

23+
RCT_EXTERN_METHOD(closeMessagingView:(RCTPromiseResolveBlock)resolve
24+
rejecter:(RCTPromiseRejectBlock)reject)
25+
2326
RCT_EXTERN_METHOD(sendPageViewEvent:(NSDictionary*)event
2427
resolver:(RCTPromiseResolveBlock)resolve
2528
rejecter:(RCTPromiseRejectBlock)reject)

ios/ZendeskMessaging.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ class ZendeskMessaging: RCTEventEmitter {
147147
}
148148
}
149149

150+
@objc(closeMessagingView:rejecter:)
151+
func closeMessagingView(
152+
resolver resolve: @escaping RCTPromiseResolveBlock,
153+
rejecter reject: @escaping RCTPromiseRejectBlock
154+
) -> Void {
155+
if !initialized {
156+
reject(nil, "Zendesk instance not initialized", nil)
157+
return
158+
}
159+
160+
DispatchQueue.main.async {
161+
guard let rootController = RCTPresentedViewController() else {
162+
reject(nil, "cannot close messaging view", nil)
163+
return
164+
}
165+
rootController.dismiss(animated: true, completion: nil)
166+
resolve(nil)
167+
}
168+
}
169+
150170
@objc(sendPageViewEvent:resolver:rejecter:)
151171
func sendPageViewEvent(
152172
event: [String: String],

src/__tests__/index.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jest.mock('react-native', () => {
1414
login: jest.fn(),
1515
logout: jest.fn(),
1616
openMessagingView: jest.fn(),
17+
closeMessagingView: jest.fn(),
1718
sendPageViewEvent: jest.fn(),
1819
setConversationFields: jest.fn(),
1920
clearConversationFields: jest.fn(),
@@ -122,6 +123,48 @@ describe('react-native-zendesk-messaging', () => {
122123
});
123124
});
124125

126+
describe('when call closeMessagingView', () => {
127+
let mockCloseMessagingView: jest.SpyInstance;
128+
129+
describe('when platform is iOS', () => {
130+
beforeAll(() => {
131+
Platform.OS = 'ios';
132+
});
133+
134+
beforeEach(async () => {
135+
mockCloseMessagingView = jest.spyOn(ZendeskMessagingModule, 'closeMessagingView');
136+
await Zendesk.closeMessagingView();
137+
});
138+
139+
afterAll(() => {
140+
mockCloseMessagingView.mockClear();
141+
});
142+
143+
it('should call native module\'s openMessagingView method', () => {
144+
expect(mockCloseMessagingView).toHaveBeenCalledTimes(1);
145+
});
146+
});
147+
148+
describe('when platform is Android', () => {
149+
beforeAll(() => {
150+
Platform.OS = 'android';
151+
});
152+
153+
beforeEach(async () => {
154+
mockCloseMessagingView = jest.spyOn(ZendeskMessagingModule, 'closeMessagingView');
155+
await Zendesk.closeMessagingView();
156+
});
157+
158+
afterAll(() => {
159+
mockCloseMessagingView.mockClear();
160+
});
161+
162+
it('should call native module\'s openMessagingView method', () => {
163+
expect(mockCloseMessagingView).not.toHaveBeenCalled();
164+
});
165+
});
166+
});
167+
125168
describe('when call sendPageViewEvent', () => {
126169
let pageTitle: string;
127170
let url: string;

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ export function openMessagingView(): Promise<void> {
9090
return ZendeskMessaging.openMessagingView();
9191
}
9292

93+
/**
94+
* **iOS Only** (no-op for other platform, always return empty promise)
95+
*
96+
* Closes the messaging view if it is open. Doesn't work on Android.
97+
* Returns a promise that resolves when the messaging view is closed.
98+
*
99+
* N.B. This is not a part of the official Zendesk SDK, but a custom implementation.
100+
*/
101+
102+
export function closeMessagingView(): Promise<void> {
103+
if (Platform.OS !== 'ios') return Promise.resolve();
104+
return ZendeskMessaging.closeMessagingView();
105+
}
106+
93107
/**
94108
* Send session-based page view event. event must have `pageTitle` and `url`.
95109
*

0 commit comments

Comments
 (0)