-
Notifications
You must be signed in to change notification settings - Fork 368
/
run.service.unit.test.ts
142 lines (123 loc) · 4.42 KB
/
run.service.unit.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
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
import { expect, describe, it, vi } from 'vitest';
import SyncRun from './run.service.js';
import * as ConfigService from './config/config.service.js';
import environmentService from '../environment.service.js';
import LocalFileService from '../file/local.service.js';
import type { IntegrationServiceInterface } from '../../models/Sync.js';
import { SyncType } from '../../models/Sync.js';
import type { Environment } from '../../models/Environment.js';
class integrationServiceMock implements IntegrationServiceInterface {
async runScript() {
return {
success: true
};
}
async cancelScript() {
return;
}
}
const integrationService = new integrationServiceMock();
const dryRunConfig = {
integrationService: integrationService as unknown as IntegrationServiceInterface,
writeToDb: false,
nangoConnection: {
connection_id: '1234',
provider_config_key: 'test_key',
environment_id: 1
},
syncName: 'test_sync',
syncType: SyncType.INCREMENTAL,
syncId: 'some-sync',
syncJobId: 123,
activityLogId: 123,
debug: true
};
describe('SyncRun', () => {
it('should initialize correctly', () => {
const config = {
integrationService: integrationService as unknown as IntegrationServiceInterface,
writeToDb: true,
nangoConnection: {
connection_id: '1234',
provider_config_key: 'test_key',
environment_id: 1
},
syncName: 'test_sync',
syncType: SyncType.INCREMENTAL,
syncId: 'some-sync',
syncJobId: 123,
activityLogId: 123,
loadLocation: '/tmp',
debug: true
};
const syncRun = new SyncRun(config);
expect(syncRun).toBeTruthy();
expect(syncRun.writeToDb).toEqual(true);
expect(syncRun.nangoConnection.connection_id).toEqual('1234');
expect(syncRun.syncName).toEqual('test_sync');
expect(syncRun.syncType).toEqual(SyncType.INCREMENTAL);
expect(syncRun.syncId).toEqual('some-sync');
expect(syncRun.syncJobId).toEqual(123);
expect(syncRun.activityLogId).toEqual(123);
expect(syncRun.loadLocation).toEqual('/tmp');
expect(syncRun.debug).toEqual(true);
});
it('should mock the run method in dry run mode with different fail and success conditions', async () => {
const syncRun = new SyncRun(dryRunConfig);
vi.spyOn(environmentService, 'getById').mockImplementation(() => {
return Promise.resolve({
id: 1,
name: 'test',
account_id: 1,
secret_key: '1234'
} as Environment);
});
vi.spyOn(ConfigService, 'getSyncConfig').mockImplementation(() => {
return Promise.resolve({
integrations: {
test_key: {
test_sync: {
runs: 'every 6h',
returns: ['Foo']
}
}
},
models: {
Foo: {
name: 'Foo'
}
}
});
});
vi.spyOn(LocalFileService, 'checkForIntegrationDistFile').mockImplementation(() => {
return {
result: true,
path: '/tmp'
};
});
vi.spyOn(integrationService, 'runScript').mockImplementation(() => {
return Promise.resolve({
success: true,
response: { success: true }
});
});
const run = await syncRun.run();
expect(run).toEqual({ success: true });
// if integration file not found it should return false
vi.spyOn(LocalFileService, 'checkForIntegrationDistFile').mockImplementation(() => {
return {
result: false,
path: '/tmp'
};
});
const failRun = await syncRun.run();
expect(failRun.response).toEqual(false);
// if run script returns null then fail
// @ts-expect-error we are faking a wrong return
vi.spyOn(integrationService, 'runScript').mockImplementation(() => {
return Promise.resolve(null);
});
const { response } = await syncRun.run();
expect(response).toEqual(false);
});
});