forked from fission-codes/keystore-idb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
80 lines (66 loc) · 2.19 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
import KeyStore from '../src'
import ECCKeyStore from '../src/ecc/keystore'
import RSAKeyStore from '../src/rsa/keystore'
import config from '../src/config'
import errors from '../src/errors'
import { CryptoSystem } from '../src/types'
import IDB from '../src/idb'
jest.mock('../src/idb')
describe('keystore', () => {
describe('init', () => {
describe('ecc enabled', () => {
beforeEach(async () => {
const mock = jest.spyOn(config, 'eccEnabled')
mock.mockResolvedValue(true)
})
it('should instantiate an ecc keystore if not specified', async () => {
const resp = await KeyStore.init()
const eccKeystore = await ECCKeyStore.init()
expect(resp).toStrictEqual(eccKeystore)
})
it('should instantiate an rsa keystore if specified', async () => {
const resp = await KeyStore.init({ type: CryptoSystem.RSA })
const rsaKeystore = await RSAKeyStore.init()
expect(resp).toStrictEqual(rsaKeystore)
})
})
describe('ecc not enabled', () => {
beforeEach(async () => {
jest.spyOn(config, 'eccEnabled').mockResolvedValue(false)
})
it('should instantiate an rsa keystore if not specified', async () => {
const resp = await KeyStore.init()
const rsaKeystore = await RSAKeyStore.init()
expect(resp).toStrictEqual(rsaKeystore)
})
it('should throw an error if ecc is specified', async () => {
let error
try{
await KeyStore.init({ type: CryptoSystem.ECC })
}catch(err){
error = err
}
expect(error).toEqual(errors.ECCNotEnabled)
})
it('should throw an error if an unsupported type of crypto is specified', async () => {
let error
try{
await KeyStore.init({ type: 'some-other-crypto' as any })
}catch(err){
error = err
}
expect(error).toEqual(errors.UnsupportedCrypto)
})
})
})
describe('clear', () => {
let mock: jest.SpyInstance
beforeAll(async () => {
mock = jest.spyOn(IDB, 'clear')
await KeyStore.clear()
})
it('calls IDB.clear once', () => {
expect(mock).toBeCalledTimes(1)
})
})
})