/
backups.state.spec.ts
124 lines (88 loc) · 3.75 KB
/
backups.state.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
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
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { of, throwError } from 'rxjs';
import { onErrorResumeNext } from 'rxjs/operators';
import { IMock, It, Mock, Times } from 'typemoq';
import {
BackupsDto,
BackupsService,
BackupsState,
DialogService
} from '@app/shared/internal';
import { TestValues } from './_test-helpers';
import { createBackup } from './../services/backups.service.spec';
describe('BackupsState', () => {
const {
app,
appsState
} = TestValues;
const backup1 = createBackup(12);
const backup2 = createBackup(13);
let dialogs: IMock<DialogService>;
let backupsService: IMock<BackupsService>;
let backupsState: BackupsState;
beforeEach(() => {
dialogs = Mock.ofType<DialogService>();
backupsService = Mock.ofType<BackupsService>();
backupsState = new BackupsState(appsState.object, backupsService.object, dialogs.object);
});
afterEach(() => {
backupsService.verifyAll();
});
describe('Loading', () => {
it('should load backups', () => {
backupsService.setup(x => x.getBackups(app))
.returns(() => of(new BackupsDto(2, [backup1, backup2], {}))).verifiable();
backupsState.load().subscribe();
expect(backupsState.snapshot.backups).toEqual([backup1, backup2]);
expect(backupsState.snapshot.isLoaded).toBeTruthy();
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.never());
});
it('should show notification on load when reload is true', () => {
backupsService.setup(x => x.getBackups(app))
.returns(() => of(new BackupsDto(2, [backup1, backup2], {}))).verifiable();
backupsState.load(true, false).subscribe();
expect().nothing();
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once());
});
it('should show notification on load error when silent is false', () => {
backupsService.setup(x => x.getBackups(app))
.returns(() => throwError({}));
backupsState.load(true, false).pipe(onErrorResumeNext()).subscribe();
expect().nothing();
dialogs.verify(x => x.notifyError(It.isAny()), Times.once());
});
it('should not show notification on load error when silent is true', () => {
backupsService.setup(x => x.getBackups(app))
.returns(() => throwError({}));
backupsState.load(true, true).pipe(onErrorResumeNext()).subscribe();
expect().nothing();
dialogs.verify(x => x.notifyError(It.isAny()), Times.never());
});
});
describe('Updates', () => {
beforeEach(() => {
backupsService.setup(x => x.getBackups(app))
.returns(() => of(new BackupsDto(2, [backup1, backup2], {}))).verifiable();
backupsState.load().subscribe();
});
it('should not add backup to snapshot', () => {
backupsService.setup(x => x.postBackup(app))
.returns(() => of({})).verifiable();
backupsState.start().subscribe();
expect(backupsState.snapshot.backups.length).toBe(2);
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once());
});
it('should not remove backup from snapshot', () => {
backupsService.setup(x => x.deleteBackup(app, backup1))
.returns(() => of({})).verifiable();
backupsState.delete(backup1).subscribe();
expect(backupsState.snapshot.backups.length).toBe(2);
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once());
});
});
});