-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathindex.test.tsx
87 lines (78 loc) · 2.67 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { validateParams, sanitize, generatePaystackParams } from '../development/utils';
import { Alert } from 'react-native';
jest.mock('react-native', () => ({
Alert: { alert: jest.fn() }
}));
describe('Paystack Utils', () => {
describe('validateParams', () => {
it('should return true for valid params', () => {
const result = validateParams({
email: '[email protected]',
amount: 5000,
onSuccess: jest.fn(),
onCancel: jest.fn()
}, false);
expect(result).toBe(true);
});
it('should fail with missing email and show alert', () => {
const result = validateParams({
email: '',
amount: 5000,
onSuccess: jest.fn(),
onCancel: jest.fn()
}, true);
expect(result).toBe(false);
expect(Alert.alert).toHaveBeenCalledWith('Payment Error', expect.stringContaining('Email is required'));
});
it('should fail with invalid amount', () => {
const result = validateParams({
email: '[email protected]',
amount: 0,
onSuccess: jest.fn(),
onCancel: jest.fn()
}, true);
expect(result).toBe(false);
expect(Alert.alert).toHaveBeenCalledWith('Payment Error', expect.stringContaining('Amount must be a valid number'));
});
it('should fail with missing callbacks', () => {
const result = validateParams({
email: '[email protected]',
amount: 1000,
onSuccess: undefined,
onCancel: undefined
} as any, true);
expect(result).toBe(false);
expect(Alert.alert).toHaveBeenCalledWith('Payment Error', expect.stringContaining('onSuccess callback is required'));
});
});
describe('sanitize', () => {
it('should wrap string by default', () => {
expect(sanitize('hello', '', true)).toBe("'hello'");
});
it('should return stringified object', () => {
expect(sanitize({ test: true }, {})).toBe(JSON.stringify({ test: true }));
});
it('should return fallback on error', () => {
const circular = {};
// @ts-ignore
circular.self = circular;
expect(sanitize(circular, 'fallback')).toBe(JSON.stringify('fallback'));
});
});
describe('generatePaystackParams', () => {
it('should generate JS object string with all fields', () => {
const js = generatePaystackParams({
publicKey: 'pk_test',
email: '[email protected]',
amount: 100,
reference: 'ref123',
metadata: { order: 123 },
currency: 'NGN',
channels: ['card']
});
expect(js).toContain("key: 'pk_test'");
expect(js).toContain("email: '[email protected]'");
expect(js).toContain("amount: 10000");
});
});
});