forked from heusalagroup/fi.hg.backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoServiceImpl.test.ts
68 lines (57 loc) · 2.62 KB
/
CryptoServiceImpl.test.ts
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
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { randomInt } from "crypto";
import { CryptoService } from "../core/crypto/CryptoService";
import { CryptoServiceImpl } from "./CryptoServiceImpl";
jest.mock('crypto', () => ({
randomInt: jest.fn()
}));
describe('CryptoServiceImpl', () => {
describe('createRandomInteger', () => {
it('should throw error if size is less than or equal to 0', () => {
const size = 0;
expect(() => {
CryptoServiceImpl.createRandomInteger(size)
}).toThrow(`CryptoService.createRandomNumberString: size must be over 0: ${size} provided`);
});
it('should return random integer for given size', () => {
const size = 2;
const expectedValue = 43;
(randomInt as jest.Mock).mockReturnValueOnce(expectedValue);
const result = CryptoServiceImpl.createRandomInteger(size);
expect(result).toEqual(expectedValue);
expect(randomInt).toHaveBeenCalledWith(0, Math.pow(10, size) - 1);
});
});
describe('createRandomIntegerString', () => {
it('should return random integer string for given size', () => {
const size = 2;
const intValue = 7;
const expectedValue = "07";
jest.spyOn(CryptoServiceImpl, 'createRandomInteger').mockReturnValueOnce(intValue);
const result = CryptoServiceImpl.createRandomIntegerString(size);
expect(result).toEqual(expectedValue);
});
});
describe('instance methods', () => {
let service: CryptoService;
beforeEach(() => {
service = CryptoServiceImpl.create();
});
it('should call static createRandomInteger method', () => {
const size = 2;
const expectedValue = 43;
jest.spyOn(CryptoServiceImpl, 'createRandomInteger').mockReturnValueOnce(expectedValue);
const result = service.createRandomInteger(size);
expect(result).toEqual(expectedValue);
expect(CryptoServiceImpl.createRandomInteger).toHaveBeenCalledWith(size);
});
it('should call static createRandomIntegerString method', () => {
const size = 2;
const expectedValue = "07";
jest.spyOn(CryptoServiceImpl, 'createRandomIntegerString').mockReturnValueOnce(expectedValue);
const result = service.createRandomIntegerString(size);
expect(result).toEqual(expectedValue);
expect(CryptoServiceImpl.createRandomIntegerString).toHaveBeenCalledWith(size);
});
});
});