File size: 6,339 Bytes
c904036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Simple Unit Tests for Core Functions
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');

// Mock external dependencies
jest.mock('bcrypt');
jest.mock('jsonwebtoken');

describe('Simple Unit Tests', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('Password Hashing', () => {
    it('should hash password correctly', async () => {
      const password = 'testpassword';
      const hashedPassword = 'hashedpassword123';

      bcrypt.hash.mockResolvedValue(hashedPassword);

      const result = await bcrypt.hash(password, 10);

      expect(result).toBe(hashedPassword);
      expect(bcrypt.hash).toHaveBeenCalledWith(password, 10);
    });

    it('should compare password correctly', async () => {
      const password = 'testpassword';
      const hashedPassword = 'hashedpassword123';

      bcrypt.compare.mockResolvedValue(true);

      const result = await bcrypt.compare(password, hashedPassword);

      expect(result).toBe(true);
      expect(bcrypt.compare).toHaveBeenCalledWith(password, hashedPassword);
    });
  });

  describe('JWT Token', () => {
    it('should generate JWT token', () => {
      const payload = { userId: 'user123' };
      const token = 'mock.jwt.token';

      jwt.sign.mockReturnValue(token);

      const result = jwt.sign(payload, 'secret', { expiresIn: '7d' });

      expect(result).toBe(token);
      expect(jwt.sign).toHaveBeenCalledWith(payload, 'secret', {
        expiresIn: '7d',
      });
    });

    it('should verify JWT token', () => {
      const token = 'mock.jwt.token';
      const decoded = { userId: 'user123' };

      jwt.verify.mockReturnValue(decoded);

      const result = jwt.verify(token, 'secret');

      expect(result).toEqual(decoded);
      expect(jwt.verify).toHaveBeenCalledWith(token, 'secret');
    });
  });

  describe('Utility Functions', () => {
    it('should validate email format', () => {
      const validateEmail = (email) => {
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return emailRegex.test(email);
      };

      expect(validateEmail('test@example.com')).toBe(true);
      expect(validateEmail('invalid-email')).toBe(false);
      expect(validateEmail('test@')).toBe(false);
    });

    it('should validate phone number format', () => {
      const validatePhone = (phone) => {
        const phoneRegex = /^[0-9]{10}$/;
        return phoneRegex.test(phone);
      };

      expect(validatePhone('1234567890')).toBe(true);
      expect(validatePhone('123456789')).toBe(false);
      expect(validatePhone('12345678901')).toBe(false);
      expect(validatePhone('123456789a')).toBe(false);
    });

    it('should validate password strength', () => {
      const { validatePassword } = require('../../Utils/passwordUtils');

      // Valid passwords with uppercase, lowercase, number, and special char
      expect(validatePassword('Password123!')).toBe(true);
      expect(validatePassword('Valid@Pass1')).toBe(true);
      
      // Invalid passwords
      expect(validatePassword('pass')).toBe(false);
      expect(validatePassword('')).toBe(false);
      expect(validatePassword(null)).toBe(false);
      expect(validatePassword(undefined)).toBe(false);
    });

    it('should validate name format', () => {
      const validateName = (name) => {
        return Boolean(name && name.trim().length >= 2);
      };

      expect(validateName('John')).toBe(true);
      expect(validateName('A')).toBe(false);
      expect(validateName('')).toBe(false);
      expect(validateName('   ')).toBe(false);
      expect(validateName(null)).toBe(false);
      expect(validateName(undefined)).toBe(false);
    });
  });

  describe('Date and Time Functions', () => {
    it('should check if date is in the future', () => {
      const isFutureDate = (date) => {
        return new Date(date) > new Date();
      };

      const futureDate = new Date();
      futureDate.setDate(futureDate.getDate() + 1);

      const pastDate = new Date();
      pastDate.setDate(pastDate.getDate() - 1);

      expect(isFutureDate(futureDate)).toBe(true);
      expect(isFutureDate(pastDate)).toBe(false);
    });

    it('should format date correctly', () => {
      const formatDate = (date) => {
        return new Date(date).toISOString().split('T')[0];
      };

      const testDate = new Date('2024-01-15');
      expect(formatDate(testDate)).toBe('2024-01-15');
    });
  });

  describe('String Manipulation', () => {
    it('should capitalize first letter', () => {
      const capitalize = (str) => {
        return str.charAt(0).toUpperCase() + str.slice(1);
      };

      expect(capitalize('hello')).toBe('Hello');
      expect(capitalize('world')).toBe('World');
      expect(capitalize('')).toBe('');
    });

    it('should truncate long strings', () => {
      const truncate = (str, length) => {
        return str.length > length ? `${str.substring(0, length)}...` : str;
      };

      expect(truncate('Hello World', 5)).toBe('Hello...');
      expect(truncate('Short', 10)).toBe('Short');
    });
  });

  describe('Array Operations', () => {
    it('should remove duplicates from array', () => {
      const removeDuplicates = (arr) => {
        return [...new Set(arr)];
      };

      expect(removeDuplicates([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]);
      expect(removeDuplicates(['a', 'b', 'a', 'c'])).toEqual(['a', 'b', 'c']);
    });

    it('should find unique values', () => {
      const findUnique = (arr) => {
        return arr.filter((item, index) => arr.indexOf(item) === index);
      };

      expect(findUnique([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]);
    });
  });

  describe('Object Operations', () => {
    it('should merge objects', () => {
      const mergeObjects = (obj1, obj2) => {
        return { ...obj1, ...obj2 };
      };

      const obj1 = { a: 1, b: 2 };
      const obj2 = { b: 3, c: 4 };

      expect(mergeObjects(obj1, obj2)).toEqual({ a: 1, b: 3, c: 4 });
    });

    it('should remove null/undefined values', () => {
      const removeNullValues = (obj) => {
        return Object.fromEntries(
          Object.entries(obj).filter(([_, value]) => value != null),
        );
      };

      const testObj = { a: 1, b: null, c: undefined, d: 2 };
      expect(removeNullValues(testObj)).toEqual({ a: 1, d: 2 });
    });
  });
});