-
Notifications
You must be signed in to change notification settings - Fork 86
/
suggest.spec.ts
116 lines (92 loc) · 3.52 KB
/
suggest.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
const testItems = [
{
_id: 1,
slugline: 'slugline',
word_count: 10,
firstcreated: 'ddmmyy',
original_creator: 'john',
headline: 'headline',
duedate: 'due-ddmmyy',
tasks: 'tasks',
attachements: 'attachements',
links: 'links',
comments: ['a', 'b'],
body_html: 'body',
}, {
_id: 2,
slugline: 'slugline2',
word_count: 11,
firstcreated: 'ddmmyy2',
original_creator: 'jim',
headline: 'headline2',
duedate: 'due-ddmmyy2',
tasks: 'tasks2',
attachements: 'attachements2',
links: 'links2',
comments: ['a2', 'b2'],
body_html: 'body2',
},
];
describe('suggest', () => {
beforeEach(window.module('superdesk.core.api'));
beforeEach(window.module('superdesk.apps.authoring.autosave'));
beforeEach(window.module('superdesk.apps.authoring.suggest'));
it('should initialize as inactive', inject((suggest) => {
expect(suggest.active).toBe(false);
}));
it('should correctly set active state', inject((suggest) => {
expect(suggest.active).toBe(false);
suggest.setActive();
expect(suggest.active).toBe(true);
}));
it('should not autosave on trigger when inactive', inject((suggest, autosave) => {
spyOn(autosave, 'save');
suggest.setActive(false);
suggest.trigger(testItems[0], testItems[0]);
expect(autosave.save).not.toHaveBeenCalled();
}));
it('should autosave on trigger when active', inject((suggest, autosave, $q) => {
spyOn(autosave, 'save').and.returnValue($q.reject());
suggest.setActive(true);
suggest.trigger(testItems[0], testItems[1]);
expect(autosave.save.calls.count()).toBe(1);
expect(autosave.save).toHaveBeenCalledWith(testItems[0], testItems[1], 0);
}));
it('should get suggestions when triggered', inject((suggest, autosave, $q, api, $rootScope) => {
const item = testItems[0];
spyOn(autosave, 'save').and.returnValue($q.when(item));
spyOn(api, 'get').and.returnValue($q.reject());
suggest.setActive();
suggest.trigger(item, item);
$rootScope.$digest();
expect(api.get.calls.count()).toBe(1);
expect(api.get).toHaveBeenCalledWith(`suggestions/${item._id}`);
}));
it('should get suggestion of first item in array when triggered',
inject((suggest, autosave, $q, api, $rootScope) => {
const item = testItems[0];
spyOn(autosave, 'save').and.returnValue($q.when(testItems));
spyOn(api, 'get').and.returnValue($q.reject());
suggest.setActive();
suggest.trigger(item, item);
$rootScope.$digest();
expect(api.get.calls.count()).toBe(1);
expect(api.get).toHaveBeenCalledWith(`suggestions/${item._id}`);
}),
);
it('should trigger listeners on success', inject((suggest, autosave, $q, api, $rootScope) => {
const item = testItems[0];
let response = null;
spyOn(autosave, 'save').and.returnValue($q.when(testItems));
spyOn(api, 'get').and.returnValue($q.when('return value'));
spyOn(suggest, 'onUpdate').and.callThrough();
suggest.setActive();
suggest.onUpdate((v) => {
response = v;
});
suggest.trigger(item, item);
$rootScope.$digest();
expect(suggest.onUpdate.calls.count()).toBe(1);
expect(response).toBe('return value');
}));
});