-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
UserService.spec.ts
95 lines (74 loc) · 3.47 KB
/
UserService.spec.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { ReencryptTypeAItems } from './../Encryption/UseCase/TypeA/ReencryptTypeAItems'
import { EncryptionProviderInterface } from './../Encryption/EncryptionProviderInterface'
import { UserApiServiceInterface } from '@standardnotes/api'
import { UserRequestType } from '@standardnotes/common'
import { User } from '@standardnotes/responses'
import {
AlertService,
ChallengeServiceInterface,
InternalEventBusInterface,
ItemManagerInterface,
ProtectionsClientInterface,
} from '..'
import { SessionsClientInterface } from '../Session/SessionsClientInterface'
import { StorageServiceInterface } from '../Storage/StorageServiceInterface'
import { SyncServiceInterface } from '../Sync/SyncServiceInterface'
import { UserService } from './UserService'
describe('UserService', () => {
let sessionManager: SessionsClientInterface
let syncService: SyncServiceInterface
let storageService: StorageServiceInterface
let itemManager: ItemManagerInterface
let encryptionService: EncryptionProviderInterface
let alertService: AlertService
let challengeService: ChallengeServiceInterface
let protectionService: ProtectionsClientInterface
let userApiService: UserApiServiceInterface
let reencryptTypeAItems: ReencryptTypeAItems
let internalEventBus: InternalEventBusInterface
const createService = () =>
new UserService(
sessionManager,
syncService,
storageService,
itemManager,
encryptionService,
alertService,
challengeService,
protectionService,
userApiService,
reencryptTypeAItems,
internalEventBus,
)
beforeEach(() => {
sessionManager = {} as jest.Mocked<SessionsClientInterface>
sessionManager.getSureUser = jest.fn().mockReturnValue({ uuid: '1-2-3' } as jest.Mocked<User>)
syncService = {} as jest.Mocked<SyncServiceInterface>
storageService = {} as jest.Mocked<StorageServiceInterface>
itemManager = {} as jest.Mocked<ItemManagerInterface>
encryptionService = {} as jest.Mocked<EncryptionProviderInterface>
alertService = {} as jest.Mocked<AlertService>
challengeService = {} as jest.Mocked<ChallengeServiceInterface>
protectionService = {} as jest.Mocked<ProtectionsClientInterface>
userApiService = {} as jest.Mocked<UserApiServiceInterface>
internalEventBus = {} as jest.Mocked<InternalEventBusInterface>
})
it('should submit a user request to the server', async () => {
userApiService.submitUserRequest = jest.fn().mockReturnValue({ data: { success: true } })
expect(await createService().submitUserRequest(UserRequestType.ExitDiscount)).toBeTruthy()
})
it('should indicate error if submit a user request to the server fails', async () => {
userApiService.submitUserRequest = jest.fn().mockReturnValue({ data: { success: false } })
expect(await createService().submitUserRequest(UserRequestType.ExitDiscount)).toBeFalsy()
})
it('should indicate error if submit a user request to the server fails with an error on server side', async () => {
userApiService.submitUserRequest = jest.fn().mockReturnValue({ data: { error: { message: 'fail' } } })
expect(await createService().submitUserRequest(UserRequestType.ExitDiscount)).toBeFalsy()
})
it('should indicate error if submitting a user request throws an exception', async () => {
userApiService.submitUserRequest = jest.fn().mockImplementation(() => {
throw new Error('Oops')
})
expect(await createService().submitUserRequest(UserRequestType.ExitDiscount)).toBeFalsy()
})
})