Skip to content

Commit a16e0bf

Browse files
started writing unit tests
1 parent fcfd533 commit a16e0bf

File tree

5 files changed

+186
-50
lines changed

5 files changed

+186
-50
lines changed

ios/Plugin/Plugin.swift

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@ import Analytics
99
@objc(SegmentPlugin)
1010
public class SegmentPlugin: CAPPlugin {
1111

12-
var key: String?;
12+
private var key: String?;
1313

14-
override init(bridge: CAPBridge!, pluginId: String!, pluginName: String!) {
15-
#if DEBUG
16-
print("[Segment] ▶️ Segment plugin initialised: " + pluginId + " " + pluginName)
17-
#endif
18-
super.init(bridge: bridge, pluginId: pluginId, pluginName: pluginName)
19-
}
14+
// override init(bridge: CAPBridge!, pluginId: String!, pluginName: String!) {
15+
// #if DEBUG
16+
// print("[Segment] ▶️ Segment plugin initialised: " + pluginId + " " + pluginName)
17+
// #endif
18+
// super.init(bridge: bridge, pluginId: pluginId, pluginName: pluginName)
19+
// }
2020

2121
@objc func setUp(_ call: CAPPluginCall) {
2222
if (self.key != nil) {
23-
call.error("segment already set up")
24-
return;
23+
call.error("segment already set up", nil, [
24+
"code": "[SET_UP] DUPE_CALL",
25+
]);
26+
return;
2527
}
2628
let key = call.getString("key")
2729
let useLocationServices = call.getBool("useLocationServices") ?? false;
2830
let trackLifecycle = call.getBool("trackLifecycle") ?? false;
2931

3032
if (key == nil) {
31-
call.error("segment key not specified")
33+
call.error("segment key not specified", nil, [
34+
"code": "[SET_UP] NO_KEY",
35+
]);
3236
return;
3337
} else {
3438
#if DEBUG
@@ -48,13 +52,17 @@ public class SegmentPlugin: CAPPlugin {
4852

4953
@objc func identify(_ call: CAPPluginCall) {
5054
if (self.key == nil) {
51-
call.error("segment not set up")
52-
return;
55+
call.error("segment not set up", nil, [
56+
"code": "[IDENTIFY] NOT_SET_UP",
57+
]);
58+
return;
5359
}
5460
let userID = call.getString("userID")
5561
if (userID == nil) {
56-
call.error("no user ID specified")
57-
return;
62+
call.error("no userID specified", nil, [
63+
"code": "[IDENTIFY] NO_USER_ID",
64+
]);
65+
return;
5866
}
5967

6068
let traits: Dictionary = call.getObject("traits") ?? [:];
@@ -70,12 +78,16 @@ public class SegmentPlugin: CAPPlugin {
7078

7179
@objc func track(_ call: CAPPluginCall) {
7280
if (self.key == nil) {
73-
call.error("segment not set up")
81+
call.error("segment not set up", nil, [
82+
"code": "[TRACK] NOT_SET_UP",
83+
]);
7484
return;
7585
}
7686
let eventName = call.getString("eventName")
7787
if (eventName == nil) {
78-
call.error("no event name specified")
88+
call.error("no eventName specified", nil, [
89+
"code": "[TRACK] NO_EVENT_NAME",
90+
]);
7991
return;
8092
}
8193

@@ -93,7 +105,9 @@ public class SegmentPlugin: CAPPlugin {
93105

94106
@objc func reset(_ call: CAPPluginCall) {
95107
if (self.key == nil) {
96-
call.error("segment not set up")
108+
call.error("segment not set up", nil, [
109+
"code": "[RESET] NOT_SET_UP",
110+
]);
97111
return;
98112
}
99113
#if DEBUG

ios/PluginTests/PluginTests.swift

Lines changed: 121 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,126 @@ import Capacitor
44

55
class PluginTests: XCTestCase {
66

7-
override func setUp() {
8-
super.setUp()
9-
// Put setup code here. This method is called before the invocation of each test method in the class.
10-
}
11-
12-
override func tearDown() {
13-
// Put teardown code here. This method is called after the invocation of each test method in the class.
14-
super.tearDown()
15-
}
16-
17-
func testSetUp() {
18-
// This is an example of a functional test case for a plugin.
19-
// Use XCTAssert and related functions to verify your tests produce the correct results.
20-
21-
let key = "SAMPLE_KEY"
22-
let plugin = MyPlugin()
23-
24-
let call = CAPPluginCall(callbackId: "test", options: [
25-
"key": key
7+
override func setUp() {
8+
super.setUp()
9+
// Put setup code here. This method is called before the invocation of each test method in the class.
10+
}
11+
12+
override func tearDown() {
13+
// Put teardown code here. This method is called after the invocation of each test method in the class.
14+
super.tearDown()
15+
}
16+
17+
func testSetUp() {
18+
19+
let plugin = SegmentPlugin()
20+
21+
22+
({
23+
let expectation = XCTestExpectation(description: "[SET_UP] NO_KEY");
24+
let call = CAPPluginCall(callbackId: "test", options: [:], success: { (result, call) in
25+
XCTFail("🤦‍♂️ Success callback should not have been called with empty key")
26+
expectation.fulfill()
27+
}, error: { (err: CAPPluginCallError?) in
28+
XCTAssertEqual(err!.message, "segment key not specified");
29+
XCTAssertEqual(err!.data!["code"]! as? String, "[SET_UP] NO_KEY");
30+
// print("👎 err message: " + err!.message + ". data: " + err!.data!.description);
31+
expectation.fulfill()
32+
});
33+
plugin.setUp(call!)
34+
wait(for: [expectation], timeout: 1);
35+
})();
36+
37+
38+
({
39+
let expectation = XCTestExpectation(description: "[SET_UP] SUCCESS");
40+
let call = CAPPluginCall(callbackId: "test", options: [
41+
"key": "test_segment_key"
42+
], success: { (result, call) in
43+
XCTAssertEqual(result!.data.count, 0);
44+
expectation.fulfill()
45+
}, error: { (err: CAPPluginCallError?) in
46+
XCTFail("🤦‍♂️ Error callback should not have been called")
47+
expectation.fulfill()
48+
});
49+
plugin.setUp(call!)
50+
wait(for: [expectation], timeout: 1);
51+
})();
52+
53+
54+
({
55+
let expectation = XCTestExpectation(description: "[SET_UP] DUPE_CALL");
56+
let call = CAPPluginCall(callbackId: "test", options: [
57+
"key": "test_segment_key"
2658
], success: { (result, call) in
27-
// let message = result!.data["message"] as? String
28-
// XCTAssertEqual(message, "✅ Success")
29-
}, error: { (err) in
30-
XCTFail("Error shouldn't have been called")
31-
})
32-
33-
plugin.setUp(call!)
34-
}
59+
XCTFail("🤦‍♂️ Success callback should not have been called")
60+
expectation.fulfill()
61+
}, error: { (err: CAPPluginCallError?) in
62+
XCTAssertEqual(err!.message, "segment already set up");
63+
XCTAssertEqual(err!.data!["code"]! as? String, "[SET_UP] DUPE_CALL");
64+
expectation.fulfill()
65+
});
66+
plugin.setUp(call!)
67+
wait(for: [expectation], timeout: 1);
68+
})();
69+
70+
// TODO: test location services and lifecycle
71+
72+
}
73+
74+
func testIdentify() {
75+
76+
let plugin = SegmentPlugin();
77+
78+
79+
({
80+
let expectation = XCTestExpectation(description: "[IDENTIFY] NOT_SET_UP");
81+
let call = CAPPluginCall(callbackId: "test", options: [:], success: { (result, call) in
82+
XCTFail("🤦‍♂️ Success callback should not have been called")
83+
expectation.fulfill()
84+
}, error: { (err: CAPPluginCallError?) in
85+
XCTAssertEqual(err!.message, "segment not set up");
86+
XCTAssertEqual(err!.data!["code"]! as? String, "[IDENTIFY] NOT_SET_UP");
87+
expectation.fulfill()
88+
});
89+
plugin.identify(call!)
90+
wait(for: [expectation], timeout: 1);
91+
})();
92+
93+
94+
// ({
95+
// let expectation = XCTestExpectation(description: "[IDENTIFY] NO_USER_ID");
96+
// let call = CAPPluginCall(callbackId: "test", options: [
97+
// "key": "test_segment_key"
98+
// ], success: { (result, call) in
99+
// XCTFail("🤦‍♂️ Success callback should not have been called")
100+
// expectation.fulfill()
101+
// }, error: { (err: CAPPluginCallError?) in
102+
// XCTAssertEqual(err!.message, "segment already set up");
103+
// XCTAssertEqual(err!.data!["code"]! as? String, "[SET_UP] DUPE_CALL");
104+
// expectation.fulfill()
105+
// });
106+
// plugin.setUp(call!)
107+
// wait(for: [expectation], timeout: 1);
108+
// })();
109+
//
110+
//
111+
// ({
112+
// let expectation = XCTestExpectation(description: "[IDENTIFY] SUCCESS");
113+
// let call = CAPPluginCall(callbackId: "test", options: [
114+
// "key": "test_segment_key"
115+
// ], success: { (result, call) in
116+
// XCTFail("🤦‍♂️ Success callback should not have been called")
117+
// expectation.fulfill()
118+
// }, error: { (err: CAPPluginCallError?) in
119+
// XCTAssertEqual(err!.message, "segment already set up");
120+
// XCTAssertEqual(err!.data!["code"]! as? String, "[SET_UP] DUPE_CALL");
121+
// expectation.fulfill()
122+
// });
123+
// plugin.setUp(call!)
124+
// wait(for: [expectation], timeout: 1);
125+
// })();
126+
127+
}
128+
35129
}

src/definitions.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
import '@capacitor/core';
22

3-
declare module "@capacitor/core" {
3+
declare module '@capacitor/core' {
44
interface PluginRegistry {
55
SegmentPlugin: SegmentPlugin;
66
}
77
}
88

99
export interface SegmentPlugin {
10-
setUp(options: { key: string }): Promise<{}>;
10+
setUp(options: {
11+
key: string,
12+
useLocationServices?: boolean,
13+
trackLifecycle?: boolean,
14+
}): Promise<{}>;
15+
16+
identify(options: {
17+
userID: string,
18+
traits?: { [K: string]: any },
19+
}): Promise<{}>;
20+
21+
track(options: {
22+
eventName: string,
23+
properties?: { [K: string]: any },
24+
}): Promise<{}>;
25+
26+
reset(): Promise<{}>;
27+
}
28+
29+
export interface SegmentPluginError {
30+
message: string;
31+
code:
32+
'[SET_UP] DUPE_CALL' |
33+
'[SET_UP] NO_KEY' |
34+
'[IDENTIFY] NOT_SET_UP' |
35+
'[IDENTIFY] NO_USER_ID' |
36+
'[TRACK] NOT_SET_UP' |
37+
'[TRACK] NO_EVENT_NAME' |
38+
'[RESET] NOT_SET_UP';
1139
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// export * from './definitions';
1+
export * from './definitions';
22
export * from './web';

src/web.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export class SegmentPluginWeb extends WebPlugin implements SegmentPlugin {
3737

3838
}
3939

40-
const SegmentPlugin = new SegmentPluginWeb();
40+
const segmentPluginWeb = new SegmentPluginWeb();
4141

42-
export { SegmentPlugin };
42+
export { segmentPluginWeb };
4343

4444
import { registerWebPlugin } from '@capacitor/core';
45-
registerWebPlugin(SegmentPlugin);
45+
registerWebPlugin(segmentPluginWeb);

0 commit comments

Comments
 (0)