-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsync.spec.ts
More file actions
223 lines (194 loc) · 7.15 KB
/
Copy pathsync.spec.ts
File metadata and controls
223 lines (194 loc) · 7.15 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Yargs from 'yargs/yargs';
import readline from 'readline';
import { builder, coerceLog, command, handler, LOG_FILENAME } from './sync';
import { FileLog } from '../../common/file-log';
import { getContentByIds } from '../../common/content-item/get-content-items-by-ids';
import dynamicContentClientFactory from '../../services/dynamic-content-client-factory';
import { ContentItem, Hub, Job } from 'dc-management-sdk-js';
import { getContent } from '../../common/filter/fetch-content';
jest.mock('readline');
const mockSync = jest.fn();
const mockOnIdle = jest.fn();
const mockFailedJobs = jest.fn();
jest.mock('./sync.service', () => {
return {
ContentItemSyncService: jest.fn().mockImplementation(() => {
return {
sync: mockSync,
onIdle: mockOnIdle,
failedJobs: mockFailedJobs
};
})
};
});
jest.mock('../../common/content-item/get-content-items-by-ids', () => {
return {
getContentByIds: jest.fn()
};
});
jest.mock('../../common/filter/fetch-content', () => {
return {
getContent: jest.fn()
};
});
jest.mock('../../services/dynamic-content-client-factory');
describe('content-item sync', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should command should defined', function () {
expect(command).toEqual('sync [id]');
});
describe('builder', () => {
it('should configure command arguments', () => {
const argv = Yargs(process.argv.slice(2));
const spyPositional = jest.spyOn(argv, 'positional').mockReturnThis();
const spyOption = jest.spyOn(argv, 'option').mockReturnThis();
builder(argv);
expect(spyPositional).toHaveBeenCalledWith('id', {
type: 'string',
describe: `The ID of a content item to sync. If id is not provided, this command will sync ALL content items through all content repositories in the hub.`
});
expect(spyOption).toHaveBeenCalledWith('repoId', {
type: 'string',
describe: 'The ID of a content repository to search items in to be sync.',
requiresArg: false
});
expect(spyOption).toHaveBeenCalledWith('folderId', {
type: 'string',
describe: 'The ID of a folder to search items in to be sync.',
requiresArg: false
});
expect(spyOption).toHaveBeenCalledWith('facet', {
type: 'string',
describe:
"Publish content matching the given facets. Provide facets in the format 'label:example name,locale:en-GB', spaces are allowed between values. A regex can be provided for text filters, surrounded with forward slashes. For more examples, see the readme."
});
expect(spyOption).toHaveBeenCalledWith('f', {
type: 'boolean',
boolean: true,
describe: 'If present, there will be no confirmation prompt before publishing the found content.'
});
expect(spyOption).toHaveBeenCalledWith('s', {
type: 'boolean',
boolean: true,
describe: 'If present, no log file will be produced.'
});
expect(spyOption).toHaveBeenCalledWith('logFile', {
type: 'string',
default: LOG_FILENAME,
describe: 'Path to a log file to write to.',
coerce: coerceLog
});
expect(spyOption).toHaveBeenCalledWith('destinationHubId', {
type: 'string',
describe: 'The ID of a destination hub to sync with.',
requiresArg: true,
demandOption: true
});
expect(spyOption).toHaveBeenCalledWith('ignoreSchemaValidation', {
type: 'boolean',
boolean: true,
describe: 'Ignore schema validation when syncing content items.'
});
expect(spyOption).toHaveBeenCalledWith('forceSync', {
type: 'boolean',
boolean: true,
describe: 'Sync destination content item when modified (overwrite destination modifications).'
});
});
});
describe('handler', () => {
const HUB_ID = '67d1c1c7642fa239dbe15164';
const DEST_HUB_ID = '67d2a201642fa239dbe1523d';
const globalArgs = {
$0: 'test',
_: ['test'],
json: true,
clientId: 'client-id',
clientSecret: 'client-secret',
hubId: HUB_ID
};
const mockLog = {
open: jest.fn().mockReturnValue({
appendLine: jest.fn(),
addComment: jest.fn(),
close: jest.fn()
})
} as unknown as FileLog;
it('should sync content item by id', async () => {
const CONTENT_ITEM_ID = 'c5b659df-680e-4711-bfbe-84eaa10d76cc';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(readline as any).setResponses(['y']);
const mockGetHub = jest.fn();
(dynamicContentClientFactory as jest.Mock).mockReturnValue({
hubs: {
get: mockGetHub.mockResolvedValue(new Hub({ id: HUB_ID }))
}
});
(getContentByIds as unknown as jest.Mock).mockResolvedValue([
new ContentItem({ id: CONTENT_ITEM_ID, body: { _meta: {} } })
]);
mockSync.mockImplementation((destinationHubId, hub, contentItem, fn) => {
fn(new Job({ id: '68e5289f0aba3024bde050f9', status: 'COMPLETE' }));
});
mockFailedJobs.mockReturnValue(0);
await handler({
...globalArgs,
id: CONTENT_ITEM_ID,
destinationHubId: DEST_HUB_ID,
ignoreSchemaValidation: true,
forceSync: true,
logFile: mockLog
});
expect(getContentByIds).toHaveBeenCalledWith(expect.any(Object), [CONTENT_ITEM_ID]);
expect(mockSync).toHaveBeenCalledTimes(1);
expect(mockSync).toHaveBeenCalledWith(
DEST_HUB_ID,
expect.any(Hub),
expect.any(ContentItem),
expect.any(Function),
{ forceSync: true, ignoreSchemaValidation: true }
);
});
it('should sync content items by query', async () => {
const CONTENT_ITEM_ID = 'c5b659df-680e-4711-bfbe-84eaa10d76cc';
const REPOSITORY_ID = 'c5b659df-680e-4711-bfbe-84eaa10d76cc';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(readline as any).setResponses(['y']);
const mockGetHub = jest.fn();
(dynamicContentClientFactory as jest.Mock).mockReturnValue({
hubs: {
get: mockGetHub.mockResolvedValue(new Hub({ id: HUB_ID }))
}
});
(getContent as unknown as jest.Mock).mockResolvedValue([
new ContentItem({ id: CONTENT_ITEM_ID, body: { _meta: {} } })
]);
mockSync.mockImplementation((destinationHubId, hub, contentItem, fn) => {
fn(new Job({ id: '68e5289f0aba3024bde050f9', status: 'COMPLETE' }));
});
mockFailedJobs.mockReturnValue(0);
await handler({
...globalArgs,
repoId: REPOSITORY_ID,
destinationHubId: DEST_HUB_ID,
logFile: mockLog
});
expect(getContent).toHaveBeenCalledWith(expect.any(Object), expect.any(Hub), undefined, {
repoId: REPOSITORY_ID,
folderId: undefined,
enrichItems: true,
status: 'ACTIVE'
});
expect(mockSync).toHaveBeenCalledTimes(1);
expect(mockSync).toHaveBeenCalledWith(
DEST_HUB_ID,
expect.any(Hub),
expect.any(ContentItem),
expect.any(Function),
{ forceSync: undefined, ignoreSchemaValidation: undefined }
);
});
});
});